簡體   English   中英

如何使用java在selenium中顯式地等待driver.get()

[英]how to give wait to driver.get() explicitly in selenium using java

如何等待driver.get(),因為我們使用.get()訪問的URL是不知道的。 並且可能需要未知時間,因此我們必須給diver.get()提供30秒超時然后如何給它。

以下是它的代碼..

package org.openqa.selenium.example;

import java.util.List;

import org.openqa.selenium.By

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;


public class MyClass{

    public static void main(String[] args) throws Exception {

        // The Firefox driver supports javascript 

        WebDriver driver = new HtmlUnitDriver();

        // Go to the some websites

        driver.get("http://www.abced.com/123456/asd.htm");

        /***  Here we DONT get back the driver, so we need to Give Time out of 30 seconds**/

        final List<WebElement> element1= driver.findElements(By.tagName("a"));

    for (WebElement webElement : element1) {

        String urlFromPage = webElement.getAttribute("href");

                System.out.println(urlFromPage);

        }


     }

}

我試過了

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(thisPage.url);

它不起作用.. plz建議,tx

如果要等待頁面加載,則應使用pageLoadTimeout(long time, java.util.concurrent.TimeUnit unit)方法。 implicitlyWait(long time, java.util.concurrent.TimeUnit unit)用於等待尚未出現的元素,而不是等待頁面加載。

在WebDriver實例上,您應該調用與您使用implicitlyWait()方法鏈類似的方法鏈。 這將按順序調用:

  • manage() - 驅動程序管理界面
  • options() - 驅動程序選項界面
  • timeouts() - 超時選項界面
  • pageLoadTimeout(...) - 將超時設置為您想要的時間

你可以在這里找到相關的javadoc。

相反,您可以使用WebDriverWait指定要檢查的條件和最大超時。 這可以使用如下:

WebDriverWait _wait = new WebDriverWait(driver, new TimeSpan(0, 0, 2)); //waits 2 secs max
_wait.Until(d => d.FindElement(By.Id("name")));

我已經在這個問題上發布了類似的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM