无法获取动态 table 中的元素,我们只有 td 标签的文本
Unable to grab an element in a dynamic table where we have only text of a td tag
我有一个table,它的数据会根据添加和删除的内容而变化。在 table 中有多个列名称、数量、类型、状态。我所拥有的只是名称文本,我需要找到具有该名称的那一行的状态字段。
问题是 html 标签具有相同的 class 名称,我尝试获取父项和兄弟项,但都失败了。请在下面找到 table 的 html 结构:
<table>
<thead> </thead>
<tbody>
<tr>
<td class = "c1">
<a class = txtclass> text1 </a>
</td>
<td class = "c1"> Qty </td>
<td class = "c2"> type </td>
<td class = "c3">
<div id = "1" class = "status1"> /div>
</td>
</tr>
<tr>
<td class = "c1">
<a> text2 </a>
</td>
<td class = "c1"> Qty </td>
<td class = "c2"> type </td>
<td class = "c3">
<div id = "2" class = "status2"> /div>
</td>
</tr>
</tbody>
</table>
所以我只有 text2,我需要获取该行的状态。
我该如何进行。我试过了
List<WebElement> ele = driver.findElements(By.xpath("//*[@class = 'txtClass'][contains(text(),'text')]"));
for(WebElement el1:ele)
{
WebElement parent = el1.findElement(By.xpath(".."));
WebElement child1= parent.findElement(By.xpath("//td[4]/div"));
System.out.println(child1.getAttribute("class"));
}
这总是给我 table 第一行状态的 class 名称。
同样我试过
WebElement child = el1.findElement(By.xpath("//following-sibling::td[4]/div[1]"));
我在 table 的第一行得到了相同的 class 名称。我认为由于所有子元素的 class 名称都相同,所以它总是会抓取第一行元素,而不是行中的元素。
请帮忙,我被困在这里很长时间了,如果您需要任何其他详细信息,请告诉我。
您正在尝试使用 -
el1.findElements(By.xpath("//following-sibling::td[4]/div[1]"));
它正在匹配您页面中格式为 td[4]/div[1]
的所有元素并检索第一个匹配项。
您必须使用以下 xpath
才能根据您的文字获取 div
下的 status
。
driver.findElement(By.xpath(".//tr/td[contains(.,'text1')]/following-sibling::td[3]/div")).getAttribute("class");
如果您要求提取所有状态,请尝试以下代码-
List<WebElement> allElements = driver.findElements(By.xpath(".//tr/td[contains(.,'text2')]/following-sibling::td[3]/div"));
for(WebElement element:allElements)
{
String status = element.getAttribute("class");
System.out.println(status);
}
我认为这种方法适合你:
获取所有 div 个包含属性状态的元素:
List<WebElement> listChildStatus = driver.findElements(By.xpath(".//tr[.//a[contains(.,'text')]]//div"));
获取包含属性状态的特定 div 元素:
WebElement childStatus = driver.findElement(By.xpath(".//tr[.//a[contains(.,'{TEXT}')]]//div"));
{TEXT} = 您拥有的文本值
您需要定位带有文本的元素,向上移动一级,然后获取具有状态的同级元素
WebElement statusElement = driver.findElement(By.xpath(".//td[a[contains(text(),'text2')]]/following-sibling::td[3]/div"));
String status = statusElement.getAttribute("class"); // will be status2
如果您不想在索引上进行中继,您可以使用 last()
查找最后一个兄弟
WebElement statusElement = driver.findElement(By.xpath(".//td[a[contains(text(),'text2')]]/following-sibling::td[last()]/div"));
我有一个table,它的数据会根据添加和删除的内容而变化。在 table 中有多个列名称、数量、类型、状态。我所拥有的只是名称文本,我需要找到具有该名称的那一行的状态字段。
问题是 html 标签具有相同的 class 名称,我尝试获取父项和兄弟项,但都失败了。请在下面找到 table 的 html 结构:
<table>
<thead> </thead>
<tbody>
<tr>
<td class = "c1">
<a class = txtclass> text1 </a>
</td>
<td class = "c1"> Qty </td>
<td class = "c2"> type </td>
<td class = "c3">
<div id = "1" class = "status1"> /div>
</td>
</tr>
<tr>
<td class = "c1">
<a> text2 </a>
</td>
<td class = "c1"> Qty </td>
<td class = "c2"> type </td>
<td class = "c3">
<div id = "2" class = "status2"> /div>
</td>
</tr>
</tbody>
</table>
所以我只有 text2,我需要获取该行的状态。
我该如何进行。我试过了
List<WebElement> ele = driver.findElements(By.xpath("//*[@class = 'txtClass'][contains(text(),'text')]"));
for(WebElement el1:ele)
{
WebElement parent = el1.findElement(By.xpath(".."));
WebElement child1= parent.findElement(By.xpath("//td[4]/div"));
System.out.println(child1.getAttribute("class"));
}
这总是给我 table 第一行状态的 class 名称。 同样我试过
WebElement child = el1.findElement(By.xpath("//following-sibling::td[4]/div[1]"));
我在 table 的第一行得到了相同的 class 名称。我认为由于所有子元素的 class 名称都相同,所以它总是会抓取第一行元素,而不是行中的元素。
请帮忙,我被困在这里很长时间了,如果您需要任何其他详细信息,请告诉我。
您正在尝试使用 -
el1.findElements(By.xpath("//following-sibling::td[4]/div[1]"));
它正在匹配您页面中格式为 td[4]/div[1]
的所有元素并检索第一个匹配项。
您必须使用以下 xpath
才能根据您的文字获取 div
下的 status
。
driver.findElement(By.xpath(".//tr/td[contains(.,'text1')]/following-sibling::td[3]/div")).getAttribute("class");
如果您要求提取所有状态,请尝试以下代码-
List<WebElement> allElements = driver.findElements(By.xpath(".//tr/td[contains(.,'text2')]/following-sibling::td[3]/div"));
for(WebElement element:allElements)
{
String status = element.getAttribute("class");
System.out.println(status);
}
我认为这种方法适合你:
获取所有 div 个包含属性状态的元素:
List<WebElement> listChildStatus = driver.findElements(By.xpath(".//tr[.//a[contains(.,'text')]]//div"));
获取包含属性状态的特定 div 元素:
WebElement childStatus = driver.findElement(By.xpath(".//tr[.//a[contains(.,'{TEXT}')]]//div"));
{TEXT} = 您拥有的文本值
您需要定位带有文本的元素,向上移动一级,然后获取具有状态的同级元素
WebElement statusElement = driver.findElement(By.xpath(".//td[a[contains(text(),'text2')]]/following-sibling::td[3]/div"));
String status = statusElement.getAttribute("class"); // will be status2
如果您不想在索引上进行中继,您可以使用 last()
查找最后一个兄弟
WebElement statusElement = driver.findElement(By.xpath(".//td[a[contains(text(),'text2')]]/following-sibling::td[last()]/div"));