carol
carol

Reputation: 319

Selenium implicitlyWait Not Working?

I am learning Java Maven Selenium. I want something like this in Selenium using 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

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.

Upvotes: 10

Views: 38922

Answers (5)

DecodeMyCode
DecodeMyCode

Reputation: 1

I have made a dedicated video on a super easy workaround for this: Solving Implicit Wait Not Working Problem in Selenium Python (With Easy Steps) | DecodeMyCode

https://youtu.be/mEDn_wMQ2kg

Upvotes: 0

Aasim
Aasim

Reputation: 143

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

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

Upvotes: 0

Bart Van De Slijcke
Bart Van De Slijcke

Reputation: 11

Implement WebDriverWait

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

}

Upvotes: 1

Prashant Shekhar
Prashant Shekhar

Reputation: 11

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.

Upvotes: 1

Paras
Paras

Reputation: 3235

Implicit Wait and Explicit Waits doesn'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.

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

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.

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.

Upvotes: 19

Related Questions