0

I am currently working on the Robot Framework and using Selenium2Libraries to work on a Web Application. I'm working on a Form and I'm dealing with a dynamic elements which is an editable text area and drop down list..

I really hope someone would be able to guide me on how I can do this. An example of what I am doing is,

[Example element code]

input id="textfield-1237-inputEl" class="x-form-field x-form-text x-form-text-default x-form-focus x-field-form-focus x-field-default-form-focus" 
data-ref="inputEl" size="1" name="textfield-1237-inputEl" 
maxlength="200" role="textbox" aria-hidden="false" aria-disabled="false" 
aria-readonly="false" aria-invalid="false" aria-required="false" autocomplete="off" data-componentid="textfield-1237" type="text"

Any information on this would be much appreciated. Thanks!

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Belle
  • 1
  • 1
  • 4
  • Dynamic how? Generally you can deal with any element, dynamic or static, it doesn't matter. What are you trying to test exactly? Just to see if there is data inside the text area? Seeing if you can input into that area? Use the drop list? What is it you're trying to do? – Goralight Dec 15 '16 at 09:04
  • Im trying to input text on the text area (i.e. Currency.) and the text area has a drop down list but it is editable as well in which I can input text without select from the drop down list. The issue here, I usually use element id to get the element locator(i.e.//*[@id="textfield-1160-inputEl"], but for this case, the id is dynamic. It keeps changing when everytime we reload the page.. @goralight – Belle Dec 15 '16 at 09:14
  • this is an example--> @goralight – Belle Dec 15 '16 at 09:16
  • I understand now, just answering you question :) @Belle – Goralight Dec 15 '16 at 09:18

4 Answers4

1

There are many types of Identifiers are available.you can search,If the values are dynamic you can use Xpath Identifier to find the locator.Id can be used only for the static values. In the above case you can use Xpath as

xpath=.//*[contains(type(),'text')]

because text is static.It wont be change.

Mohanapriya
  • 149
  • 1
  • 9
1

When trying to handle dynamic IDs, and elements which dont have easy UIDs about them, the best way to go around this is using Xpath.

Xpaths are basically the location of the element within the HTML. This is kind of the best way to get around the problem of not having ID readily available (My work has no IDs anywhere I can use, thus I have no choice but to use Xpaths)

Xpaths are really powerful, if used correctly. If not they are really brittle and can be a nightmare to maintain. Ill give you an example of potential Xpaths you may have to use:

Select From List By Label    xpath=(//select)[2]    DropDownItem1

You said that you have a drop down. Here is a potenital "look-a-like" you would see. The Xpath here is basically saying, find the 2nd drop down you find, anywhere on the entire HTML page.

Xpaths will take a while to get your head round, esspecially if you have had the luxurary of using IDs. The tools I use in order to locate and debug Xpaths are:

FireBug

Selenium IDE

I mainly use Selenium IDE now, as it is a nice tool which basically lets you "Select" an element within the HTML and it will spurt out its ID, CSS Path, Xpath, DOM, etc... Not only that, when you come to discover more complex Xpaths, there is a "Find" tools which shows you visually, where your Xpath is pointing to (or isnt, if its wrong)

Something which really helped me was This. It is really usful and has a lot of examples for you to work against.

If you have any problems, just reply and ill try to help

More Examples:

Click Element    //span[contains(text(), 'Submit')]
Input Text    xpath=(//textarea)[3]    Some Random Text!
Goralight
  • 2,067
  • 6
  • 25
  • 40
  • Thank you so much! Lot of info! Anyway, I tried to use class --> .//div/input[contains(@class, 'x-field-default-form-focus')] and I checked with firepath.Firepath can find it but when I use the xpath, they cannot detect the element.. @goralight – Belle Dec 15 '16 at 10:03
  • not sure if it is intentional, but remove the "." from the beginning. Try installing Selenium IDE. I get better results from that then using FirePath. – Goralight Dec 15 '16 at 10:07
  • TruePath plugin for chrome works great for me. – George Horlacher Jan 27 '21 at 23:13
  • Is there any option to check for multiple texts with OR option? Click Element //span[contains(text(), 'Submit' | 'Ok' | 'Next' | 'Further')] – user8162 Jul 06 '21 at 14:48
0

As with the other answers, I propose that you use Xpath. Using Xpath can point you to the element by identifying the relationship of that element with the other elements around it. So my suggestion is to find a static element that you could use as your starting point.

For example: starting point has static id: xpath=//td[@id='startingPoint']/following-sibling::select[1]

starting point has no id but has static text (usually the label of the field): xpath=//td[contains(text(),'Field Label')]/following-sibling::select[1]

If you could give us an idea of what the element is..we could provide you better examples..

becixb
  • 365
  • 1
  • 9
  • also just a tip when trying to identify with xpath..the google chrome console allows you to check whether your xpath is correct by typing: `$x("")` example `$x("//td[2]")` – becixb Dec 16 '16 at 06:08
  • also better to create your own xpath rather than rely on some application..you'll find it easier to create reusable keywords when using awesome xpath. example: `//td[contains(text(),'${rowValue}')]/following-sibling::td[count(//td[contains(text(),'${columnValue}')]/preceding-sibling::td)-count(//td[contains(text(),'${rowValueColumnName}')]/preceding-sibling::td)]` which gives you the value under columnValue on the same row as rowValue (which is under rowValueColumnName) – becixb Dec 16 '16 at 06:13
-1

What I did was alter the Xpath for example:

//*[@id="cec9efb093e14182b361766c26fd1919"]/section/div[1]/ticket/div/div/input

And took out the Id what was being generated dynamically cec9efb093e14182b361766c26fd1919 to switch for an autoId I set to the parent element where the Id was being generated. It's a cheap fix but it works if only one of the parent element is being generated.

So the parent element has the attribute autoid=container added to it and I referenced it as [@autoid="container"]/section/div[1]/ticket/div/div/input in the robot code

Mathews Sunny
  • 1,796
  • 7
  • 21
  • 31