QTP – Secret on How to Enter Text in a WebElement

PerfectoTopAd

WebElement Object Recognition Issues

Assistance with Quick Test Professional WebElement Object Recognition issues is one of the most-asked questions I receive.

It's mostly due to the reasons I list in my 6 Things to Try When QTP Does Not Recognize an Object post; other times the issue is that people are having a hard time interacting with a web element – for instance, trying to enter text into a web element without any luck.

Although it can be frustrating at times to obtain the behavior you want, you can actually use the .object method of QTP to get around most interaction issues.

WebElement Frustration

Recently I received an email from a frustrated user. They wrote:

Here is a sample problem I am running into:

  • go to Boostmobile.com
  • select Shop/Phones from the menu
  • add any phone to the cart
  • enter a zip code and click check availability

We have now arrived at a place where the problem is: How would one make that State drop down select a state?”

Using the object method along with descriptive programming will allow us to get around this issue.

How to enter text into a WebElement

  • First, start QTP up and manually follow the steps above to get to the state web element that we're going to work with in the following example.


  • Click “record” on QTP, select a state from the dropdown and stop recording.
  • Notice how only the click action is recorded, without any information on what state was selected:

Browser("Checkout Step 1 | Shipping/Bil").Page("Checkout Step 1 | Shipping/Bil").Link("State").Click

  • Next, point the QTP spy on the state field.


The “State” field is identified as a webelement. (This kind of stinks, because if you look at the help for the methods of a webelement, there are no available methods for entering or selecting text.)

WebElement Methods

Method Name

Method Desc

CaptureBitmap

Saves a screen capture of the object as a .png or .bmp image, depending on the

specified file extension.

Check

Checks whether the actual value of an item matches the expected value.

CheckProperty

Checks whether the specified object property achieves the specified value within the specified timeout.

ChildObjects

Returns the collection of child objects contained within the object.

Click

Clicks the object.

Drag

Performs the ‘drag' part of a drag and drop operation.

Drop

Performs the ‘drop' part of a drag and drop operation.

FireEvent

Triggers an event.

GetROProperty

Returns the current value of the specified identification property from the object in the application.

GetTOProperties

Returns the collection of properties and values used to identify the object.

GetTOProperty

Returns the value of the specified identification property from the test object

description

MiddleClick

Middle-clicks the object.

Output

Retrieves the current value of an item and stores it in a specified location.

RefreshObject

Instructs QuickTest to re-identify the object in the application the next time a step refers to this object.

RightClick

Right-clicks the object.

SetTOProperty

Sets the value of the specified identification property in the test object description.

Submit    

Submits a form.

ToString

Returns a string that represents the current test object.

WaitProperty

Waits until the specified object property achieves the specified value or exceeds

the specified timeout before continuing to the next step.

Now what? Actually, we're in luck; we can use the webelements object method along with descriptive programming to access and interact with the native methods and properties of a webelement.

WebElement Object Property with Descriptive Programming

To select a state — Rhode Island, for instance — we can use the innertext property to select the value in the State webelement as follows:

Browser("Checkout Step 1 | Shipping/Bil").Page("Checkout Step 1 | Shipping/Bil").Link("html tag:=A","index:=1").WebElement("html tag:=SPAN").Object.innertext = "Rhode Island"

Using descriptive programming allows us to bypass the object repository and use the property/value in place of an OR name. Using the innertext property of the web element allows us to set the State name we want to use.

This should give us enough functionality to move on in our scripting.

Pretty cool!


 

19 comments
Naveen - September 29, 2012

Nice on Joe ….this will save time instead of running a loop and matching with the innertext :-)

Reply
reena - April 19, 2013

Please also add as to how to enter text in WebElement object. I have to enter quantity instead of selecting. Would innertext work
like
object.innertext = “1”
Please advise. Thanks

Reply
Saad Zaidani - May 23, 2013

Thank you so much, i’m looking for this thing 2 days ago.

Thank u.

Reply
Kevin Abel - December 20, 2013

Appreciated.

Kevin

Reply
Sam - January 20, 2014

Hi Joe,
I am facing an issue.I have recorded a script in qtp 11 in IE 9 browser.
my script has two drop downs, country and state.
When i select country, values in ‘state’ drop down updates accordingly. it is working fine manually but when i record the script and run then on selecting country from country drop down, state drop down doesn’t update and shows no values. hence script shows error .
Please suggest any solution for this problem asap.

Reply
    Joe Colantonio - January 23, 2014

    This might sound like odd suggestion but after you select the value what happens if you add a tab off? I’ve tested application that If I did not tap out of a field the values would not change.

    Reply
Kiran - May 19, 2014

I have a strange issue with the above method when I change the innertext and then perform remaining actions in my case I select dropdown selection (change innertext) and then press submit button. The result takes in whatever the default selection in the dropdown list was and then queries it. For example if the DDL(webelement) had selection ‘A’ in it when I do the innertext to ‘B’ and then do the search. It still gets tme the result for ‘A’. I would appreciate if you have any work around for this. The application is HTML5 C#.Net application

Reply
[BLOCKED BY STBV] Weblist Identified as WebElement. - August 20, 2014

[…] See if anything in this link helps QTP ? Secret on How to Enter Text in a WebElement | Joe Colantonio – Selenium-UFT-QTP-SoapUI-ALM-Loa… […]

Reply
vibhuti - October 20, 2014

Hi,

I am facing an issue, where I need to enter text in webelement. It is kind of text box. Is there is any function for doing this?

Reply
[BLOCKED BY STBV] How can I select row count from Grid drop down box - November 11, 2014

[…] I'm a little surprised object.value didn't work. Perhaps you could try object.innertext. Here is an article that shows an example QTP ? Secret on How to Enter Text in a WebElement | Joe Colantonio – Selenium-UFT-QTP-SoapUI-ALM-Loa… […]

Reply
Adrian - January 9, 2015

Hi Joe, I think the issue resolves for one WebElement in the page, but i have 5 more webelements in the same page and qtp sends me a meesage “objects description matches more than one of the objects currently displayed in you application….”, how can i identify the webelement to eidt?

Thanks for your help

Reply
    Joe Colantonio - January 13, 2015

    Hi Adrian – can you specify the index:= for each one to make them unique so that you don’t get this message?

    Reply
Madhu - April 16, 2015

Kindly, specify a solution on how to resolve if the same kind of problem arises with a desktop application

Reply
Barrie - September 2, 2015

I am running into the same issue as Kiran where I can see the value I need in the WebElement, but when I click ‘Submit’ or ‘Save’ the application is somehow not seeing the value. My example is for a State field. I use Object.innertext = “Michigan”, and can see the value get populated in the WebElement, but when I click ‘Save’ I am receiving a ‘State is required’ message since it’s a required field.

I have a strange issue with the above method when I change the innertext and then perform remaining actions in my case I select dropdown selection (change innertext) and then press submit button. The result takes in whatever the default selection in the dropdown list was and then queries it. For example if the DDL(webelement) had selection ‘A’ in it when I do the innertext to ‘B’ and then do the search. It still gets tme the result for ‘A’. I would appreciate if you have any work around for this

Reply
Jagadeesh - January 9, 2016

Hi All,

I am facing a issue, when a drop down is identified as a webelement, if I need to verify the dropdown values in the dropdown. How this can be done .Please help me…..

Reply
Jasbir Singh - November 13, 2016

Hi,

I’m facing a problem where I need to enter a string value in webelement and then click on next button but when I used Object.innertext = “Text” with webelement its just print the Test and next button disappear . I want to go to next page after entering the Text and clicking next.Please help.

Reply
Click here to add a comment

Leave a comment: