Python – Basic Selenium

Install Selenium

pip install selenium

Download Driver

Chrome :
https://sites.google.com/a/chromium.org/chromedriver/downloads

Firefox :
https://github.com/mozilla/geckodriver/releases

Edge :
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

Find Element

SINGLE :

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

MULTIPLE :

find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector

Example

Get Element by & Click it :

from selenium import webdriver
from shutil import which
from selenium.webdriver.common.keys import Keys

chrome_path = which("chromedriver")

driver = webdriver.Chrome(executable_path=chrome_path)
driver.get("https://www.goodreads.com/quotes")

mybooks = driver.find_element_by_link_text("My Books")
mybooks.click()
Fill Input Search & Press Enter :

from selenium import webdriver
from shutil import which
from selenium.webdriver.common.keys import Keys

chrome_path = which("chromedriver")

driver = webdriver.Chrome(executable_path=chrome_path)
driver.get("https://www.goodreads.com/quotes")

input_search = driver.find_element_by_id("explore_search_query")
input_search.send_keys("Albert")
input_search.send_keys(Keys.ENTER)
Fill Input & Press Enter

Notes

driver.close()
# Close Browser Window

Time Sleep :
import time
time.sleep(5) # 5 Seconds

Move Browser's Tab :
driver.switch_to_window(driver.window_handles[0]) 
# [0] is tab no, start from 0

Maximize & Minimize window :
driver.maximize_window()
driver.minimize_window()

Alert :
driver.switch_to_alert.accept()
driver.switch_to_alert.dismiss()

driver.switch_to_alert.send_keys("Fill input")
driver.switch_to_alert.accept()
# Alert with input

Implicitly Wait
# element not found because problem connection 

driver.implicitly_wait(10)
driver.get("website.com")

Explicitly Wait

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

chrome_path = which("chromedriver")

driver = webdriver.Chrome(executable_path=chrome_path)
driver.get("https://www.goodreads.com/quotes")

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

# Wait element by id myDynamicElement for 10 Seconds


Mouse Hover 
from selenium.webdriver.common.action_chains import ActionChains

driver.get("https://demoqa.com/menu")
menu = driver.find_element_by_link_text("Main Item 2")

hover = ActionChains(driver).move_to_element(menu)
hover.perform()


Upload File
from selenium import webdriver
from shutil import which
from selenium.webdriver.common.keys import Keys
import time

chrome_path = which("chromedriver")

driver = webdriver.Chrome(executable_path=chrome_path)
driver.get("https://demoqa.com/upload-download")
time.sleep(10)
driver.find_element_by_id("uploadFile").send_keys("C:/Users/afais/Downloads/favicon.ico")


Upload File With pyautogui

pip install pyautogui

import pyautogui

driver.find_element .. .click()

time.sleep(5)

pyautogui.write(r"C:/Users/afais/Downloads/favicon.ico")
pyautogui.press("enter")


Datepicker 
driver.find_element_by_id('datepicker').send_keys('02/01/2021')

Selected Box 
combo = Select(driver.find_element_by_id('combobox'))
combo.select_by_value('combo_value')

Leave a Reply

Your email address will not be published. Required fields are marked *