Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriverWait how to wait until an item exists or doesn't exist?

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

like image 829
Rochelle C Avatar asked Oct 24 '13 18:10

Rochelle C


People also ask

How do you use wait until an element is visible?

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.

How do you wait for an element to appear in Selenium?

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.

How do you wait until an element is clickable on a website?

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).

How do you wait for invisibility of element in Selenium?

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));


2 Answers

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;
    }
}
like image 199
Richard Avatar answered Oct 27 '22 09:10

Richard


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;
}
like image 25
Faiz Avatar answered Oct 27 '22 11:10

Faiz