问题
I am working on selenium webdriver using java, and I am facing a problem. I have a field on a page whose id and xpath changes every time that page is loaded. Like, when I open that page manually, I can see some id and xpath, but when my test run, the id, xpath has changed. Example,
.//*[@id='dp1445259656810']
.//*[@id='dp1445260051638']
.//*[@id='dp1445260078674']
.//*[@id='dp1445260154173']
these are the xpaths that are created every time I refresh my page. How can I locate/find such an element during my run test. Any help will be highly appreciated.
Below is the HTML code for the field.
<div class="form-group event-date">
<div class="input-group">
<input id="dp1445260154173" class="form-control ng-pristine ng-
untouched ng-valid ng-isolate-scope hasDatepicker"
enter-action="datePickerEnter(event)" allow-empty=""
has-datepicker="" ng-model="eventSeries.newEvent.date"
placeholder="Event Date"/>
<span class="input-group-addon">
<i class="fa fa-calendar"/>
</span>
</div>
</div>
回答1:
Usually when ID or Class attributes are dynamic, you need to look for them using: other attributes/by their dependence to static tags.
Here is an example of looking for a "Compose" button in Gmail. It has: - Dynamic ID - Dynamic Class - One static unique attribute
Here is the HTML code:
<div class="T-I J-J5-Ji T-I-KE L3" tabindex="0" role="button" style="-moz-user-select: none;" gh="cm">COMPOSE</div>
So my xpath would be:
//div[@gh='cm']
Or imagine that you are looking for a "Send message" button on Gmail. It has: - Dynamic ID - Dynamic Class - One static unique attribute
The HTML code:
<div id=":ly" class="T-I J-J5-Ji aoO T-I-atl L3" tabindex="1" role="button" style="-moz-user-select: none;" data-tooltip="Send (Ctrl-Enter)" aria-label="Send (Ctrl-Enter)" data-tooltip-delay="800">Send</div>
This example uses CSS selector:
[aria-label*='Enter)']
the rule here is, when something is dynamic, look for it by using something static as a reference.
In your case it will be:
//div[@class="form-group event-date"]/input
works if your div-class is not dynamic.
回答2:
Most likely you need to fetch it based on text. Selenium does allow to locate elements based on text. In Python binding there are these selector methods:
- find_element_by_link_text
- find_element_by_partial_link_text
- find_element_by_css_selector (if applicable)
You just figure out the corresponding ones in Java and head up.
回答3:
Without more HTML I don't know how specific these need to be but I generally use CSS Selectors to do things like this. Some examples that might help...
A more simple selector would be div.form-group.event-date
which looks for a DIV
that has classes (.) form-group
and event-date
.
You can look for parents and children using something like div.form-group.event-date > div.input-group
which finds the same DIV
above that has a child (>) DIV
that has the class input-group
.
You can also look for HTML attributes like ng-model
from the INPUT
in question using the selector input[ng-model='eventSeries.newEvent.date']
or using the placeholder
attribute with the selector input[placeholder='Event Date']
.
If that still isn't enough, you can go crazy and chain a bunch of these together to find very specific elements in a specific structure, e.g. div.form-group.event-date > div.input-group > input[placeholder='Event Date']
.
Try some of these and see if any of them help.
There are a lot of great resources on the web about CSS selectors. Google some and learn more about them. They are a very powerful tool in finding elements on the page. Here's a CSS reference that I use a lot:
http://www.w3.org/TR/selectors/#selectors
The articles on MDN are all really good also.
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors
来源:https://stackoverflow.com/questions/33215336/how-to-locate-find-element-whose-id-and-xpath-changes-on-every-refresh-seleniu