首页 > 解决方案 > 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 的类名

标签: selenium

解决方案


选择器没有指向带有按钮类的元素。.button选择器之间和之间有一个空格:first-child。删除空间。给定的选择器正在搜索带有按钮类的标签的子元素。但我假设您正在尝试单击按钮类的第一个元素,而不是按钮类元素的子节点。用这个:

String selector = ".rmcAlertDialog .buttons:first-child";

推荐阅读