Home > OS >  Selenium WebDriver wait (visibilityOfElement) with c#
Selenium WebDriver wait (visibilityOfElement) with c#

Time:02-16

I was using selenium java before. There is such a method in java ;

public static ExpectedCondition<WebElement> visibilityOf(final WebElement element)

I cannot find it in c#. There is such a method in C# ;

public static Func<IWebDriver, IWebElement> ElementIsVisible(By locator)

In this case, I have to give a locater to the method I created every time. However, I want a method where I can directly give the element. How could this be possible. ?

CodePudding user response:

You can use ElementToBeClickable method.
It accepts a IWebElement element as a parameter.
I agree with you that this is not exactly what element visibility means, however element visibility and element clickability ExpectedConditions methods are internally implemented similarly.

CodePudding user response:

The equivalent of Java based line of code:

public static ExpectedCondition<WebElement> visibilityOf(final WebElement element)

in C# would be ElementIsVisible() method which os defined as follows:

public static Func<IWebDriver, IWebElement> ElementIsVisible(
    By locator
)

An example would be:

IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.Id("ElementID")));

CodePudding user response:

I think what you're asking is, how do you use WebDriverWait to wait for an element to become visible. If that's true, you simply need to call the Until method on the WebDriverWait object and pass ExpectedConditions.ElementIsVisible as the argument.

See code below, using a 30 second wait.

Doing it all with one line of code:

IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(ExpectedConditions.ElementIsVisible(By.XPath(id)));

Split up into several lines for clarity:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(id)));
  • Related