Bas*_*DUR 0 python selenium primefaces
我正在尝试编写 Selenium 测试,但问题是我了解到该页面是使用 PrimeFaces 生成的,因此元素 ID 会不时随机更改。不使用 ID 不是很可靠。有什么我可以做的吗?
没有有意义的稳定 ID 不是问题,因为总是有其他方法可以在页面上定位元素。仅举几个选项:
部分 id 与 XPath 或 CSS 匹配,例如:
# contains
driver.find_element_by_css_selector("span[id*=customer]")
driver.find_element_by_xpath("//span[contains(@id, 'customer')]")
# starts with
driver.find_element_by_css_selector("span[id^=customer]")
driver.find_element_by_xpath("//span[starts-with(@id, 'customer')]")
# ends with
driver.find_element_by_css_selector("span[id$=customer]")
Run Code Online (Sandbox Code Playgroud)引用/告诉有关数据类型的一些有价值信息的类(“面向数据的定位器”):
driver.find_element_by_css_selector(".price")
driver.find_element_by_class_name("price")
Run Code Online (Sandbox Code Playgroud)从标签横过来:
# <label>Price</label><span id="65123safg12">10.00</span>
driver.find_element_by_xpath("//label[.='Price']/following-sibling::span")
Run Code Online (Sandbox Code Playgroud)通过链接文本或部分链接文本链接:
driver.find_element_by_link_text("Information")
driver.find_element_by_partial_link_text("more")
Run Code Online (Sandbox Code Playgroud)而且,当然,您可以发挥创意并将它们结合起来。还有更多:
还有这个相关的线程,它介绍了在选择一种方法来定位页面上的元素时的最佳实践:
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |