如何使用不同的搜索(cssSelector/tag/ClassName)创建元素的Xpath [英] How to create element's Xpath using different search ( cssSelector / tag / ClassName )
问题描述
我想使用一个不同的cssSelector/tag/ClassName查找一个元素,并让他们得到它的xpath值(更具体地说,我有一个网站,当一天改变时,其中一个类改变了它的类)在这里我的意思是什么:
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 ?
我想要的简短版本:
使用cssSelector查找元素
Find element using cssSelector
获取该元素的Xpath<-问题
Get this element's Xpath <- the problem
使用已编辑的xpath单击它
click it using edited xpath
我想获取XPATH OF LOCATED ELEMENT,而不是使用Xpath定位
推荐答案
您可以通过在第一行中找到所有<td>
元素并检查其中是否有索引来找到索引
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;
}
}
这篇关于如何使用不同的搜索(cssSelector/tag/ClassName)创建元素的Xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!