Selenium隐式和显式等待,超时异常元素未找到 [英] Selenium implicit and explicit wait, timeout exception element not found

查看:211
本文介绍了Selenium隐式和显式等待,超时异常元素未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是selenium的新手(但经验丰富的Java开发人员).

I am new to selenium (but experienced java developer).

我正在使用类似以下的内容:

I am using something like below:

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);

但是,当我运行测试用例时,出现以下错误:

however when I run the test case I get below error:

Unable to locate element: {"method":"xpath","selector":"//a[contains(.,'Search&Baskets')]"}
Command duration or timeout: 4 milliseconds

我认为在抛出此异常之前,它应该至少等待15秒.从上面的日志看来,它仅在4毫秒内引发了异常. 而且我可以在控制台上看到,只要它到达那条线,就会引发异常.

I thought it should have wait atleast 15 seconds before throwing this exception. From the log above it looks like it threw exception only in 4ms. and i could see on console that as soon as it hit that line, it threw exception.

我将隐式等待设置为0并使用了显式等待.

I have implicit waiting set as 0 and using explicit wait.

我在这里想念任何东西吗?

Am i missing anything here.

另外,在显式和隐式等待中,是花费了那么多时间还是精确了那么多时间, 例如,如果我将隐式等待设置为10秒,那么这是否意味着等待精确的10秒或等待长达10秒(如果找到了元素,则继续进行,即使元素founf在第6秒也是如此)

Also, in explicit and implicit wait, is it upto that much time OR exact that much time, example if I set implicit wait as 10 sec, then does it mean wait for exact 10 sec OR wait upto 10 sec (if element found then proceed, even if element founf on 6th second)

上面同样需要显式等待吗?

is above same for explicit wait as well?

请帮助

推荐答案

让我们分析代码中正在发生的事情.

Let us analyze what is happening in our code.

我们定义了两个WebElements searchBasket searchproduct ,如下所示:

We have defined two WebElements searchBasket and searchproduct as follows :

WebElement searchBasket = pDriver.findElement(By.xpath("//a[contains(.,'Search&Baskets')]"));
WebElement searchproduct = pDriver.findElement(By.xpath("//a[contains(.,'Search a product')]"));

我们没有尝试立即在我们的代码中使用这些WebElement,所以没有影响.

We havn't tried to use those WebElements in our code at immediate basis, so had No Impact.

接下来,我们为WebElement尝试了 WebDriverWait ,如下所示:

Next, we tried WebDriverWait for an WebElement as follows :

pWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Search&Baskets')]")));

同样,我们没有捕获到结果的return type,因此,没有影响.

Again we didn't capture the return type of the result, so had No Impact.

现在,在try{}块中,我们再次尝试了 WebDriverWait :

Now, within the try{} block we have again tried WebDriverWait:

pWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Search&Baskets')]")));

但是同样,我们没有捕获/执行结果的return type.这就是为什么我们这样做时要前进的原因:

But again we havn't captured/acted on the return type of the result. That's why moving ahead when we did :

action.moveToElement(searchBasket).build().perform();

searchBasket 是指我们之前存储的WebElement:

searchBasket referred to the WebElement which we have stored earlier as :

WebElement searchBasket = pDriver.findElement(By.xpath("//a[contains(.,'Search&Baskets')]"));

因为第一个搜索结果(没有 WebDriverWait )可能根本不返回任何WebElement,而返回了 Null .

As this first search result (which was without WebDriverWait) may not have returned any WebElement at all and have returned Null.

最后,错误的最重要因素无法找到元素:{"method":"xpath","selector":"//a [contains(.,'Search& Baskets')]""} 是, WebDriverWait 实例为wait.我们一直尝试使用pWait

Finally, the most important factor for the error Unable to locate element: {"method":"xpath","selector":"//a[contains(.,'Search&Baskets')]"} is, the WebDriverWait instance was wait. Instead of using wait we have always tried to use pWait

因此,由于所有这些原因, WebDriverWait 从未在我们的代码中正确实现.

So for all these reasons, WebDriverWait was never implemented properly in our code.

Selenium文档明确提到了以下内容:

The Selenium Documentation clearly mentions the following :

警告:请勿混合使用隐式和显式等待.这样做可能导致无法预测的等待时间.例如,将隐式等待设置为10秒,将显式等待设置为15秒,则可能导致20秒后发生超时.

WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.

这篇关于Selenium隐式和显式等待,超时异常元素未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆