100% found this document useful (2 votes)
2K views

Selenium - Python - Cheat Sheet

This document provides a cheat sheet for finding and interacting with elements using Selenium and Python. It outlines 19 different actions including how to find elements by ID, name, class, tag name, link text, partial link text, CSS selector, XPath, and how to click elements, write text, select options, take screenshots, upload files, switch frames and tabs, hover over elements, right click, and click at offsets.

Uploaded by

josevelas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
2K views

Selenium - Python - Cheat Sheet

This document provides a cheat sheet for finding and interacting with elements using Selenium and Python. It outlines 19 different actions including how to find elements by ID, name, class, tag name, link text, partial link text, CSS selector, XPath, and how to click elements, write text, select options, take screenshots, upload files, switch frames and tabs, hover over elements, right click, and click at offsets.

Uploaded by

josevelas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

https://dev.

to/razgandeanu/selenium-cheat-sheet-9lc the_id = 'register' //*[@attribute = "attribute_value"]


element = driver.find_element_by_id(the_id)
Selenium | Python | Cheat Sheet For our element, a custom XPath would be:
Find element by Name //a[@href = "/sign-up"]
1. Import the Selenium library the_name = 'register'
element = driver.find_element_by_id(the_name) You can read more about that here.
You can get Selenium from here.
the_xpath = '//a[@href = "/sign-up"]'
from selenium import webdriver Find element by Class Name element = driver.find_element_by_xpath(the_xpath)
the_class_name = 'nav-link'
2. Start the webdriver and the browser element =
driver.find_element_by_class_name(the_class_name) 5. Click on an element
the_id = 'register'
Starting the webdriver and the Chrome browser. Find element by Tag Name element = driver.find_element_by_id(the_id)
element.click()
You can get ChromeDriver from here. the_tag_name = 'a'
chromedriver = "C:/tests/chromedriver.exe" element =
driver = webdriver.Chrome(executable_path = driver.find_element_by_tag_name(the_tag_name) 6. Write text inside an element
chromedriver)
Find element by Link Text Works only for inputs and textareas.
the_id = 'email'
Starting the webdriver and the Firefox browser. Works only for anchor elements. the_email = '[email protected]'
You can get GeckoDriver from here. the_link_text = 'Sign Up' element = driver.find_element_by_id(the_id)
geckodriver = "C:/tests/geckodriver.exe" element = element.send_keys(the_email)
driver = webdriver.Firefox(executable_path = driver.find_element_by_link_text(the_link_text)
geckodriver)
7. Select an option
Starting the webdriver and the Internet Explorer browser. Find element by Partial Link Text Works only for select elements.
You can get IEDriverServer from here. Works only for anchor elements. <select id="country">
iedriver = "C:/tests/IEDriverServer.exe" <option value="US">United States</option>
the_partial_link_text = 'Sign'
driver = webdriver.Firefox(executable_path = <option value="CA">Canada</option>
element =
iedriver) <option value="MX">Mexico</option>
driver.find_element_by_partial_link_text(the_part
</select>
ial_link_text)

🇨🇦
Starting the webdriver and the Safari browser.
Nothing to download. The SafariDriver is integrated in Safari. Find element by CSS Selector Let's select Canada.
driver = webdriver.Safari() You can extract the CSS Selector from the browser.
Or you can write your own by using an attribute from the element: You can use the visible text:
*[attribute="attribute_value"] the_id = 'country'
3. Open a website
For our element, a custom CSS Selector would be: element = driver.find_element_by_id(the_id)
the_url = "https://example.com" a[href="/sign-up"] select_element = Select(element)
driver.get(the_url) select_element.select_by_visible_text('Canada')
the_css_selector = 'a[href="/sign-up"]'
element = You can use the value:
4. Find an element
driver.find_element_by_css_selector(the_css_selec the_id = 'country'
Let's try to find this element: tor) element = driver.find_element_by_id(the_id)
<a href="/sign-up" id="register" name="register" select_element = Select(element)
class="cta nav-link">Sign Up</a> Find element by XPath select_element.select_by_value('CA')
You can extract the XPath from the browser.
Find element by ID You can also use the index:
Or you can write your own by using an attribute from the element:
the_id = 'country' the_iframe_id = 'payment_section' the_id = "register"
element = driver.find_element_by_id(the_id) the_element_id = 'card_number' the_element = driver.find_element_by_id(the_id)
select_element = Select(element) the_iframe = hover =
select_element.select_by_index(1) driver.find_element_by_id(the_iframe_id) ActionChains(driver).move_to_element(the_element)
driver.switch_to.frame(the_iframe) hover.perform()
element =
8. Take a screenshot driver.find_element_by_id(the_element_id)
element.send_keys('41111111111111') 18. Right Click
the_path = 'C:/tests/screenshots/1.png'
driver.save_screenshot(the_path) driver.switch_to.default_content() the_id = "register"
Selenium does not offer Screenshot Comparison but we know Endtest also supports iframes and it even supports Shadow DOM. the_element = driver.find_element_by_id(the_id)
who does. right_click =
ActionChains(driver).context_click(the_element)
12. Switch to the next tab right_click.perform()
9. Upload a file You have to store the handle of your current tab in a global
This works by using the send_keys method to write the local path variable. 19. Click with offset
of the file in the input type="file" element. If you have only one tab open, the handle is 0.
global nextTab In order to precisely click on a certain position in a canvas
Let's use this example: element, you have to provide the offset.
global currentTab
<input type="file" multiple="" nextTab = currentTab + 1 The offset represents the number of pixels to the right and down,
id="upload_button"> driver.switch_to_window(driver.window_handles[nex starting from the top left corner of your canvas element.
tTab]) the_id = "register"
the_file_path = 'C:/tests/files/example.pdf' currentTab = currentTab + 1
the_id = 'upload_button' the_element = driver.find_element_by_id(the_id)
element = driver.find_element_by_id(the_id) x = 30
element.send_keys(the_file_path) y = 20
13. Switch to the previous tab
You can read more about uploading files in a test here. offset =
global previousTab ActionChains(driver).move_to_element_with_offset(
global currentTab the_element,x,y)
10. Execute JavaScript previousTab = currentTab - 1 offset.click()
driver.switch_to_window(driver.window_handles[pre offset.perform()
In some cases, you might need to execute some JavaScript code. viousTab]) You can read how to do this with Endtest here.
This works exactly like you would execute it in your browser currentTab = currentTab - 1
console.
js_code = 20. Press Key
14. Close tab
'document.getElementById("pop-up").remove()' the_id = 'register'
driver = execute_script(js_code) driver.close() element = driver.find_element_by_id(the_id)
element.send_keys(Keys.RETURN)
11. Switch to iframe 15. Close alert
<iframe id="payment_section"> driver.switch_to.alert.accept() 21. Drag and drop
<input id="card_number"> element_to_drag_id = 'ball'
<input id="card_name"> target_element_id = 'goal'
<input id="expiration_date"> 16. Refresh element_to_drag =
<input id="cvv"> driver.refresh() driver.find_element_by_id(element_to_drag_id)
</iframe> target_element =
driver.find_element_by_id(target_element_id)
17. Hover ActionChains(driver).drag_and_drop(element_to_dra
g_id, target_element).perform()

22. Get Page Source


the_page_source = driver.page_source chromedriver = 'C:/tests/chromedriver.exe'
options = webdriver.ChromeOptions()
options.add_argument("--use-fake-ui-for-media-str
23. Get Cookies eam")
cookies_list = driver.get_cookies() options.add_argument("--use-fake-device-for-media
-stream")
driver = webdriver.Chrome(
24. Delete Cookies executable_path = chromedriver,
chrome_options = options)
cookie_item = 'shopping_cart'
# delete one cookie
driver.delete_cookie(cookie_item) 31. Add Chrome Extension
# delete all cookies
driver.delete_all_cookies() chromedriver = 'C:/tests/chromedriver.exe'
extension_path = 'C:/tests/my_extension.zip'
options = webdriver.ChromeOptions()
25. Get first element from list options.add_extension(extension_path)
driver = webdriver.Chrome(
the_id = 'register' executable_path = chromedriver,
list_of_elements = chrome_options = options)
driver.find_elements_by_id(the_id)
first_element = list_of_elements[0]
32. Emulate mobile device
26. Configure Page Load Timeout google_pixel_3_xl_user_agent = 'Mozilla/5.0
(Linux; Android 9.0; Pixel 3 XL
driver.set_page_load_timeout(20) Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/61.0.3163.98 Mobile
Safari/537.36'
27. Configure Element Load Timeout pixel_3_xl_emulation = {
from selenium.webdriver.support.ui import "deviceMetrics": {
WebDriverWait "width": 411,
the_id = 'register' "height": 731,
WebDriverWait(driver,10).until(EC.presence_of_ele "pixelRatio": 3
ment_located((By.ID, the_id))) },
"userAgent": google_pixel_3_xl_user_agent
}
28. Set window size options = webdriver.ChromeOptions()
options.add_experimental_option("mobileEmulation"
driver.set_window_size(1600, 1200)
, pixel_3_xl_emulation)
driver = webdriver.Chrome(
29. Change the user agent string executable_path = chromedriver,
chrome_options = options)
the_user_agent = 'hello'
chromedriver = 'C:/tests/chromedriver.exe'
options = webdriver.ChromeOptions()
options.add_argument('--user-agent = '+
the_user_agent)
driver = webdriver.Chrome(
executable_path = chromedriver,
chrome_options = options)

30. Simulate webcam and microphone

You might also like