Selenium search functions act faster than page loading

Asked

Viewed 41 times

0

I automatized so that they were inserted email and password, and then clicked on "Login", until then your well, but the button that should be clicked after the login has not finished loading and Selenium accuses the following error: Openqa.Selenium.Nosuchelementexception.

I’m trying with Implicit and Explicit Wait but I’m not able to "force" this wait.

Details: I need to perform this wait without the command Thread.Sleep(MILISEGUNDOS);

I’m conducting the search for Xpath because the element does not have Id

Here’s the search model I’m currently using.

var btnSystem = new WebDriverWait(driver, TimeSpan.FromSeconds(15)).Until(drv => drv.FindElement(By.Xpath("/html/body/div/div/div[2]/div/div[2]/div/div[2]/div/a[8]/div")));
btnSystem.Click();

1 answer

0

I’ve had this problem recently and solved it in a few ways, see if any help:

Using a timeout when instantiating the driver:

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

Using the functions below (you will need to install the Dotnetseleniumextras.Waithelpers package):

  1. Expectation that the element is visible and enabled to click

     public static IWebElement WaitUntilElementClickable(IWebDriver driver, By elementLocator, int timeout = 30)
     {
         try
         {
             var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
             return wait.Until(ExpectedConditions.ElementToBeClickable(elementLocator));
         }
         catch (NoSuchElementException)
         {
             Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
             throw;
         }
     }
    
  2. Expectation that the element is present in the DOM and visible (element is displayed and has height and width greater than zero)

     public static IWebElement WaitUntilElementVisible(IWebDriver driver, By elementLocator, int timeout = 30)
     {
         try
         {
             var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
             return wait.Until(ExpectedConditions.ElementIsVisible(elementLocator));
         }
         catch (NoSuchElementException)
         {
             Console.WriteLine("Element with locator: '" + elementLocator + "' was not found.");
             throw;
         }
     }
    
  3. Expectation that the element is present in the page DOM, without necessarily being visible

     public static IWebElement WaitUntilElementExists(IWebDriver driver, By elementLocator, int timeout = 30)
     {
         try
         {
             var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
             return wait.Until(ExpectedConditions.ElementExists(elementLocator));
         }
         catch (NoSuchElementException)
         {
             Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
             throw;
         }
     }
    
  • Thank you, I used the second form you suggested and it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.