【发布时间】:2018-05-20 07:09:31
【问题描述】:
我是 selenium 新手(但经验丰富的 java 开发人员)。
我正在使用类似下面的东西:
WebElement searchBasket = pDriver.findElement(By.xpath("//a[contains(.,'Search&Baskets')]"));
WebElement searchproduct = pDriver.findElement(By.xpath("//a[contains(.,'Search a product')]"));
//if search an agreement is not show up, then click on other menu, then click it back
pWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Search&Baskets')]")));
pDriver.findElement(By.xpath("//a[contains(.,'Search&Baskets')]")).click();
// click on search an agreement
try {
pWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Search&Baskets')]")));
action = new Actions(pDriver);
action.moveToElement(searchBasket).build().perform();
pWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Search a product')]")));
searchproduct.click();
} catch (TimeoutException e) {
}
pWait 在哪里:
WebDriverWait wait = new WebDriverWait(driver, 15);
但是,当我运行测试用例时,出现以下错误:
Unable to locate element: {"method":"xpath","selector":"//a[contains(.,'Search&Baskets')]"}
Command duration or timeout: 4 milliseconds
我认为它应该至少等待 15 秒才能抛出此异常。从上面的日志看来,它似乎只在 4 毫秒内引发了异常。 我可以在控制台上看到,一旦它到达那条线,它就会抛出异常。
我将隐式等待设置为 0 并使用显式等待。
我有什么遗漏吗?
此外,在显式和隐式等待中,是最多多少时间还是确切多少时间, 例如,如果我将隐式等待设置为 10 秒,那么这是否意味着等待确切的 10 秒或等待最多 10 秒(如果找到元素,则继续,即使元素 founf 在第 6 秒)
显式等待也一样吗?
请帮忙
【问题讨论】:
-
嗨,我尝试将隐式等待设置为 0,将显式等待时间设置为 10。它恰好在第 10 秒超时。此外,不建议同时使用隐式等待和显式等待。但这应该不是问题,因为您的隐式超时为 0 秒。如果在第 6 秒内找到元素,则将进行下一步。对于隐式和显式等待,它不会等待整个 10 秒。你能把整个代码贴在这里吗?
-
谢谢...更新了代码...
标签: java selenium-webdriver webdriverwait timeoutexception implicitwait