如何定位/查找每次刷新时 ID 和 xpath 都会更改的元素 - Selenium WebDriver Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33215336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to locate/find element whose ID and xpath changes on every refresh - Selenium WebDriver Java
提问by Uziii
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,
我正在使用 java 开发 selenium webdriver,但遇到了一个问题。我在页面上有一个字段,每次加载该页面时其 id 和 xpath 都会更改。比如,当我手动打开那个页面时,我可以看到一些 id 和 xpath,但是当我的测试运行时,id、xpath 已经改变了。例子,
.//*[@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.
这些是每次刷新页面时创建的 xpath。如何在运行测试期间定位/找到这样的元素。任何帮助将不胜感激。
Below is the HTML code for the field.
下面是该字段的 HTML 代码。
<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>
回答by Dmitry Malinovsky
Usually when ID or Class attributes are dynamic, you need to look for them using: other attributes/by their dependence to static tags.
通常当 ID 或 Class 属性是动态的时,您需要使用:其他属性/通过它们对静态标签的依赖来查找它们。
Here is an example of looking for a "Compose" button in Gmail. It has: - Dynamic ID - Dynamic Class - One static unique attribute
以下是在 Gmail 中查找“撰写”按钮的示例。它具有: - 动态 ID - 动态类 - 一个静态唯一属性
Here is the HTML code:
这是 HTML 代码:
<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:
所以我的 xpath 将是:
//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
或者想象一下,您正在 Gmail 上寻找“发送消息”按钮。它具有: - 动态 ID - 动态类 - 一个静态唯一属性
The HTML code:
HTML代码:
<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:
此示例使用 CSS 选择器:
[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.
如果您的 div-class 不是动态的,则有效。
回答by Igor Savinkin
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:
您很可能需要根据 text来获取它。Selenium 确实允许根据文本定位元素。在 Python 绑定中有这些选择器方法:
- find_element_by_link_text
- find_element_by_partial_link_text
- find_element_by_css_selector (if applicable)
- find_element_by_link_text
- find_element_by_partial_link_text
- find_element_by_css_selector(如果适用)
You just figure out the corresponding ones in Java and head up.
您只需在 Java 中找出相应的内容并抬头即可。
回答by JeffC
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...
没有更多的 HTML,我不知道这些需要有多具体,但我通常使用 CSS 选择器来做这样的事情。一些可能有帮助的例子...
A more simple selector would be div.form-group.event-date
which looks for a DIV
that has classes (.) form-group
and event-date
.
更简单的选择器是div.form-group.event-date
查找DIV
具有类 (.)form-group
和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
.
您可以使用类似的东西来查找父母和孩子div.form-group.event-date > div.input-group
,它可以找到与DIV
上面相同的DIV
具有 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']
.
您还可以像HTML属性ng-model
从INPUT
有关使用选择input[ng-model='eventSeries.newEvent.date']
或使用placeholder
与选择属性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']
.
如果这还不够,您可以疯狂地将一堆这些链接在一起,以在特定结构中找到非常特定的元素,例如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:
网上有很多关于 CSS 选择器的优秀资源。谷歌一些并了解更多关于它们的信息。它们是在页面上查找元素的非常强大的工具。这是我经常使用的 CSS 参考:
http://www.w3.org/TR/selectors/#selectors
http://www.w3.org/TR/selectors/#selectors
The articles on MDN are all really good also.
MDN 上的文章也都很不错。
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors