Last modified: Jan 02, 2024 By Alexander Williams

Python Selenium: Element Not Interactable - Handling

Example 1: Retry Mechanism with Explicit Wait


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

try:
    # Use an explicit wait for the element to be clickable
    element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'example-element')))

    # Perform the desired action on the interactable element
    element.click()

except Exception as e:
    print(f"Exception: {str(e)}")
    # Handle the exception, such as retrying the action or logging an error
finally:
    driver.quit()

Output: N/A

Example 2: JavaScript Click


# Continue from the previous example

try:
    # Use JavaScript to click the element even if it's not interactable
    element = driver.find_element(By.ID, 'example-element')
    driver.execute_script("arguments[0].click();", element)

except Exception as e:
    print(f"Exception: {str(e)}")
    # Handle the exception, such as retrying the action or logging an error
finally:
    driver.quit()

Output: N/A

Example 3: Scroll Into View


# Continue from the first example

try:
    # Scroll the element into view before interacting with it
    element = driver.find_element(By.ID, 'example-element')
    driver.execute_script("arguments[0].scrollIntoView(true);", element)
    element.click()

except Exception as e:
    print(f"Exception: {str(e)}")
    # Handle the exception, such as retrying the action or logging an error
finally:
    driver.quit()

Output: N/A