c# - 单击不适用于 Selenium 的元素
问题描述
我正在尝试使用 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();
请帮助解决任何其他问题。
解决方案
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());
推荐阅读
- javascript - 没有错误存在=> ReactDOM.render() 方法无处可寻
- reactjs - React Rerender 组件不起作用,无法读取未定义的属性“forceUpdate”
- haskell - 分解 Haskell Stack 构建的阶段
- javascript - 将数组与对象进行比较的问题
- python - 执行不带语句的 Sql 查询
- c++ - 如何从对其中一个项目的引用中获取 std::vector 元素的索引?
- firebase - SwiftUI Firebase 电话身份验证更改显示名称
- makefile - 如何改变这个makefile的结构
- static - 如何设置 HTTPS:Google Cloud Storage 上的站点
- python - 如何旋转 xtick 标签条形图 plotly express?