Selenium - Python - Cheat Sheet
Selenium - Python - Cheat Sheet
🇨🇦
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()