0

Let's say I am trying to find an element named element0,

driver.FindElement(element0).Click;
Thread.Sleep(5000);

Depending on my WiFi speed, element0 could take anywhere from 5000 to 10000 milliseconds to show up.

Having to constantly change the value in Thread.Sleep() would defeat the purpose of automation.

Surrounding it around a try catch block could work:

try
{
   driver.FindElement(element0).Click;
   Thread.Sleep(5000);
} 
catch(org.openqa.selenium.NoSuchElementException e)
{
   driver.FindElement(element0).Click;
   Thread.Sleep(5000);
}

But if element0 is still not present after catching org.openqa.selenium.NoSuchElementException e then it would just throw another one of the same errors.

Is there a better way of telling my code to sleep?

Could I iterate over driver.FindElement(element0).Click in a loop until element0 is present?

LopDev
  • 823
  • 10
  • 26
dam1ne
  • 181
  • 1
  • 2
  • 17

3 Answers3

1

Selenium gas explicit wait for that purpose

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

WebDriverWait wait = new WebDriverWait(WebDriverRefrence, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(element0));
element.click();

This will wait up to 10 seconds for the visibility of the element. You have many more ExpectedConditions you can choose from.

Guy
  • 46,488
  • 10
  • 44
  • 88
0

Also sleep used after findElement will result pause with no sence, since findElemen will use defined timeouts https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html

You may increase implicitlyWait timeout for waiting element.

driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
0

Thread.Sleep()

Pausing Execution with Thread.Sleep() causes the current executing thread to suspend execution for a specified time period. This is an efficient approach of making processor time available to the other threads of the application or other applications that might be running on the same system. However, these sleep times are not guaranteed to be precise, because they are limited by the facilities provided by the underlying . The sleep periods can also be terminated by interrupts. The bottom line is, you cannot assume that invoking sleep will suspend the thread for precisely the time period specified.


implicitlyWait

When using Selenium you can replace the sleep with implicitlyWait. By inducing implicitlyWait the driver instance will poll the DOM Tree until the element has been found for the configured amount of time looking out for the element or elements before throwing a NoSuchElementException.

  • Example:

    • Python:

      driver.implicitly_wait(10)
      
    • Java:

      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
    • DotNet:

      driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
      

ExplicitWait

However, a better approach would be to replace sleep with ExplicitWait which configures the driver instance to wait for a specific condition to be met before proceeding for the next line of code.

  • Example:

    • Python:

      WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "element_css")))
      
    • Java:

      new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element_css")));
      
    • DotNet:

      new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("element_css")))
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352