Selenium-Debugging:元素在点(X,Y)不可点击

Selenium-Debugging: Element is not clickable at point (X,Y)

我试着用 Selenium 抓取这个 site

我想点击 "Next Page" 按钮,为此我这样做:

 driver.find_element_by_class_name('pagination-r').click()

它适用于很多页面,但不适用于所有页面,我遇到了这个错误

WebDriverException: Message: Element is not clickable at point (918, 13). Other element would receive the click: <div class="linkAuchan"></div>

总是 this page

我读了this question

我试过了

driver.implicitly_wait(10)
el = driver.find_element_by_class_name('pagination-r')
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(el, 918, 13)
action.click()
action.perform()

但我得到了同样的错误

另一个元素覆盖了您要单击的元素。你可以用execute_script()点击这个

element = driver.find_element_by_class_name('pagination-r')
driver.execute_script("arguments[0].click();", element)

使用显式等待而不是隐式等待。

 new WebDriverWait(TestingSession.Browser.WebDriver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists((By.ClassName("pagination-r'")))); 

我有一个类似的问题,使用 ActionChains 没有解决我的错误: WebDriverException:消息:未知错误:元素在点不可点击(5 74、892)

如果你不想使用,我找到了一个很好的解决方案 execute_script:

    from selenium.webdriver.common.keys import Keys #need to send keystrokes

    inputElement = self.driver.find_element_by_name('checkout')

    inputElement.send_keys("\n") #send enter for links, buttons

    inputElement.send_keys(Keys.SPACE) #for checkbox etc

如果您收到 element not clickable 错误,即使在对元素使用等待之后,请尝试以下解决方法之一:

  • 使用Action移动到element的位置,然后在action
  • 上运行perform
WebElement element = driver.findElement(By("element_path"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();`
  • 检查 elementwait 上的叠加层或旋转器是否不可见
By spinnerimg = By.id("spinner ID");
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.invisibilityOfElementLocated(spinnerimg ));

希望这对您有所帮助

我已经编写了逻辑来处理这些类型的异常。

   def find_element_click(self, by, expression, search_window=None, timeout=32, ignore_exception=None,
                       poll_frequency=4):
    """It find the element and click then  handle all type of exception during click

    :param poll_frequency:
    :param by:
    :param expression:
    :param timeout:
    :param ignore_exception:list It is a list of exception which is need to ignore.
    :return:
    """
    if ignore_exception is None:
        ignore_exception = []

    ignore_exception.append(NoSuchElementException)
    if search_window is None:
        search_window = self.driver

    end_time = time.time() + timeout
    while True:
        try:
            web_element = search_window.find_element(by=by, value=expression)
            web_element.click()
            return True
        except tuple(ignore_exception) as e:
            self.logger.debug(str(e))
            if time.time() > end_time:
                self.logger.exception(e)
                time.sleep(poll_frequency)
                break
        except Exception as e:
            raise
    return False

因为元素在浏览器上是不可见的,首先你需要向下滚动到该元素 这可以通过执行 javascript.

来执行
element = driver.find_element_by_class_name('pagination-r')
driver.execute_script("arguments[0].scrollIntoView();", element)
driver.execute_script("arguments[0].click();", element)