Selenium webdriver explicit wait

后端 未结 7 1455
无人共我
无人共我 2021-01-03 10:21

I\'m writing some automated tests for using the selenium chrome driver. I trying to write a reusable method that will explicitly wait for elements to appear and then call th

相关标签:
7条回答
  • 2021-01-03 10:40

    Just use this method.I hope it will work perfectly.

    public void waitForElement(String item) {
        WebDriverWait wait = new WebDriverWait(driver,30);
        WebElement element =  wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("item")));
    }
    

    Then call the method :

    waitForElement("new-message-button");
    
    0 讨论(0)
  • 2021-01-03 10:43

    I built a package using Selenium and waiting was one of the biggest issues I had. In the end, the methods as you described above wouldn't work. I had to resort to doing a simple implicit wait for any dynamic elements, as described below

    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. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

    Code:

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

    src

    Hope that helps.

    0 讨论(0)
  • 2021-01-03 10:56
    public static void clickOn(WebDriver driver, WebElement locator, int timeout)
     {
            new WebDriverWait(driver,timeout).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(locator));
            locator.click();
    
        }
    

    Call the above method in the main method then we will get explicitly wait functionality.

    0 讨论(0)
  • 2021-01-03 10:56

    Your problem is that you passed String to method parameter:

    public String waitForElement(String item) {

    You have to pass your WebElement, something like:

    public boolean visibilityOfElementWait(WebElement webElement) {
        if (webElement != null) {
            try {
                WebDriverWait wait = new WebDriverWait(Driver.getCurrentDriver(), 20);
                wait.until(ExpectedConditions.visibilityOf(wrappedElement));
                highlightElement(webElement);
                return true;
            } catch (Exception e) {
                return false;
            }
        } else
            Logger.logError("PageElement " + webElement.getText() + " not exist");
        return false;
    }
    
    public void highlightElement(WebElement element) {
        if (!Config.getProperty(Config.BROWSER).equalsIgnoreCase("ANDROIDHYBRID")) {
    
            String bg = element.getCssValue("backgroundColor");
    
            for (int i = 0; i < 4; i++) {
                Driver.getDefault()
                        .executeScript("arguments[0].style.backgroundColor = 'red'", element);
                Driver.getDefault()
                        .executeScript("arguments[0].style.backgroundColor = '" + bg + "'", element);
            }
    
       //            String highlightElementScript = "arguments[0].style.backgroundColor = 'red';";
       //            Driver.getDefault().executeScript(highlightElementScript, element);
        }
    }
    
    0 讨论(0)
  • 2021-01-03 11:01

    You can use Explicit wait or Fluent Wait

    Example of Explicit Wait -

    WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
    WebElement aboutMe;
    aboutMe= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));
    

    Example of Fluent Wait -

    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)                            
    .withTimeout(20, TimeUnit.SECONDS)          
    .pollingEvery(5, TimeUnit.SECONDS)          
    .ignoring(NoSuchElementException.class);    
    
      WebElement aboutMe= wait.until(new Function<WebDriver, WebElement>() {       
    public WebElement apply(WebDriver driver) { 
    return driver.findElement(By.id("about_me"));     
     }  
    });
    

    Check this TUTORIAL for more details.

    0 讨论(0)
  • 2021-01-03 11:04

    We can develop implicit wait on our own.

    Use this code; it should also work the same as implicit wait.

    //=== Start of Implicit Wait Statement ===
    
    public void implicit_Wait_ID(String str) throws Exception{
    
            for(int i=0;i<60;i++){
                try{
                    driver.findElement(By.id(str)).isDisplayed();
                    break;
                }catch(Exception e){Thread.sleep(2000);
    
                }   
            }
    }
    //=== End of Implicit Wait Statement ===    
    

    Use this method by passing the ID value:

    public void loginGmail() throws Exception
    {
            driver.findElement(By.id("Email")).sendKeys("Mail ID");
            driver.findElement(By.id("next")).click();
            implicit_Wait_ID("Passwd");
            driver.findElement(By.id("Passwd")).sendKeys("Pwd value");
            driver.findElement(By.id("signIn")).click();
    }
    

    If it is Xpath, LinkText, just create one of the above methods for all locator types and reuse it n number of times in your script.

    0 讨论(0)
提交回复
热议问题