简体   繁体   中英

Selenium WaitForElement in c# throws ElementNotVisibleException

IWebDriver driver = Browser.Instance.Driver;

if (timeout > 0)
{
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
    try
    {
        wait.Until(ExpectedConditions.ElementExists(selector));
        return driver.FindElement(selector);
    }
    catch(NoSuchElementException)
    {
        throw new NoSuchElementException();
    }
}
else
{
    // Search for element without timeout 
    return driver.FindElement(selector);
}

This is my code and i really don't know why it is throwing an Exception here. My timout is set to 30 seconds but he throws the Exception after a couple seconds already. When i run the Test in Debug Modus and wait for the Element myself, it works fine.

At first I used this, but didn't work either

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(drv => drv.FindElement(by));</code>

ExpectedConditions.ElementExists(); is waiting for the element to exists in the DOM . To make sure the element is visible in the website use ExpectedConditions.ElementIsVisible()

wait.Until(ExpectedConditions.ElementIsVisible(selector));

As a side not, if wait.Until condition doesn't met it throws WebDriverTimeoutException , not NoSuchElementException .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM