Javascript Selenium - 元素无法点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29508143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium - Element is not clickable at point
提问by haeminish
I am using selenium for test script. I am getting following error and this error randomly occur. When I run 10 times, I get this about twice. So it's not really reproducible. Does anyone know why this is happening? the element I am trying to click is definitely visible in the browser and doesn't move around so there is no need to resize or drag element. I am using chrome webdriver and I read other troubleshooting strategies(Debugging "Element is not clickable at point" error) and they don't seem relevant to my issue. I waited enough time as well.
我正在使用硒作为测试脚本。我收到以下错误并且此错误随机发生。当我跑 10 次时,我得到了大约两次。所以它不是真的可以重现。有谁知道为什么会这样?我尝试单击的元素在浏览器中绝对可见并且不会四处移动,因此无需调整大小或拖动元素。我正在使用 chrome webdriver 并且我阅读了其他故障排除策略(调试“元素在点上不可点击”错误),它们似乎与我的问题无关。我也等了足够的时间。
UnknownError: unknown error: Element is not clickable at point (167, 403). Other element would receive the click: <div class="leftMasterBackground"></div>
回答by Johnny
There are a number of steps you can do in order to improve the stability while clicking on different UI elements:
您可以执行许多步骤来提高单击不同 UI 元素时的稳定性:
- Explicitlywait for it's presencein the DOM
- Scrollinto the element view
- Check if clickable
- 显式等待它出现在 DOM 中
- 滚动到元素视图
- 检查是否可点击
Does it helped the stability?
对稳定性有帮助吗?
WebDriverWait wait = new WebDriverWait(driver, 3)
JavascriptExecutor js = ((JavascriptExecutor) driver)
//presence in DOM
wait.until(ExpectedConditions.presenceOfElement(By.id("ID")));
//scrolling
WebElement element = driver.findElement(By.id("ID")));
js.executeScript("arguments[0].scrollIntoView(true);", element);
//clickable
wait.until(ExpectedConditions.elementToBeClickable(By.id("ID")));
Further, if you will decide to override the default Actionsinterface with more customized one, you can use two type of clicks (for example): click()which will have all those stability steps and fastClick()which will be the default clicking without any varification.
此外,如果您决定使用更自定义的操作界面覆盖默认操作界面,您可以使用两种类型的点击(例如):click()一种将具有所有这些稳定性步骤,fastClick()另一种将是没有任何修改的默认点击。
回答by billi90
I have solved by catching the exception and managing it like this:
我已经通过捕获异常并像这样管理它来解决:
WebDriver driver = new ChromeDriver();
WebElement element = driver.findElement(By.id("ID"));
boolean clicked = false;
do{
try {
element.click();
} catch (WebDriverException e) {
continue;
} finally {
clicked = true;
}
} while (!clicked);
回答by user6226795
I was also facing same problem with Chrome. I have solved that putting one line of code before clicking on the element:
Chrome 也遇到了同样的问题。我已经解决了在单击元素之前放置一行代码的问题:
scrollToViewElement(driver,xpath);
回答by Siddiqui Arshad
for best solution , use java script to focus element Using ----> JavascriptExecutor jsnew=(JavascriptExecutor) driver; WebElement element=driver.findElement(By.xpath("")); jsnew.executeScript("arguments[0].scrollIntoView({block:\"center\"});", element);
为了获得最佳解决方案,请使用 java 脚本来聚焦元素 Using ----> JavascriptExecutor jsnew=(JavascriptExecutor) 驱动程序;WebElement element=driver.findElement(By.xpath("")); jsnew.executeScript("arguments[0].scrollIntoView({block:\"center\"});", element);
In place of xpath you can use id , css selector : This scrollIntoView will bring the this specific element in middle of page , themn driver wil be able to hit element.
您可以使用 id , css 选择器代替 xpath :此 scrollIntoView 将在页面中间带来此特定元素,他们的驱动程序将能够点击元素。
if it is normal button or link , use jsnew.executeScript("arguments[0].click();",element);
如果是普通按钮或链接,使用 jsnew.executeScript("arguments[0].click();",element);
This is consistent solution for click.
这是点击的一致解决方案。
回答by Balu
click parent element of the element which you want to click. this can be workaround solution only.
单击要单击的元素的父元素。这只能是变通解决方案。
回答by Manan Trivedi
- This happens only on chrome so it works on ie and firefox
- ChromeDriver always clicks the middle of the element
- The reason Chrome driver doesn't calculate the correct screen location of link.
- 这只发生在 chrome 上,所以它适用于 ie 和 firefox
- ChromeDriver 总是点击元素的中间
- Chrome 驱动程序不计算链接的正确屏幕位置的原因。
Solution:
解决方案:
// Find an element and define it
WebElement elementToClick = driver.findElement(By.xpath("some xpath"));
// Scroll the browser to the element's Y position
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().y+")");
// Click the element
elementToClick.click();
回答by dimyo
I got the same issue due to one of spinner was hiding the element.
由于其中一个微调器隐藏了元素,我遇到了同样的问题。
I gave xpath and it resolved the issue. Other people suggested to 1. scroll 2. sleep also worked for them.
我给了 xpath,它解决了这个问题。其他人建议 1. 滚动 2. 睡眠也对他们有用。

