Selenium div attributes keep changing, how can I find this element?

不想你离开。 提交于 2021-02-10 22:21:47

问题


I am trying to find an element with Selenium and Java, the problem is that the element's id, class, and name always increment so I am not able to find it with selenium. Below is what I am currently trying:

WebElement field = driver.findElement(By.xpath("//input[contains(@linkText, 'Broadcast copy')]"));

In my html file these are the attributes that keeps changing:

id="files[%2Fopt%240%2Frules%2F%2F000102%2.xml][%2Fcluster%2Fname]"

name="files[%2Fopt%240%2Frules%2F%2F000102%2.xml][%2Fcluster%2Fname]" 

value="copy (Cluster 102)"

Entire html

<tbody>
   <tr class='rowOdd'>
      <td><b>Name</b></td>
      <td> <input type='text' data-validation='required validate-name-unique validate-name-not-empty' size='65' id='files[%2Fopt%240%2Frules%2F%2F000102%2Fcluster.xml][%2Fcluster%2Fname]' name='files[%2Fopt%240%2Frules%2F%2F000102%2Fcluster.xml][%2Fcluster%2Fname]' value='copy (Cluster 102)' /> </td>

These always increment and I have no access to the html file to change anything. So my question is how can I find this input element? Thanks in advance.

UPDATE

I get the error:

Unable to locate element:{"method":"id", "selector":"files[.*][.*]"}

回答1:


I believe the xpath you are using is incorrect. Use

//input[contains(text(), 'Broadcast copy')]

instead of

//input[contains(@linkText, 'Broadcast copy')]

According to the html you have provide the following should work as well

//body[contains(.,'Name')]//input



回答2:


Try this..

In case "copy (Cluster" text in value attribute is not changing, then you can try below xpath:-

 //body[contains(.,'Name')]//input[contains(@value,'copy (Cluster')]



回答3:


Since the attributes of id, class, and css were constantly changing, 'data-validation' was one that stayed the same all the time. So the code below worked for me.

driver.findElement(By.xpath("//input[@data-validation='required validate-name-unique validate-name-not-empty']"));


来源:https://stackoverflow.com/questions/31407402/selenium-div-attributes-keep-changing-how-can-i-find-this-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!