-1

I've written a script using Selenium with Java. It is working fine sometimes without any kind of exceptions. But sometimes I'm getting TimeOutException as I've used explicit wait. Does this kind of behavior relate to the application? What could be the problem?

ChromeOptions options = new ChromeOptions();
    options.addArguments("incognito");
    WebDriver driver = new ChromeDriver(options);

    driver.get("url");

    WebDriverWait wait = new WebDriverWait(driver, 20);


    wait.until(ExpectedConditions.elementToBeClickable(By.id("usernameid")));
    driver.findElement(By.id("usernameid")).sendKeys("632145");

    wait.until(ExpectedConditions.elementToBeClickable(By.id("passwordid")));
    driver.findElement(By.id("passwordid")).sendKeys("1234");


    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//button[@type='button']")));
    driver.findElement(By.xpath(".//button[@type='button']")).click();

The script is failing sometimes at the button. I'm getting TimeOutException.

learningQA
  • 115
  • 1
  • 9
  • Do you see the target elements loaded when you get `Timeout Exception`? – supputuri May 19 '19 at 04:12
  • yes, I've seen target elements loaded when I get the TimeOutException. – learningQA May 19 '19 at 04:28
  • Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a *specific problem or error and the shortest code necessary* to reproduce it **in the question itself**. Questions without a **clear problem statement** are not useful to other readers. See: [mcve]. – JeffC May 19 '19 at 05:17

2 Answers2

1

Mean while just use the "Waiter" api with lots of wait combinations..

<dependency>
   <groupId>com.imalittletester</groupId>
    <artifactId>thewaiter</artifactId>
    <version>1.0</version>
</dependency>
YaDav MaNish
  • 1,260
  • 2
  • 12
  • 20
0

Here, you are using WebDriverWait for 20 seconds on each element without setting ImplicitWait. If you need to wait for 20 seconds definitely on each element, first set ImplicitWait with more than 20 seconds and then use WebDriverWait.

As a side note, ImplicitWait will be applicable only on findElement and findElements methods.

The default timeout what selenium uses to find element is 0 seconds if we are not setting up ImplicitWait. You can refer more details on this from this url: The default value of timeouts on selenium webdriver

Sreenivasulu
  • 494
  • 2
  • 7
  • I didn't use implicit wait 'coz I think we shouldn't mix implicit and explicit waits which may lead to unexpected behavior. I've learned that using both implicit and explicit waits is not a best practice. – learningQA May 28 '19 at 06:06