I am running a test using Selenium WebDriver and if a user doesn't have access rights a div does not exist on the page. I am trying to do a wait so that if the item is displayed it returns true but if it hits the timeout it returns false.
public bool SummaryDisplayed()
{
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(5));
var myElement = wait.Until(x => x.FindElement(By.Id("summaryPage")));
return myElement.Displayed;
}
I don't want to use Thread.Sleep because if the element is there after 2 seconds I want it to proceed. But if the element isn't there after 5 seconds it should return false. I don't want it to throw an exception, in some test cases I am expecting it to not exist. Is there a way I can suppress the exception and return false after the timeout? Thanks
Selenium: Waiting Until the Element Is Visible var wait = new WebDriverWait(driver, TimeSpan. FromSeconds(20)); As you can see, we give the WebDriverWait object two parameters: the driver itself and a TimeSpan object that represents the timeout for trying to locate the element.
We can wait until an element is present in Selenium webdriver. This can be done with the help of synchronization concept. We have an explicit wait condition where we can pause or wait for an element before proceeding to the next step. The explicit wait waits for a specific amount of time before throwing an exception.
Wait until the element's . Displayed property is true (which is essentially what visibilityOfElementLocated is checking for). Wait until the element's . Enabled property is true (which is essentially what the elementToBeClickable is checking for).
Wait for invisibility of element with Text Below is the syntax which is used for checking that an element with text is either invisible or not present on the DOM. WebDriverWait wait = new WebDriverWait(driver, waitTime); wait. until(ExpectedConditions. invisibilityOfElementWithText(by, strText));
This will work for you, I think.
public bool SummaryDisplayed()
{
try
{
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(5));
var myElement = wait.Until(x => x.FindElement(By.Id("summaryPage")));
return myElement.Displayed;
}
catch
{
return false;
}
}
You can use a generic extension method to wait for an element to be visible within a specified wait; and then just call it from your methods as below:
public bool SummaryDisplayed()
{
var summaryElement = driver.WaitGetElement(By.Id("summaryPage"), 5, true);
return (summaryElement !=null);
}
The extension method simply wraps the WebDriverWait
and waits for the element to exist/be displayed, depending on what you asked for. If element is not found within the specified wait, it returns null
. I use this extension instead of FindElement()
in my frameworks.
public static IWebElement WaitGetElement(this IWebDriver driver, By by, int timeoutInSeconds, bool checkIsVisible=false)
{
IWebElement element;
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
try
{
if (checkIsVisible)
{
element = wait.Until(ExpectedConditions.ElementIsVisible(by));
}
else
{
element = wait.Until(ExpectedConditions.ElementExists(by));
}
}
catch (NoSuchElementException) { element = null; }
catch (WebDriverTimeoutException) { element = null; }
catch (TimeoutException) { element = null; }
return element;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With