'Error "Other element would receive the click" in Python
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)
Solution 1:[1]
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_scriptlike :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.
Solution 2:[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()
Solution 3:[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()
Solution 4:[4]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ishita Shah |
| Solution 3 | |
| Solution 4 | Ivy Chiu |
