How to create element's Xpath using different search ( cssSelector / tag / ClassName )

删除回忆录丶 提交于 2020-01-11 14:17:09

问题


I would like to find an element using a differect cssSelector / tag / ClassName amd them get it's xpath value ( to be more specific, I have a website where when a day changes, one of the classes change it's class) here is what do I meean:

<tr>
<td> 1.1.2019 </td>
<td> 2.1.2019 </td>
<td class="active"> 3.1.2019 </td>
<td> 4.1.2019 </td>
</tr>
<tr>
<td> </td>
<td> 10 </td>
<td> </td> #Here
<td> </td>
</tr>

I want to according to where is that "active class", click the table under it. ny idea how to do so ?

short version of what I want :

Find element using cssSelector

Get this element's Xpath <- the problem

click it using edited xpath

I want to GET XPATH OF LOCATED ELEMENT , not to locate it using Xpath


回答1:


You can find the index by locating all the <td> elements in the first row and check wich one has the index

List<WebElement> columns = driver.findElements(By.xpath("//tr[td[@class='active']]/td")); # just an example, can be any other locator
int index = 0;
for (int i = 0 ; i < columns.getSize() ; i++) {
    String attribute = columns.get(i).getAttribute("class")
    if (attribute != null && attribute.equals("active")) {
        index = i + 1;
    }
}


来源:https://stackoverflow.com/questions/54671248/how-to-create-elements-xpath-using-different-search-cssselector-tag-class

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