Selenium .NET(C#) Webdriver too fast (does't wait until element is available)

905 views
Skip to first unread message

ThatDudeOverThere

unread,
Nov 14, 2019, 3:18:42 AM11/14/19
to Selenium Users
I'm currently working on an automated unit test project for a webservice. The main problem is that the selenium webdriver runs too fast and tries to select elements that are not even loaded.
The first adjustmend that I did to my code is addig a ImlicitWait to my main driver.

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

This makes it wait a certain amount of time before finishing the Test Case, but I doesn't retry to select the searched element. The second adjustment that I tried was creating a WebDriverWait Object that uses the Util Method to get a specific element. Apperently this doesn't work too, because if it does'nt find the element it throws an exeption right away.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
var mainElement = wait.Until(obj => obj.FindElement(By.XPath("xPath")));

The only way which I can make my test cases work is with a Thread.Sleep() procedure brefore each element selection.
Are there any other methods that I can use to make selenium wait until a specific element is available?
Im currently using Selenium.WebDriver verison 3.141.0.
The browser that I'm testing with is Chrome at version 78.0.3904.97.
OS: Windows 7 Version 6.1 Build 7601.

Error message:
OpenQA.Selenium.ElementClickInterceptedException : element click intercepted: Element <span role="img" id="sampleButton"  unselectable="on" style="">...</span> is not clickable at point (1632, 576). Other element would receive the click: <div class="x-mask" style="top:0;left:0;" id="ext-gen4305"></div>
 (Session info: chrome=78.0.3904.97)

Oleksiy Kyrylenko

unread,
Nov 14, 2019, 12:30:25 PM11/14/19
to Selenium Users
You can try wait for element to be visible, or write your own method to wait for element to be in DOM
Something like
        public static Func<IWebDriver, bool> EInDOM(IWebElement el)
        {
            return (dr) =>
            {
                try
                {
                    return el != null ? element.El : false;
                }
                catch (Exception ex)
                {
                    return false;
                }
            };
        }

And then invoke it buy
wait.Until(ClassNameWhereYouPlaceTheMethod.EInDOM(element));

Mike Hetzer

unread,
Nov 14, 2019, 12:39:55 PM11/14/19
to Selenium Users
Yeah - I would invoke the ExpectedConditions of the Selenium.Support.UI libs

You can use a method like
                public IWebElement WaitUntil_ElementIsVisible(By by, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.ElementIsVisible(by));
}
to wait for the element to be present.

And then make another to wait for the element to also be clickable.

This is typically way more robust and you won't have to rely on Thread.Sleep (which is frowned upon).

I have a whole class dedicated to these in my framework.

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using ExpectedConditions = SeleniumExtras.WaitHelpers.ExpectedConditions;

namespace SeleniumBase.PageObjects
{
public class CustomWaitHelpers
{
private IWebDriver driver = null;

public CustomWaitHelpers(IWebDriver driver)
{
this.driver = driver;
}

public IAlert WaitUntil_AlertIsPresent(int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.AlertIsPresent());
}
public IWebElement WaitUntil_ElementExists(By by, int timeoutInSeconds = 30)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.ElementExists(by));
}
public IWebElement WaitUntil_ElementIsVisible(By by, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.ElementIsVisible(by));
}
public bool WaitUntil_ElementSelectionStateToBe(By by, bool selected, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.ElementSelectionStateToBe(by, selected));
}
public bool WaitUntil_ElementSelectionStateToBe(IWebElement element, bool expectedState, int timeoutInSeconds = 90)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.ElementSelectionStateToBe(element, expectedState));
}
public IWebElement WaitUntil_ElementToBeClickable(IWebElement element, int timeoutInSeconds = 90)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.ElementToBeClickable(element));
}
public IWebElement WaitUntil_ElementToBeClickable(By by, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.ElementToBeClickable(by));
}
public bool WaitUntil_InvisibilityOfElementLocated(By by, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.InvisibilityOfElementLocated(by));
}
public bool WaitUntil_InvisibilityOfElementWithText(By by, string text, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.InvisibilityOfElementWithText(by, text));
}
public bool WaitUntil_StalenessOf(IWebElement element, int timeoutInSeconds = 90)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.StalenessOf(element));
}
public bool WaitUntil_TextToBePresentInElement(IWebElement element, string str, int timeoutInSeconds = 90)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.TextToBePresentInElement(element, str));
}
}
}






On Thursday, November 14, 2019 at 3:18:42 AM UTC-5, ThatDudeOverThere wrote:

ThatDudeOverThere

unread,
Nov 15, 2019, 9:35:18 AM11/15/19
to Selenium Users
Thank you for your solution. I tried to use the ExpectedConditions from Selenium.Support Library and the selenium test case worked seamlessly.

Abdessalam Aadel

unread,
Nov 18, 2019, 7:33:27 AM11/18/19
to Selenium Users
Hi
      you can try the Explicit Waits
// Satrt Explicit Waits
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("xpath")));
                    


Reply all
Reply to author
Forward
0 new messages