ElementClickInterceptedException in Selenium occurs when an attempt to click on an element is intercepted or blocked by another element on the web page. This typically happens when the element you want to click is partially or fully obscured by another element, preventing the click action. Here are different scenarios when an ElementClickInterceptedException can occur, along with coding examples:
Overlaying Elements:
Hidden or Invisible Elements:
Interactive Elements Overlapping:
Dynamic Elements Loading:
Scrolling Required:
Overlaying Elements:
When an element is covered by another element, such as a pop-up or a modal dialog, clicking on the element can result in an ElementClickInterceptedException.
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
overlaying_element = driver.find_element_by_id("overlaying-element")
overlaying_element.click() # Raises ElementClickInterceptedException
except ElementClickInterceptedException as e:
print(f"ElementClickInterceptedException: {e}")
finally:
driver.quit()
Hidden or Invisible Elements:
Elements that are hidden or not visible on the page cannot be clicked and can trigger this exception.
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
hidden_element = driver.find_element_by_id("hidden-element")
hidden_element.click() # Raises ElementClickInterceptedException
except ElementClickInterceptedException as e:
print(f"ElementClickInterceptedException: {e}")
finally:
driver.quit()
Interactive Elements Overlapping:
When interactive elements like buttons or links overlap, attempting to click on one may result in the interception of the click by the overlapping element.
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
button1 = driver.find_element_by_id("button1")
button2 = driver.find_element_by_id("button2")
button1.click() # Clicking button1 might raise ElementClickInterceptedException if it's overlapped by button2
except ElementClickInterceptedException as e:
print(f"ElementClickInterceptedException: {e}")
finally:
driver.quit()
Dynamic Elements Loading:
Elements loaded dynamically via AJAX or JavaScript after the page has loaded may not be immediately clickable.
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "dynamic-element")))
dynamic_element = driver.find_element_by_id("dynamic-element")
dynamic_element.click() # Raises ElementClickInterceptedException if the element is not ready
except ElementClickInterceptedException as e:
print(f"ElementClickInterceptedException: {e}")
finally:
driver.quit()
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
card = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@class="mosaic"]/div[1]/a')))
card.click()
Error
ElementClickInterceptedException Traceback (most recent call last)
Cell In[18], line 7
5 wait = WebDriverWait(driver, 10)
6 card = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@class="mosaic"]/div[1]/a')))
----> 7 card.click()
File ~\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py:93, in WebElement.click(self)
91 def click(self) -> None:
92 """Clicks the element."""
---> 93 self._execute(Command.CLICK_ELEMENT)
solution
increase driver delay and add time.sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
time.sleep(20)
wait = WebDriverWait(driver, 20)
card = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@class="mosaic"]/div[1]/a')))
card.click()
Scrolling Required:
Sometimes, elements that are not in the current viewport require scrolling to become visible and clickable.
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException
driver = webdriver.Chrome()
try:
driver.get("https://example.com/scroll-page")
element_to_scroll_to = driver.find_element_by_id("element-to-scroll-to")
element_to_scroll_to.click() # Raises ElementClickInterceptedException
except ElementClickInterceptedException as e:
print(f"ElementClickInterceptedException: {e}")
finally:
driver.quit()
=================================
my code
my code is from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Click the first element
element1 = driver.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/main/div/div[1]/div/div/div/a[1]').click()
# Wait for the second element to be clickable and then click it
wait = WebDriverWait(driver, 10)
element2 = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div[1]/div[1]/main/div/div[2]/div[1]/div/label[2]/div[2]/span[1]')))
Errors
element2.click() and i got error ElementClickInterceptedException Traceback (most recent call last)
Cell In[23], line 8
6 element2 = driver.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/main/div/div[2]/div[1]/div/label[2]/div[2]/span[1]')
7 driver.execute_script("arguments[0].scrollIntoView();", element2)
----> 8 element2.click()
solution
Use Actions class: If you encounter issues with clicking, you can try using the Actions class to perform a click. Here's an example:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
element1 = driver.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/main/div/div[1]/div/div/div/a[1]').click()
element2 = driver.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/main/div/div[2]/div[1]/div/label[2]/div[2]/span[1]')
# Perform a click using the Actions class
ActionChains(driver).move_to_element(element2).click().perform()
=====================================
Another Solution
Top comments (0)