Error “Other element would receive the click” in Python

孤者浪人 提交于 2019-12-06 05:00:47

问题


I tried to click a link like this:

<div class="loading" style="display:none;">
<p class="btn blue"><span>さらに表示</span></p>
<a href="javascript:void(0);" onclick="get_more();"></a>
</div>

and I used this code:

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

I got an error like this, what can I do to solve it?

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)

回答1:


Element on which you are trying to click has been covered by some other element so that element getting the click instead of clicking on actual element.There can be following cases :

  • Case 1. lets say it a loader which comes while you element getting load and get invisible after some time.

    Solution: Here you have to wait until the loader get invisible and then have to perform click on actual element

    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()
    
  • Case 2. actual element is not visible withing browser dimension and covered by some overlay element.

    Solution: Here you need to scroll to the the required element and then have to perform the click

    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()
    

    OR use can use execute_script like :

    driver.execute_script("arguments[0].scrollIntoView();", element)
    

OR perform the click using javascript

driver.execute_script("arguments[0].click();", element) 

Note: Please make necessary correction as per Python syntax if require.




回答2:


You may use action class to click your element,

from selenium.webdriver import ActionChains

actions = ActionChains(driver)
actions.move_to_element(element).click().perform()



回答3:


As per the HTML and your code trials you have attempted to click on the <span> tag in stead you should try to invoke click() on the <a> tag as follows:

  • Using css_selector:

    element = WebDriverWait(driver, 30).until(lambda x: x.find_element_by_css_selector("div.loading a[onclick^='get_more']"))
    element.click()
    
  • Using xpath:

    element = WebDriverWait(driver, 30).until(lambda x: x.find_element_by_xpath("//p[@class='btn blue']/span[contains(.,'さらに表示')]//following::a[contains(@onclick,'get_more')]"))
    element.click()
    


来源:https://stackoverflow.com/questions/52400233/error-other-element-would-receive-the-click-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!