简体   繁体   English

C# chromedriver 中的“元素不可交互”异常

[英]'element not interactable' exception in C# chromedriver

I am using selenium chromedriver in c# to try and click a print button however I am receiving an exception of "element not interactable", here is the website source of the print button:我在 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>

What I've tried:我试过的:

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

However none of these worked for me.然而,这些都不适合我。

The website used is bjs.com and the print button and can be found on the Clipped tab.使用的网站是bjs.com和打印按钮,可以在Clipped选项卡上找到。

What am I doing wrong, why is the element not intractable and how could I solve this?我做错了什么,为什么元素不是棘手的,我该如何解决这个问题?

The element has to be visible (that's usually what makes it "interactable") before you can Click() it.在您可以 Click() 之前,该元素必须是可见的(这通常是使其“可交互”的原因)。 Are you opening the "Clipped" tab before you Click()?您是否在 Click() 之前打开“已剪辑”选项卡?

The Xpath you are making is actually causing issue.您正在制作的 Xpath 实际上会导致问题。

All the above Xpaths are not unique and actually point to 2 Xpaths 1 is Print you want to click, and other is not interactable(or hidden) one.以上所有 Xpaths 都不是唯一的,实际上指向 2 个 Xpaths 1 是您要单击的 Print,其他不是可交互(或隐藏)的。 Since it is trying to click on hidden one, it is throwing Error.因为它试图点击隐藏的,所以它会抛出错误。 See image看图片在此处输入图像描述

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

The desired element is an Angular element so to Click() on the element you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies :所需的元素是Angular元素,因此要在元素上Click() ,您必须为ElementToBeClickable()诱导WebDriverWait ,您可以使用以下任一定位器策略

  • CssSelector : CssSelector

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

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

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

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