【问题标题】:Click on element not working with Selenium单击不适用于 Selenium 的元素
【发布时间】:2021-05-07 07:58:07
【问题描述】:

我正在尝试使用 Selenium 单击一个元素。这是一个带有 HTML 代码的复选框:

包含复选框和条款的段落代码:

<p class="jss72 jss80 jss927 jss943 jss930">
    <span class="js122">
        <img src="/images/purple.svg">
    </span>
    <span class="jss941">
        I Agree To The Terms
    </span>
</p>

我尝试了三种不同的方法,但都没有奏效:

//Tried with xpath clicking on image: Error: Element not interactable   
driver.FindElement(By.XPath("//img[@src='/images/purple.svg']")).Click();

//Tried with Xpath by selecting the span 
driver.FindElement(By.XPath("//span[@class='js122')]")).Click();

//Tried with CssSelector Error: Element not interactable  
driver.FindElement(By.CssSelector("img[src*='purple.svg']")).Click();

请帮助解决任何其他问题。

【问题讨论】:

  • 有时需要在点击事件onmouseover之前完成
  • 这是一个复选框,onmouseover 的解决方法是什么。请帮忙
  • 这里是java,不过能看懂和for c#stackoverflow.com/questions/17293914/…
  • 您能提供网站或更多 HTML 代码吗?并把错误日志也放进去,你得到“没有这样的元素”或过时的元素exption,还是别的什么?
  • 是的,我收到错误“元素不可交互”。我无法使用 span 类作为其动态。我已经编辑了问题,请检查

标签: c# selenium


【解决方案1】:

ElementNotInteractableException 的发生是由于 Selenium 执行代码和浏览器在页面上执行 JavaScript 之间的竞争条件。您要单击的元素存在于天桥中。我敢打赌,天桥出现时会出现淡入动画。有一个短暂的时间可以看到复选框,Selenium 可以在 DOM 中找到它,但 Selenium 无法点击它。解决方案非常简单。使用显式等待:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

// The 'd' parameter to the lambda expression is an IWebDriver object
wait.Until(d => d.FindElement(By.XPath("//img[@src='/images/purple.svg']")).Click());

【讨论】:

  • 仍然是同样的错误“元素不可交互”,即使是代码。
【解决方案2】:

在尝试找到您需要切换该框架或弹出窗口的元素之前。

这样做;

driver.switchTo().frame(<yourFrameIndex>);

driver.switchTo().activeElement();

【讨论】:

  • 我可以填写整个表格,除了复选框....
【解决方案3】:

我在您分享的 html 中没有看到复选框:

<p class="jss72 jss80 jss927 jss943 jss930">
    <span class="js122">
        <img src="/images/purple.svg">
    </span>
    <span class="jss941">
        I Agree To The Terms
    </span>
</p>

我希望看到类似的东西

<input type='checkbox'/>

而不是复选框旁边的文本段落。

【讨论】:

    【解决方案4】:

    我在这里尝试了所有答案,但没有运气。我自己找到了解决方案。我尝试了DOM方法。

    IWebElement we = driver.FindElement(By.CssSelector("img[src*='purple.svg']"));
    
    IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
    js.ExecuteScript("arguments[0].click();", we);

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 2012-04-19
      • 1970-01-01
      • 2020-07-09
      相关资源
      最近更新 更多