Selenium Web Automation for Element Selection and Interaction

  • Share this:

Code introduction


This function uses the selenium library to open a webpage, wait for a specific element with a given ID to become clickable, click on that element, and finally close the browser.


Technology Stack : selenium, Chrome WebDriver, WebDriverWait, expected_conditions

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_select_element(arg1, arg2):
    # Initialize the webdriver
    driver = webdriver.Chrome(executable_path=arg1)
    
    # Open a webpage
    driver.get(arg2)
    
    # Wait for a specific element to be clickable and then click on it
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, 'randomElement'))
    )
    
    # Perform an action on the element
    element.click()
    
    # Close the browser
    driver.quit()