Java Selenium 隐含等待不工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34422661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:38:07  来源:igfitidea点击:

Selenium implicitlyWait Not Working?

javaseleniumselenium-webdriver

提问by carol

I am learning Java Maven Selenium. I want something like this in Selenium using implicitlyWait.

我正在学习 Java Maven Selenium。我想在 Selenium 中使用类似的东西implicitlyWait

  1. Open website (for example https://www.facebook.com)
  2. Click on email field of login
  3. Wait 20 seconds
  4. Enter my email
  1. 打开网站(例如https://www.facebook.com
  2. 单击登录的电子邮件字段
  3. 等待 20 秒
  4. 输入我的电子邮件

Here is my simple code:

这是我的简单代码:

package com.org.learningMaven;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class HelloWorldTest {   
    @Test
    public void login() {
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.facebook.com/");
        driver.findElement(By.id("email")).click();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.findElement(By.id("email")).sendKeys("[email protected]");
    }
    private void sendKeys(Keys enter) {
        // TODO Auto-generated method stub

    }
}

This code is not working. It will simply open Facebook, click on email field & enter my email id instead of waiting 10 seconds before entering my email.

此代码不起作用。它将简单地打开 Facebook,单击电子邮件字段并输入我的电子邮件 ID,而不是等待 10 秒钟才输入我的电子邮件。

采纳答案by Paras

Implicit Waitand Explicit Waitsdoesn't work that way, they will maximum wait for element for the time duration specified, If they find the element before that next step would be executed.

Implicit Wait并且Explicit Waits不会以这种方式工作,他们将在指定的持续时间内最大程度地等待元素,如果他们在执行下一步之前找到元素。

If you want your test to wait for exact time duration, you may want to use.

如果您希望您的测试等待确切的持续时间,您可能需要使用。

Thread.sleep(Time duration in milliseconds);

You may want to refer Diff b/w Implict Wait and Explicit Wait

您可能想参考Diff b/w 隐式等待和显式等待

Explicit Waits: An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code.

显式等待:显式等待是您定义的代码,用于在继续执行代码之前等待特定条件发生。

Implicit Waits: An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.

隐式等待:隐式等待是告诉 WebDriver 在尝试查找一个或多个元素(如果它们不能立即可用)时轮询 DOM 一段时间。

Thread.sleep: In sleep code It will always wait for mentioned seconds, even in case the page is ready to interact after 1 sec. So this can slow the tests.

Thread.sleep:在睡眠代码中它会一直等待提到的几秒钟,即使页面在 1 秒后准备好进行交互。所以这会减慢测试速度。

回答by Prashant Shekhar

Thread.sleep halts you execution for that particular time period. That why it is not recommended to use Thread.sleep in your execution script. Where as Implicit/Explicit wait deals with particular webelement. If script finds the required web element is present in the page, script moves on. If it does not find the mentioned web element, if finds that element in the web page for that particular wait period.

Thread.sleep 会在该特定时间段内停止执行。这就是为什么不建议在执行脚本中使用 Thread.sleep 的原因。隐式/显式等待处理特定的 webelement。如果脚本发现页面中存在所需的 Web 元素,则脚本继续执行。如果它没有找到提到的 web 元素,如果在该特定等待期间在网页中找到该元素。

回答by learner

If a web element is not displaying and you want to wait for that element to be displayed then following code will work.

如果 Web 元素未显示并且您想等待该元素显示,则以下代码将起作用。

while(true) {
    boolean flag = driver.findElement(By.id("id_name")).isDisplayed();
    if(flag)
        break;
}

回答by Bart Van De Slijcke

Implement WebDriverWait

实现 WebDriverWait

public void waitForElement(WebDriver driver, WebElement element) {
    WebDriverWait wait = new WebDriverWait(driver,5);
    wait.until(ExpectedConditions.visibilityOf(element));

}