Selenium WebDriver Element Click Function

  • Share this:

Code introduction


This function uses the Selenium library's WebDriver and WebDriverWait to wait for a certain element on the page to load and then click on it.


Technology Stack : Selenium, WebDriver, WebDriverWait, By, EC

Code Type : Function

Code Difficulty : Intermediate


                
                    
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

def random_element_click(arg1, arg2):
    driver = arg1
    element_id = arg2
    
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, element_id))
        )
        element.click()
    except Exception as e:
        print("Error occurred: ", e)