简体   繁体   English

Selenium C# ElementNotVisibleException:元素不可交互但元素实际上是可见的

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

I'm using Selenium.WebDriver for C# to ask a question on Quora just typing my question in notepad.我正在使用 Selenium.WebDriver for C# 在 Quora 上提问,只需在记事本中输入我的问题。

Everything worked fine since I had to post it.一切正常,因为我不得不发布它。

To post it I need to click on a link inside a span like this:要发布它,我需要单击跨度内的链接,如下所示:

<span id="__w2_wEA6apRq1_submit_question">
  <a class="submit_button modal_action" href="#" id="__w2_wEA6apRq1_submit">Add Question</a>
</span>

In order to click it I've tried this method, that I've used for all my previous button clicks:为了点击它,我尝试了这种方法,我以前所有的按钮点击都使用过这种方法:

Selecting the element and clicking it:选择元素并单击它:

var element = driver.FindElement(By.CssSelector(".submit_button.modal_action"));
element.Click();

Doing so I can get the element, but unfortunately it throws "ElementNotVisibleException".这样做我可以获得元素,但不幸的是它抛出“ElementNotVisibleException”。 Debugging my application I could see that Displayed property was set to False , while it wasn't, because in my ChromeDriver I could clearly see the button.调试我的应用程序时,我可以看到Displayed属性设置为False ,而事实并非如此,因为在我的 ChromeDriver 中我可以清楚地看到按钮。

To avoid clicking the element, I tried IJavaScriptExecutor and Driver.ExecuteJavaScript();为了避免点击元素,我尝试了IJavaScriptExecutorDriver.ExecuteJavaScript(); to click the link through a script:通过脚本单击链接:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].click()", element);

Same logic has been used for Driver.ExecuteJavaScript(); Driver.ExecuteJavaScript();使用了相同的逻辑Driver.ExecuteJavaScript(); but I get the same result, but when I write the same script into DevTools's Console tab it works perfectly.但我得到了相同的结果,但是当我将相同的脚本写入 DevTools 的 Console 选项卡时,它运行良好。

How can I solve this issue?我该如何解决这个问题?

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<IWebDriver, bool> 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(ie 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);
}

As per the HTML you have shared to click on the element with text as Add Question as the element is within a Modal Dialog you need to induce WebDriverWait for the desired ElementToBeClickable and you can use either of the following Locator Strategies as solutions:根据您共享的 HTML,单击带有文本作为添加问题的元素,因为该元素位于模态对话框中,您需要为所需的ElementToBeClickable引入WebDriverWait ,您可以使用以下任一定位器策略作为解决方案:

  • LinkText : LinkText

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.LinkText("Add Question"))).Click();
  • CssSelector : CssSelector

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("span[id$='_submit_question']>a.submit_button.modal_action"))).Click();
  • XPath : XPath

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//span[contains(@id,'_submit_question')]/a[@class='submit_button modal_action' and contains(.,'Add Question')]"))).Click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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