简体   繁体   English

如何通过部分 XPath 定位元素

[英]How to locate element by partial XPath

Trying to locate a text input box by its Xpath which dynamically changes some of the last elements every time the page is loaded.试图通过其 Xpath 定位文本输入框,每次加载页面时都会动态更改一些最后的元素。

This is the HTML for it.这是它的 HTML。

<input type="text" value="" name="client[phones_attributes][1654091616613][number]" id="client_phones_attributes_1654091616613_number" class="placeholderField-input">

It's XPath is //*[@id="client_phones_attributes_1654091616613_number"]它的 XPath 是 //*[@id="client_phones_attributes_1654091616613_number"]

The numbers change every load.每次负载时,数字都会改变。 I tried locating it by using this line but it didn't find it.我尝试使用这条线来定位它,但没有找到它。

search = driver.find_element_by_xpath("//*[contains(@id, 'client_phones_attributes_')]")

you need to search as:您需要搜索为:

on last versions of selenium:在硒的最新版本中:

search = driver.find_element(By.XPATH, '//input[contains(@id, "client_phones_attributes_")]')

on your version在你的版本上

search = driver.find_element_by_xpath("//input[contains(@id, 'client_phones_attributes_')]")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM