How do you get selenium to recognize that a page loaded?

后端 未结 9 1608
误落风尘
误落风尘 2020-12-16 19:40

In certain unknown situations selenium does not detect that a page has loaded when using the open method. I am using the Java API. For example (This code will not produce th

9条回答
  •  清酒与你
    2020-12-16 19:53

    Maybe this will help you....

    Consider the following method is in page called Functions.java

    public static void waitForPageLoaded(WebDriver driver) {
    
             ExpectedCondition expectation = new
        ExpectedCondition() {
                public Boolean apply(WebDriver driver) {
                  return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
                }
              };
    
              WebDriverWait wait = new WebDriverWait(driver,30);
              try {
                      wait.until(expectation);
              } catch(Throwable error) {
                      Assert.assertFalse(true, "Timeout waiting for Page Load Request to complete.");
              }
         } 
    

    And you can call this method into your function. Since it is a static method, you can directly call with the class name.

    public class Test(){
        WebDriver driver;
    
        @Test
        public void testing(){
             driver = new FirefoxDriver();
             driver.get("http://www.gmail.com");
             Functions.waitForPageLoaded(driver);
       }
    }
    

提交回复
热议问题