Error "Other element would receive the click" in Python
Asked Answered
C

4

16

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)
Catechu answered 19/9, 2018 at 7:18 Comment(1)
Do you really want to click paragraph node instead of link? Maybe try x.find_element_by_css_selector(".btn.blue+a")? Also note that parent div node has style="display:none;", so it seem to be hiddenForbis
D
36

Element on which you are trying to click has been covered by some other element so that other element getting the click instead of the actual element.

There may be following possibilities that actual element not getting clicked:

  • Case 1. lets say if its a loader which comes while your 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 within browser dimension and covered by some overlay element.

    Solution: Here you need to scroll to 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.

Dormer answered 19/9, 2018 at 11:53 Comment(1)
Great! Last solution worked for me to "click" in a JS environment... Thx!Cattima
S
12

You may use action class to click your element,

from selenium.webdriver import ActionChains

actions = ActionChains(driver)
actions.move_to_element(element).click().perform()
Sizar answered 19/9, 2018 at 7:33 Comment(0)
L
0

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()
    
Limonene answered 19/9, 2018 at 8:14 Comment(0)
C
0

This is my function for click element on every condition:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

def xpathElement(driver, xpath):
    return WebDriverWait(driver, config['waitSeconds']).until(EC.presence_of_element_located((By.XPATH, xpath)))

def scroll2elementByXpath(driver, xpath):
    element = xpathElement(driver, xpath)
    element.location_once_scrolled_into_view

def clickObject(driver, xpath, firstTime=True, useJS=False):
    try:
        element = xpathElement(driver, xpath)
        if useJS:
            driver.execute_script("arguments[0].click();", element) 
        else:
           element.click()
    except Exception as exception:
        if firstTime:
            scroll2elementByXpath(driver, xpath)
            clickObject(driver, xpath, desc, False)
        elif not useJS:
            clickObject(driver, xpath, desc, firstTime, True)
        else:
            assert False, exception
Chook answered 16/2, 2022 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.