10

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.

6
  • read the doc seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits and make conclusion when we need to use it :P
    – drets
    Commented Dec 22, 2015 at 19:07
  • and can you share why you need to wait specific amount of time? it doesn't make sense to me in this particular case.
    – drets
    Commented Dec 22, 2015 at 19:10
  • you are right, it does not make sense using wait here, i just made this a simple example to understand the process easily. :)
    – carol
    Commented Dec 22, 2015 at 19:36
  • nice, welcome to SO, gooood eyes :P
    – drets
    Commented Dec 22, 2015 at 19:36
  • & i was trying to post on my Facebook timeline. if i post a link google.com there, then i will wait 10 seconds to load link thumbnail before clicking Post button. & i think my next question will be how to Click Post button, reaching post button using TAB key is not a good way, it is hard to know how many time i should press TAB key to Focus POST button :P and..... eyes...? Thanks
    – carol
    Commented Dec 22, 2015 at 19:45

5 Answers 5

19

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.

6
  • Thanks, it worked. i heard that Thread.sleep(); is not a good thing, i dont know why it is not good, but it is working for me, thanks again.
    – carol
    Commented Dec 22, 2015 at 19:13
  • But its not a good practice to use Thread.sleep you should always try to use Implicit or Explicit Waits.
    – Paras
    Commented Dec 22, 2015 at 19:14
  • can you tell me plz why it is not good? in my case, i only want to wait few seconds before doing any action, so what side effect can i have with sleep method ? thanks
    – carol
    Commented Dec 22, 2015 at 19:16
  • i am using try { Thread.sleep(20000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }
    – carol
    Commented Dec 22, 2015 at 19:17
  • Thanks a lot Drets, i got it. :)
    – carol
    Commented Dec 22, 2015 at 19:20
1

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.

1

Implement WebDriverWait

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

}
0

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;
}
1
  • The OP is asking about just waiting, not waiting for an Element. Aside from that, this code will fail and throw a TimeoutException if the page takes longer to load than the default implicit wait (I think it's 3 seconds). @Bart Van De Slijcke's answer is the correct way to wait for an Element on a slow page.
    – belwood
    Commented Aug 4, 2019 at 16:24
0

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

2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jun 4 at 18:17
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – kamentk
    Commented Jun 5 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.