我只在Chrome浏览器中看到这个。
完整的错误信息如下:
“org.openqa.selenium。WebDriverException:元素在点(411,675)不可点击。其他元素会收到点击:……”
“将接收点击”的元素位于相关元素的一侧,而不是在元素的顶部,也没有重叠,也没有在页面上移动。
我试过加一个偏移量,但也不行。该项目在显示的窗口上,不需要滚动。
我只在Chrome浏览器中看到这个。
完整的错误信息如下:
“org.openqa.selenium。WebDriverException:元素在点(411,675)不可点击。其他元素会收到点击:……”
“将接收点击”的元素位于相关元素的一侧,而不是在元素的顶部,也没有重叠,也没有在页面上移动。
我试过加一个偏移量,但也不行。该项目在显示的窗口上,不需要滚动。
当前回答
我在使用clj-webdriver (Selenium的clojure端口)时也遇到了同样的问题。为了方便起见,我只是将前面的解决方案翻译为clojure。你可以在点击之前调用这个函数来避免这个问题。
(defn scrollTo
"Scrolls to the position of the given css selector if found"
[q]
(if (exists? q)
(let [ loc (location-once-visible q) jscript (str "window.scrollTo(" (:x loc) "," (:y loc) ")") ]
(execute-script jscript))))
其他回答
这是由以下3种类型引起的:
1.单击该元素是不可见的。
使用Actions或JavascriptExecutor让它点击。
行动:
WebElement element = driver.findElement(By("element_path"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
由JavascriptExecutor:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(250, 0)"); // if the element is on top.
jse.executeScript("scroll(0, 250)"); // if the element is on bottom.
or
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView()", Webelement);
然后单击元素。
2.在单击元素之前,页面会被刷新。
为此,让页面等待几秒钟。
3.元素是可点击的,但有一个旋转/覆盖在它的顶部
下面的代码将等待覆盖消失
By loadingImage = By.id("loading image ID");
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.invisibilityOfElementLocated(loadingImage));
然后单击元素。
在测试了所有提到的建议后,没有一个奏效。我写了这个代码。它有效,但并不漂亮
public void click(WebElement element) {
//https://code.google.com/p/selenium/issues/detail?id=2766 (fix)
while(true){
try{
element.click();
break;
}catch (Throwable e){
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
public void click(String css) {
//https://code.google.com/p/selenium/issues/detail?id=2766 (fix)
while(true){
try{
driver.findElement(By.cssSelector(css)).click();
break;
}catch (Throwable e){
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
在Drupal中使用Selenium时:
// Get element.
$element = $this->driver->getElement('xpath=//input');
// Get screen location.
$location = $element->getLocation();
// To make sure that Chrome correctly handles clicking on the elements
// outside of the screen, we must move the cursor to the element's location.
$this->driver->moveCursor($location['x'], $location['y']);
// Optionally, set some sleep time (0.5 sec in the example below) if your
// elements are visible after some animation.
time_nanosleep(0, 500000000);
// Click on the element.
$element->click();
你也可以使用JavaScript点击和滚动将不需要那么。
IJavaScriptExecutor ex = (IJavaScriptExecutor)Driver;
ex.ExecuteScript("arguments[0].click();", elementToClick);
我也面临着同样的问题,我使用了延迟,这在我这边很有效。Page正在刷新和获取其他定位器。
Thread.sleep(2000);
Click(By.xpath("//*[@id='instructions_container']/a"));