Complete Selenium Cheatsheet for Free (PYTHON)

Selenium Cheatsheet (PYTHON) for beginners to speed up their learning curve and codes much faster in Selenium Framework.

Selenium Cheat Sheet
Selenium Cheatsheet

Few Question Answers

What is Selenium?

Selenium is an open-source framework for testing web applications.

What is WebDriver in Selenium?

Selenium WebDriver is a tool that used to automate web application testing.

Download Drivers

Chrome Driver

https://chromedriver.chromium.org/downloads

Gecko Driver (Firefox / IE)

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

Install Selenium library

pip install selenium

Import the Selenium library

from selenium import webdriver

Initialize Drivers

Initialize Chrome Driver

chromedriver = "C:/tests/chromedriver.exe"
driver = webdriver.Chrome(executable_path = chromedriver)

Initialize Firefox Driver

geckodriver = "C:/tests/geckodriver.exe"
driver = webdriver.Firefox(executable_path = geckodriver)

Initialize Internet Explorer Driver

iedriver = "C:/tests/IEDriverServer.exe"
driver = webdriver.Firefox(executable_path = iedriver)

Initialize Safari Driver

driver = webdriver.Safari()

Open a url

url = "https://codingtutz.com/"
driver.get(url)

Set window size

driver.set_window_size(1600, 1200)

Change the user agent

chromedriver = 'C:/tests/chromedriver.exe'
options = webdriver.ChromeOptions()
options.add_argument('--user-agent = Googlebot/2.1 (+http://www.google.com/bot.html)')
driver = webdriver.Chrome(executable_path = chromedriver, chrome_options = options)

Simulate webcam and microphone

chromedriver = 'C:/tests/chromedriver.exe'
options = webdriver.ChromeOptions()
options.add_argument("--use-fake-ui-for-media-stream")
options.add_argument("--use-fake-device-for-media-stream")
driver = webdriver.Chrome(executable_path = chromedriver, chrome_options = options)

Find Element

Find element by ID

id = 'loginbtn'
element = driver.find_element_by_id(id)

Find element by Name

name = 'username'
element = driver.find_element_by_name(name)

Find element by Class Name

class_name = 'nav-link'
element = driver.find_element_by_class_name(class_name)

Find element by Tag Name

tag_name = 'nav'
element = driver.find_element_by_tag_name(tag_name)

Find element by Link Text

link_text = 'Contact'
element = driver.find_element_by_link_text(link_text)

Find element by CSS Selector

css_selector = 'button[type="submit"]'
element = driver.find_element_by_css_selector(css_selector)

Find element by XPath

xpath = '//*[@id="block-7"]/form/div/button'
element = driver.find_element_by_xpath(xpath)

Click on an element

....
element.click()

Write text

.....
element.send_keys('[email protected]')

Select an option

Example Html

<select id="country">
<option value="US">United States</option>
<option value="CA">Canada</option>
<option value="IN">India</option>
</select>

Let’s select India. IN

Select by visible text

id = 'country'
element = driver.find_element_by_id(id)
select_element = Select(element)
select_element.select_by_visible_text('India')

Select by value

id = 'country'
element = driver.find_element_by_id(id)
select_element = Select(element)
select_element.select_by_value('IN')

Select by index

id = 'country'
element = driver.find_element_by_id(id)
select_element = Select(element)
select_element.select_by_index(2)

Take a screenshot

driver.save_screenshot('C:/screenshot/codingtutz.png')

Upload File

file_path = 'C:/images/john.png'
id = 'profilepic'
element = driver.find_element_by_id(id)
element.send_keys(file_path)

Execute JavaScript

js_code = 'document.getElementById("logo").src = "https://codingtutz.com/wp-content/uploads/2021/11/logo.png"'
driver.execute_script(js_code)

Manage Tabs

Switch to the next tab

global nextTab
global currentTab
nextTab = currentTab + 1
driver.switch_to_window(driver.window_handles[nextTab])
currentTab = currentTab + 1

Switch to the previous tab

global previousTab
global currentTab
previousTab = currentTab - 1
driver.switch_to_window(driver.window_handles[previousTab])
currentTab = currentTab - 1

Close tab

driver.close()

Refresh tab

driver.refresh()

Hover

from selenium.webdriver.common.action_chains import ActionChains

id = "search"
element = driver.find_element_by_id(id)
hover = ActionChains(driver).move_to_element(element)
hover.perform()

Right Click

from selenium.webdriver.common.action_chains import ActionChains

id = "search"
element = driver.find_element_by_id(id)
right_click = ActionChains(driver).context_click(element)
right_click.perform()

Press Key

....
element.send_keys(Keys.RETURN)

Special Keys in Selenium Python

ADDALTARROW_DOWN
ARROW_LEFTARROW_RIGHTARROW_UP
BACKSPACEBACK_SPACECANCEL
CLEARCOMMANDCONTROL
DECIMALDELETEDIVIDE
DOWNENDENTER
EQUALSESCAPEF1
F10F11F12
F2F3F4
F5F6F7
F8F9HELP
HOMEINSERTLEFT
LEFT_ALTLEFT_CONTROLLEFT_SHIFT
METAMULTIPLYNULL
NUMPAD0NUMPAD1NUMPAD2
NUMPAD3NUMPAD4NUMPAD5
NUMPAD6NUMPAD7NUMPAD8
NUMPAD9PAGE_DOWNPAGE_UP
PAUSERETURNRIGHT
SEMICOLONSEPARATORSHIFT
SPACESUBTRACTTAB
Special Keys in Selenium Python

Get Page Source

page_source = driver.page_source

Get Cookies

cookies_list = driver.get_cookies()
Delete Cookies
Delete Cookies – Selenium Cheatsheet

Delete Cookies

cookie_item = 'shopping_cart'

Delete one cookie

driver.delete_cookie(cookie_item)

Delete all cookies

driver.delete_all_cookies()

Add Chrome Extension

chromedriver = 'C:/tests/chromedriver.exe'
extension_path = 'C:/tests/my_extension.zip'
options = webdriver.ChromeOptions()
options.add_extension(extension_path)
driver = webdriver.Chrome(executable_path = chromedriver, chrome_options = options)

Selenium Official Documentation

https://www.selenium.dev/documentation/

Hope You like this “ Selenium Cheatsheet

Django Complete Cheatsheet [PDF]

Similar Posts

2 Comments

Leave a Reply

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