我只在Chrome浏览器中看到这个。
完整的错误信息如下:
“org.openqa.selenium。WebDriverException:元素在点(411,675)不可点击。其他元素会收到点击:……”
“将接收点击”的元素位于相关元素的一侧,而不是在元素的顶部,也没有重叠,也没有在页面上移动。
我试过加一个偏移量,但也不行。该项目在显示的窗口上,不需要滚动。
我只在Chrome浏览器中看到这个。
完整的错误信息如下:
“org.openqa.selenium。WebDriverException:元素在点(411,675)不可点击。其他元素会收到点击:……”
“将接收点击”的元素位于相关元素的一侧,而不是在元素的顶部,也没有重叠,也没有在页面上移动。
我试过加一个偏移量,但也不行。该项目在显示的窗口上,不需要滚动。
当前回答
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.<Id or anything>));
希望这能有所帮助。
其他回答
有趣的是,我花了这么多时间研究各种各样的答案,没有人尝试过显而易见的答案,当然,我也没有。如果您的页面多次使用相同的id,就像我的页面一样("newButton",),而您想要的id不是第一个找到的,那么您很可能会得到这个错误。最简单的事情(c#):
var testIt = driver.FindElements(By.Id("newButton"));
注意它是FindElements,而不是FindElement。
然后测试从检索中得到了多少结果。如果是第二个,你可以使用:
testit[1].Click();
或者让那些重复使用id的人来修复它们。
出现此错误的原因是您试图单击的元素不在浏览器的视口中(用户可以看到的区域)。因此,解决这个问题的方法是首先滚动到所需的元素,然后执行单击。
Javascript:
async scrollTo (webElement) {
await this.driver.executeScript('arguments[0].scrollIntoView(true)', webElement)
await this.driver.executeScript('window.scrollBy(0,-150)')
}
Java:
public void scrollTo (WebElement e) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeAsyncScript('arguments[0].scrollIntoView(true)', e)
js.executeAsyncScript('window.scrollBy(0,-150)')
}
如果页面中已经加载了jQuery,可以执行以下javascript命令:
"$('#" + element_id + "').click()"
使用python executor的示例:
driver.execute_script("$('#%s').click()" % element_id)
我做了一种蛮力点击,它对我有用。
try:
elem.click()
except:
print "failed to click"
size = elem.size
mid_of_y = int(size["height"])/2
stepts_to_do_to_left = int(size["width"])
while stepts_to_do_to_left > 0:
try:
print stepts_to_do_to_left, mid_of_y
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(elem, mid_of_y, stepts_to_do_to_left)
action.click()
action.perform()
print "DONE CLICK"
break
except:
pass
可能有很多因素会抛出这个错误。 首先,dom可能会在selenium webdriver捕获元素之后发生变化,或者在捕获元素之前元素中使用的所有java脚本都没有加载成功。 要解决这个问题,我们需要使用javascript等待或ajax等待如下。
wait = new WebDriverWait(driver, 30);
wait.until((ExpectedCondition<Boolean>) wd -> ((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete"));
e_driver = new EventFiringWebDriver(driver);
其次,大多数提供软件qa服务的公司的工程师尝试使用java脚本执行器进行单击操作。
如果这也不起作用,那么使用Actions类执行action。