selenium - selenium element.click() 不工作(不点击)
问题描述
String selector = ".rmcAlertDialog .buttons :first-child";
RemoteWebElement selection = (RemoteWebElement) driver.findElement(By.cssSelector(selector));
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(selection));
if (element == selection) selection.click();
但是有问题的元素(按钮)没有响应点击。
如果我手动单击该按钮,它会起作用,因此它不是错误的网页,而是自动化。
我已经通过比较它的文本内容来验证按钮是否存在。
更新澄清
此代码适用于(或有效)大多数按钮。该代码来自正在解析的脚本解释器:-
select ".rmcAlertDialog .buttons :first-child" click
此代码在 chrome/selenium/chromedriver 的更新版本之前有效。
该代码现在不适用于某些按钮。
selection.click()
正在调用(在调试器中验证),因为元素将始终等于选择,它只是不起作用。
.buttons
是按钮的容器 div 的类名
解决方案
选择器没有指向带有按钮类的元素。.button
选择器之间和之间有一个空格:first-child
。删除空间。给定的选择器正在搜索带有按钮类的标签的子元素。但我假设您正在尝试单击按钮类的第一个元素,而不是按钮类元素的子节点。用这个:
String selector = ".rmcAlertDialog .buttons:first-child";
推荐阅读
- angular - 如何根据从另一个可观察对象返回的数组中的键创建可观察/ http请求数组
- reactjs - ReactiveSearch 组件和传递状态/道具
- javascript - Node.js 中的网页抓取
- mapbox - Mapbox 地形 RGB 图像'
- sql - Oracle regexp_replace - 删除尾随空格
- reactjs - 无法使用 GraphQL 和 GatsbyJS 查询 ACF 选项页面
- replace - 修改所有列 pyspark 的值
- swift - 如何使用 Swift 和 Firebase 使用 UIAlert 删除当前用户
- mysql - mysql - 浏览已知数据库?
- arrays - 将元素添加到现有数组 Bash