'Selenium WebDriver wait (visibilityOfElement) with c#
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. ?
Solution 1:[1]
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.
Solution 2:[2]
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")));
Solution 3:[3]
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)));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prophet |
| Solution 2 | undetected Selenium |
| Solution 3 |
