通过文本查找元素并获取 xpath - selenium webdriver junit [英] Find an element by text and get xpath - selenium webdriver junit
问题描述
我的网页中有一个 9 行 6 列的表格.我想搜索文本MakeGoodDisabled-Programwise_09_44_38_461(n)"并获取单元格的xpath.我使用了以下内容但它失败了,因为它无法在页面上找到文本.你能帮忙吗?我正在使用 Selenium Webdriver Junit 对此进行编码.
I have a table with 9 rows and 6 columns in my webpage. I want to search for a text "MakeGoodDisabled-Programwise_09_44_38_461(n)" and get the xpath of the cell. I have used the following but it fails because it is not able to find the text on the page. Can you please help? I am using Selenium Webdriver Junit to code this.
List < WebElement > links = driver.findElements(By.tagName("td"));
Iterator < WebElement > itr = links.iterator();
while (itr.hasNext()) {
String test = itr.next().getText();
if (test.equals("MakeGoodDisabled-Programwise_09_44_38_461(n)")) {
String xpath = driver.findElement(By.name(test)).getAttribute("xpath");
System.out.println(xpath);
}
}
推荐答案
我的目的是在一个表格中查找一个文本,并在其中获取对应的下一列值同一行.我以为我会替换通过获取 xpath 找到的列号我想要的列号.有没有更好的办法
My intention is to find a text in a table and get the corresponding next column value in the same row. I thought that I will replace the column number found by fetching the xpath with the column number I want. is there a better way to do it
当然有办法.这是一种可能的解决方案.
Of course there's a way. Here's one possible solution.
获取所有行:
While (iterate over row)
While(Iterate over column)
if(column.Text=='YOUR_MATCH'){
int voila=column.Index
}
}
}
现在您可以简单地移动到其他行的特定索引;或者您可以使用像 .../tr/td[voila]
这样的 xpath 来检索该特定列的所有单元格.
Now you can simply move to that particular index for the other rows; or you could use xpath like .../tr/td[voila]
to retrieve all cells for that particular column.
我已经写了一个方法,请不要认为是真正的工作代码!
I've written an approach, please don't take to be real-working-code!
这篇关于通过文本查找元素并获取 xpath - selenium webdriver junit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!