Selenium C#ElementNotVisibleException:元素不可交互,但该元素实际上是可见的 [英] Selenium C# ElementNotVisibleException: element not interactable but the element is actually visible

查看:74
本文介绍了Selenium C#ElementNotVisibleException:元素不可交互,但该元素实际上是可见的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#的Selenium.WebDriver在Quora上提问,只需在记事本中键入我的问题即可.

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 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:

选择元素并单击它:

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

这样做可以获取元素,但不幸的是,它引发了"ElementNotVisibleException".调试我的应用程序时,我可以看到 Displayed 属性设置为 False ,而实际上没有,因为在ChromeDriver中,我可以清楚地看到按钮.

Doing so I can get the element, but unfortunately it throws "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.

为避免单击元素,我尝试了 IJavaScriptExecutor Driver.ExecuteJavaScript(); 来通过脚本单击链接:

To avoid clicking the element, I tried IJavaScriptExecutor and Driver.ExecuteJavaScript(); to click the link through a script:

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

Driver.ExecuteJavaScript(); 使用了相同的逻辑,但是得到了相同的结果,但是当我在DevTools的控制台"选项卡中写入相同的脚本时,效果很好.

Same logic has been used for Driver.ExecuteJavaScript(); but I get the same result, but when I write the same script into DevTools's Console tab it works perfectly.

我该如何解决这个问题?

How can I solve this issue?

推荐答案

根据您共享的HTML,单击元素为添加问题的元素,因为该元素位于模态对话框,您需要为所需的 ElementToBeClickable 引入 WebDriverWait ,并且可以使用以下任一

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:

  • LinkText :

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

  • CssSelector :

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

  • 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();
    

  • 这篇关于Selenium C#ElementNotVisibleException:元素不可交互,但该元素实际上是可见的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆