'element not interactable' C# chromedriver 中的异常

'element not interactable' exception in C# chromedriver

我在 C# 中使用 selenium chromedriver 尝试单击打印按钮,但我收到“元素不可交互”的异常,这是打印按钮的网站来源:

<p _ngcontent-c27="" class="print"><span _ngcontent-c27="" class="floatRight">Print</span><img _ngcontent-c27="" class="printImg" src="../../../../../assets/images/Print.svg"></p>

我尝试过的:

driver.FindElementById("clippedTab").Click(); // Successfully clicks on the 'Clipped' tab

//None of the below worked:

driver.FindElementByClassName("print").Click();
// and
driver.FindElementByClassName("printImg").Click();
// and
driver.FindElementByClassName("floatRight").Click();

但是 none 这些对我有用。

使用的网站是 bjs.com 和打印按钮,可以在 Clipped 选项卡上找到。

我哪里做错了,为什么这个元素不是难处理的,我该如何解决这个问题?

元素必须可见(这通常是使它“可交互”的原因),然后您才能对其进行 Click()。您是否在单击 () 之前打开“剪辑”选项卡?

您创建的 Xpath 实际上是导致问题的原因。

以上所有Xpaths都不是唯一的,实际上指向2个Xpaths 1个是你要点击的打印,另一个是不可交互(或隐藏)的。因为它试图点击隐藏的,所以它抛出错误。 见图片

简单的解决方案:- 使用 XPath 作为 :: //*@id="clipped"]/div/div[1]/div[3]/div/p/span

所需的元素是 Angular element so to Click() on the element you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following :

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("img.printImg"))).Click();
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//img[@class='printImg']/h2"))).Click();