我只在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>));

希望这能有所帮助。

其他回答

Re Tony Lâmpada的回答,评论#27确实为我解决了这个问题,除了它提供了Java代码,而我需要Python。下面是一个Python函数,它滚动到元素的位置,然后单击它。

def scroll_to_and_click(xpath):
    element = TestUtil.driver.find_element_by_xpath(xpath)
    TestUtil.driver.execute_script('window.scrollTo(0, ' + str(element.location['y']) + ');')
    element.click()

这解决了我在Chrome 34.0的问题。它在Firefox 28.0和IE 11中没有造成伤害;这些浏览器不受这个问题的影响,但在单击元素之前滚动到元素的位置仍然不是一件坏事。

在Rselenium中,当链接位于顶部窗口边框时,代码多次成功地使用了这种情况。简单的解决方案是使用sendKeysToElement(),如下所示。

if (unlist(webElem$isElementDisplayed())) {
    webElem$sendKeysToElement(list(key = "up_arrow"))  # Add "up arrow"..
    webElem$clickElement()  # ... before $clickElement

我在使用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))))

当我试图单击页面上的单选按钮时,我遇到了同样的异常。我使用下面的Javascript并使用IJavaScriptExecutor执行。 c#示例

string script=" function clickCharity() {"+
"var InputElements = document.getElementsByName('Charity');"+
  "for (i=0; i<InputElements.length; i++){"+
    "if(InputElements[i].getAttribute('value') == 'true')"+
    "{"+
        "InputElements[i].click();"+
    "}"+
"}"+
"}";
var js=WebDriver as IJavaScriptExecutor;
js.ExecuteScript(script);

而不是

webdriver.findElement(By.id("id1")).click();

试着使用

click(By.id("id1"));

void click(final By byLocator) {
    waitFor(new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            WebElement element = driver.findElement(byLocator);
            if (element.isDisplayed()) {
                try {
                    element.click();
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return false;
        }

        @Override
        public String toString() {
            return "Element located " + byLocator + " clicked";
        }
    });
}