Versions used while doing this blog post, (These are the prerequisites to automate chrome browser. Browser and driver versions should match)
selenium python version – 4.5.0
chrome browser version – 105
chrome driver version – 105 (Download link : https://chromedriver.chromium.org/downloads) [download this to a path in project directory]
Chrome browser can be opened using selenium by following below steps:
1. Instantiate (creation of object) driver object with webdriver.Chrome() (This step opens the chrome browser)
driver = webdriver.Chrome("chromedriver path need to be passed")
2. Now, next steps of maximizing the browser, setting implicit timeout for driver object and launch of expected website can be carried out
driver.implicitly_wait(10) driver.maximize_window() driver.get("https://www.google.com")
3. Now, find an element using find element method. Then use relative_locator and methods on the found element to identify the adjacent elements. Then actions can be triggered on the elements that are identified through relative locators
imfeelingluckybutton = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "form[role='search']>div>div>div[class]:not([style]) input[value*='Feeling Lucky']"))) googlesearchbuttonfoundusingrelativelocator = driver.find_element(locate_with(By.TAG_NAME, "input").to_left_of(imfeelingluckybutton)) googlesearchtextboxfoundusingrelativelocator = driver.find_element(locate_with(By.TAG_NAME, "input").above(imfeelingluckybutton)) googlesearchtextboxfoundusingrelativelocator.send_keys("test") driver.find_element(By.CSS_SELECTOR, "img[alt='Google']").click() WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable(googlesearchbuttonfoundusingrelativelocator)) googlesearchbuttonfoundusingrelativelocator.click()
4. Quit/close the driver object after automating the necessary flows within the website.
driver.quit()
Sample code for reference
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome("chromedriver path need to be passed")
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("https://www.google.com")
imfeelingluckybutton = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "form[role='search']>div>div>div[class]:not([style]) input[value*='Feeling Lucky']")))
googlesearchbuttonfoundusingrelativelocator = driver.find_element(locate_with(By.TAG_NAME, "input").to_left_of(imfeelingluckybutton))
googlesearchtextboxfoundusingrelativelocator = driver.find_element(locate_with(By.TAG_NAME, "input").above(imfeelingluckybutton))
googlesearchtextboxfoundusingrelativelocator.send_keys("test")
driver.find_element(By.CSS_SELECTOR, "img[alt='Google']").click()
WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable(googlesearchbuttonfoundusingrelativelocator))
googlesearchbuttonfoundusingrelativelocator.click()
driver.quit()