Selenium C# ElementNotVisibleException: element not interactable but the element is actually visible

后端 未结 2 478
無奈伤痛
無奈伤痛 2021-01-20 13:36

I\'m using Selenium.WebDriver for C# to ask a question on Quora just typing my question in notepad.

Everything worked fine since I had to post it.

To post it

2条回答
  •  梦谈多话
    2021-01-20 14:13

    You might have a case where the button become displayed(visible) after your check is executed, so you might try following delay in order to ensure that the button is displayed at the time of check:

    public static void WaitForElementToBecomeVisibleWithinTimeout(IWebDriver driver, 
        IWebElement element, int timeout)
    {
        new WebDriverWait(driver,                 
            TimeSpan.FromSeconds(timeout)).Until(ElementIsVisible(element));
    }
    
    private static Func ElementIsVisible(IWebElement element)
    {
        return driver =>
        {
            try
            {
                return element.Displayed;
            }
            catch (Exception)
            {
                // If element is null, stale or if it cannot be located
                return false;
            }
        };
    }
    

    If the button is not visible in the viewport(i.e. need scrolling to become visible) then you may scroll it with

    public static void ScrollElementToBecomeVisible(IWebDriver driver, IWebElement element)
    {
        IJavaScriptExecutor jsExec = (IJavaScriptExecutor)driver;
        jsExec.ExecuteScript("arguments[0].scrollIntoView(true);", element);
    }
    

提交回复
热议问题