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

par*_*rik 42 python selenium web-scraping selenium-firefoxdriver selenium-webdriver

我试图通过Selenium 刮掉这个网站.

我想点击"下一页"按钮,为此我这样做:

 driver.find_element_by_class_name('pagination-r').click()
Run Code Online (Sandbox Code Playgroud)

它适用于许多页面但不适用于所有页面,我收到此错误

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

总是为这个页面

我读了这个问题

我试过这个

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()
Run Code Online (Sandbox Code Playgroud)

但我得到了同样的错误

Rem*_*coW 90

另一个元素是覆盖要单击的元素.您可以execute_script()用来点击它.

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

  • @chandresh`execute_script()`方法有2个参数.第一个是脚本,第二个是vararg,您可以在其中放置脚本中使用的任何参数.在这种情况下,我们只需要元素作为参数,但由于它是一个vararg,因此我们的元素是集合中的第一个元素.例如你也可以做`driver.execute_script("arguments [0] .click(); arguments [1] .click();"element1,element2)`这会点击两个传递的元素 (9认同)
  • 请记住,如果您正在编写打算像真实用户一样使用网站的测试,那么您可能会做一些真实用户无法做的事情,因为他们想要单击的元素被覆盖了。不要这样做只是为了让你的测试通过! (3认同)
  • @RemcoW这里的“ arguments [0]”是什么意思? (2认同)

Dee*_*rud 13

我有一个类似的问题,使用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
Run Code Online (Sandbox Code Playgroud)

要么

    inputElement.send_keys(Keys.SPACE) #for checkbox etc
Run Code Online (Sandbox Code Playgroud)


小智 7

由于元素在浏览器上不可见,首先您需要向下滚动到可以通过执行 javascript 执行的元素。

element = driver.find_element_by_class_name('pagination-r')
driver.execute_script("arguments[0].scrollIntoView();", element)
driver.execute_script("arguments[0].click();", element)
Run Code Online (Sandbox Code Playgroud)

  • 当前接受的答案中严重缺少“arguments[0].scrollIntoView();”。这非常有效。 (7认同)