[英]Selenium-Get list index number based on text match
自动化方案是获取Web列表中的所有链接,然后在循环外获取此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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.