gpt4 book ai didi

java - while 循环中的 .isDisplayed 条件给出 "Element Not Found Exception"

转载 作者:行者123 更新时间:2023-11-29 08:48:50 24 4
gpt4 key购买 nike

我正在尝试获取此特定页面上的所有列并在下面使用,有些事情我做得不对,因为它给出了未找到元素的异常。根据我的说法,它不应该进入 while 循环,因为条件不成立,那么这里有什么错误。

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();

String xpathstart = "html/body/div[2]/div/ul/li[";
String xpathends="]/a";

driver.get("http://www.timeanddate.com/worldclock/");
int i=1;
while(driver.findElement(By.xpath(xpathstart+i+xpathends)).isDisplayed()){
System.out.println("current column value is"+i);
i++;

}
System.out.println("total colored columns are "+i);
}

我没有问 while 循环的替代方案(使用 for 循环,我已经知道)

它在 selenium RC 中的作用

while(selenium.isElementPresent(xpathstarts+i+xpathends))
{

}

最佳答案

发生这种情况是因为 findElement 在 XPath 中找不到您引用的元素。最简单的解决方案是改用 findElements:

    WebDriver driver = new FirefoxDriver();

String xpath = "html/body/div[2]/div/ul/li";

driver.get("http://www.timeanddate.com/worldclock/");
int i=1;
List<WebElement> elements = driver.findElements(By.xpath(xpath));

for(int i = 0; i < elements.size(); ++i) {
System.out.println("current column value is"+i);

}

System.out.println("total colored columns are "+elements.size());

关于java - while 循环中的 .isDisplayed 条件给出 "Element Not Found Exception",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23752202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com