Python中出现错误“其他元素将获得点击”

mik*_*ang 4 selenium webdriver python-3.x selenium-webdriver webdriverwait

我试图单击这样的链接:

<div class="loading" style="display:none;">
<p class="btn blue"><span>?????</span></p>
<a href="javascript:void(0);" onclick="get_more();"></a>
</div>
Run Code Online (Sandbox Code Playgroud)

我使用了这段代码:

element = WebDriverWait(driver, 30).until(lambda x: x.find_element_by_css_selector(".btn.blue"))  # @UnusedVariable
element.click()
Run Code Online (Sandbox Code Playgroud)

我收到这样的错误,该怎么解决?

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <p class="btn blue">...</p> is not clickable at point (391, 577). Other element would receive the click: <a href="javascript:void(0);" onclick="get_more();"></a>
(Session info: headless chrome=69.0.3497.100)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.1.7601 SP1 x86_64)
Run Code Online (Sandbox Code Playgroud)

Ish*_*hah 8

您可以使用操作类来单击您的元素,

from selenium.webdriver import ActionChains

actions = ActionChains(driver)
actions.move_to_element(element).click().perform()
Run Code Online (Sandbox Code Playgroud)


Nar*_*raR 5

您尝试单击的元素已被其他元素覆盖,因此该元素获得点击而不是点击实际元素。可能存在以下情况:

  • 案例1。可以说它是一个加载程序,它是在您获取元素并在一段时间后变得不可见时出现的。

    解决方案:在这里,您必须等到加载程序变得不可见,然后必须单击实际元素

    from selenium.webdriver.support import expected_conditions as EC
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.invisibility_of_element_located((By.ID, 'loader_element_id')))
    element_button = wait.until(EC.element_to_be_clickable((By.ID, 'your_button_id')))
    element_button.click()
    
    Run Code Online (Sandbox Code Playgroud)
  • 情况2。使用浏览器尺寸时,实际元素不可见,并被某些叠加元素覆盖。

    解决方案:在这里,您需要滚动到所需的元素,然后必须执行单击

    from selenium.webdriver.common.action_chains import ActionChains
    
    element = driver.find_element_by_id("your_element_id")
    
    actions = ActionChains(driver)
    actions.move_to_element(element).perform()
    
    Run Code Online (Sandbox Code Playgroud)

    或使用可以使用execute_script像:

    driver.execute_script("arguments[0].scrollIntoView();", element)
    
    Run Code Online (Sandbox Code Playgroud)

或使用javascript执行点击

driver.execute_script("arguments[0].click();", element) 
Run Code Online (Sandbox Code Playgroud)

注意:如果需要,请根据Python语法进行必要的更正。

  • 伟大的!最后一个解决方案对我有用,可以在 JS 环境中“单击”......谢谢! (3认同)