-->

How to handle dynamic elements using Robot Framewo

2020-08-03 09:02发布

问题:

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!

回答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.



回答2:

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!


回答3:

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..



回答4:

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