如果您刚开始使用 selenium,这是一个合法的问题。我总是建议看看official doc。
隐式等待是告诉 WebDriver 轮询 DOM
尝试查找一个或多个元素(如果它们是)时的时间量
无法立即使用。默认设置为 0。一旦设置,
为 WebDriver 对象实例的生命周期设置了隐式等待。
例子:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
在这种情况下,如果 DOM 中不存在 ID 为 myDynamicElement 的 Web 元素,则当您尝试定位它时,您表示要重试直到 10 秒。轮询时间取决于您使用的网络驱动程序。你必须知道的是它会在 10 秒内尝试查找元素。当然,如果元素位于这个时间结束之前,代码会继续。否则抛出异常。
显式等待是您定义的等待特定条件的代码
在继续代码之前发生。最坏的情况是
Thread.sleep(),它将条件设置为一个确切的时间段
等待。提供了一些方便的方法来帮助您编写
只等待所需时间的代码。 WebDriverWait in
与 ExpectedCondition 结合是一种方法
完成。
例子:
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
在抛出 TimeoutException 或如果
它发现元素将在 0 - 10 秒内返回它。 WebDriverWait
默认情况下,每 500 毫秒调用一次 ExpectedCondition,直到它
成功返回。的成功返回值
ExpectedCondition 函数类型是布尔值 true,或
非空对象。
而且,最后写着:这个例子在功能上也等同于第一个隐式等待例子。
因此,如果您使用 presenceOfElementLocated 作为预期条件(对于您尝试定位的每个元素),则与使用隐式等待完全相同。但条件不只是这个。从ExpectedConditions 可以看出,您可以指定其他条件(例如:elementToBeClickable、stalenessOf 等)。
所以,回到你的问题:
有
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
你是说要等待,每当你试图定位一个元素,存在(记住,它就像存在的元素定位)直到 30 秒.
有
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
您将需要加载网页的时间设置为 30 秒。
与:
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
boolean status = element.isDisplayed();
您是说要等待 id:"id" 的 web 元素的 visibilityOfElementLocated,直到 30 秒。
最后:
为什么要给后面的wait分配一个WebElement,
WebElement 元素接收什么?这是对的吗
实施?
WebElement 元素接收什么?如果不是带有 id:"id" 的 webelement,还有什么?当然,如果它是可见的。否则会抛出异常。