自动化场景是获取 weblist 中的所有链接,然后在循环外获取此 arraylist 内容,然后根据 url 命中动态获取索引号。
String values="";
List<WebElement> url_link = driver.findElements(By.cssSelector(".anomaly>a"));
for ( WebElement we: url_link) {
String temp = values;
values = temp + we.getText();
}
System.out.println("Text "+values);
int ind=values.indexOf("www.test.com");
System.out.println("Index "+ind);
上面的代码返回了一个奇怪的索引号 74。
url_link输出内容为:
wwww.hatch.com
wwww.tist.com
wwww.helix.com
wwww.patching.com
wwww.seyh.com
wwww.test.com
wwww.toast.com
wwww.telling.com
wwww.uity.com
因此,根据预期结果,我预计索引号为 5。
任何帮助将不胜感激。
最佳答案
变量values
是一个String
类型的变量,它不是一个List
。因此,当您执行 values = temp + we.getText();
时,您基本上是将所有链接附加到单个 String
中。所以输出实际上是这样的:
wwww.hatch.comwwww.tist.comwwww.helix.comwwww.patching.comwwww.seyh.comwwww.test.comwwww.toast.comwwww.telling.comwwww.uity.com
该字符串中 www.test.com
的索引为 74。您应该将这些链接添加到 ArrayList
中,然后找到要搜索的链接的索引。
这样的事情应该有效:
List<String> values = new ArrayList<String>();
List<WebElement> url_link = driver.findElements(By.cssSelector(".anomaly>a"));
for ( WebElement we: url_link) {
values.add(we.getText());
}
int ind = values.indexOf("www.test.com");
System.out.println("Index "+ind);
关于java - Selenium-根据文本匹配获取列表索引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43682463/