Last active
July 29, 2021 15:51
-
-
Save IngmarBoddington/b784d2b50c54a998131a05519e264265 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
? Action Chains? (Why? | |
? move_to_element? (Why?) | |
WebDriver | |
--------- | |
Allows control over browser functions | |
#Import - for features in this doc (Chrome is generally being assumed) | |
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
from selenium.webdriver.common.action_chains import ActionChains | |
#Get a sized window (example, with implicit wait) | |
options = Options() | |
options.add_argument("window-size=1920,1080") | |
driver = webdriver.Chrome(options=options) | |
driver.implicitly_wait(30) | |
... | |
driver.close() | |
#Fetch a page- WebDriver will wait until the page has fully loaded (that is, the onload event has fired) before returning control to your test or script. It’s worth noting that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded. If you need to ensure such pages are fully loaded then you can use waits. | |
driver.get("<URL>") | |
WebElement | |
---------- | |
Allows control over element from a webpage, instance of selenium.webdriver.remote.webelement.WebElement | |
#If there’s more than one element that matches the query, then only the first will be returned. If nothing can be found, a NoSuchElementException will be raised. | |
element = driver.find_element_by_id("<id>") | |
element = driver.find_element_by_name("<name>") | |
element = driver.find_element_by_xpath("<xpath>") | |
element = driver.find_element_by_link_text("<text>") | |
element = driver.find_element_by_partial_link_text("<text>") | |
element = driver.find_element_by_tag_name("<tagName>") | |
element = driver.find_element_by_class_name("<className>") | |
element = driver.find_element_by_css_selector("<cssSelector>") | |
#Return lists of elements | |
elements = driver.find_elements_by_name("<name>") | |
elements = driver.find_elements_by_xpath("<xpath>") | |
elements = driver.find_elements_by_link_text("<text>") | |
elements = driver.find_elements_by_partial_link_text("<text>") | |
elements = driver.find_elements_by_tag_name("<tagName>") | |
elements = driver.find_elements_by_class_name("<className>") | |
elements = driver.find_elements_by_css_selector("<cssSelector>") | |
#Actions | |
element.clear() | |
element.send_keys("<text>"[,...]) #Can send special keys documented below instead of text | |
element.click() | |
... | |
#Action Chains - This is useful for doing more complex actions like hover over and drag and drop. Action chain methods are used by advanced scripts where we need to drag an element, click an element, etc | |
action = ActionChains(driver) | |
#Can chain together... | |
action.click([element]) | |
action.click_and_hold([element]) | |
action.context_click([element]) | |
action.double_click([element]) | |
action.drag_and_drop(source, target) | |
action.drag_and_drop_by_offset(element) | |
action.key_down(value[, element]) | |
action.key_up(value[, element]) | |
action.move_by_offset(xoffset, yoffset) | |
action.move_to_element(element) | |
action.move_to_element_with_offset(element, xoffset, yoffset) | |
action.pause(seconds) | |
action.release(element) | |
action.reset_actions(element) | |
action.send_keys('<text>') | |
#Execute with perform | |
action.perform() | |
Alert Handling | |
-------------- | |
Alert(driver).accept() | |
Alert(driver).dismiss() | |
Window Handling | |
--------------- | |
handle = driver.current_window_handle | |
handles = driver.window_handles | |
driver.switch_to.window(handle) | |
#Scroll to end of page | |
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") | |
Wait Handling | |
------------- | |
#Explicit Waits - Explicit waits are introduced to temporarily freeze the execution of the Selenium test automation script. | |
WebDriverWait(driver, <timeout>[, <pollFrequency>, <ignoredExceptionsList>).until(EC.<conditionType>(<condition>) | |
WebDriverWait(driver, <timeout>[, <pollFrequency>, <ignoredExceptionsList>).until_not(<condition>) | |
#pollFrequency is 0.5 default (0 would default back to 0.5) | |
#ignoredExceotionsList is used to pass list of exceptions to be ignored when waiting (NoSuchElementException is ignored by default) | |
#Conditions - These are generally used in the form EC.condition(<args>) | |
#Args = By.<type>, "identifier", for example EC.element_to_be_clickable((By.ID, "IAmAnIdGov")) | |
#GOTCHA: Note that we are passing a tuple! Hence the additional brackets around the args! | |
alert_is_present | |
element_to_be_clickable | |
element_to_be_selected | |
frame_to_be_available_and_switch_to_it | |
new_window_is_opened | |
number_of_windows_to_be | |
presence_of_element_located | |
text_to_be_present_in_element | |
title_contains | |
title_is | |
url_changes | |
url_contains | |
url_matches | |
#TIP: Declare the wait and reuse, e.g: | |
wait = WebDriverWait(driver, 20) | |
wait.until(EC.element_to_be_clickable((By.ID, "_evidon-accept-button"))).click() | |
#Implicit Waits | |
driver.implicitly_wait(<time>) | |
Special Keys | |
------------ | |
#Format = Keys.SOMETHINGBELOW | |
ADD ALT ARROW_DOWN | |
ARROW_LEFT ARROW_RIGHT ARROW_UP | |
BACKSPACE BACK_SPACE CANCEL | |
CLEAR COMMAND CONTROL | |
DECIMAL DELETE DIVIDE | |
DOWN END ENTER | |
EQUALS ESCAPE F1 | |
F10 F11 F12 | |
F2 F3 F4 | |
F5 F6 F7 | |
F8 F9 HELP | |
HOME INSERT LEFT | |
LEFT_ALT LEFT_CONTROL LEFT_SHIFT | |
META MULTIPLY NULL | |
NUMPAD0 NUMPAD1 NUMPAD2 | |
NUMPAD3 NUMPAD4 NUMPAD5 | |
NUMPAD6 NUMPAD7 NUMPAD8 | |
NUMPAD9 PAGE_DOWN PAGE_UP | |
PAUSE RETURN RIGHT | |
SEMICOLON SEPARATOR SHIFT | |
SPACE SUBTRACT TAB | |
XPath | |
----- | |
#Element from Root | |
/html/head/title | |
#Element from anywhere | |
//div/p | |
#Element with property match | |
//label[@for='IsVehicleRegConfirmedA'] | |
Exceptions | |
---------- | |
#Lots of possible related Python Exceptions | |
ElementClickInterceptedException | |
ElementNotInteractableException | |
ElementNotSelectableException | |
ElementNotVisibleException | |
ErrorInResponseException | |
ImeActivationFailedException | |
ImeNotAvailableException | |
InsecureCertificateException | |
InvalidArgumentException | |
InvalidCookieDomainException | |
InvalidCoordinatesException | |
InvalidElementStateException | |
InvalidSelectorException | |
InvalidSessionIdException | |
InvalidSwitchToTargetException | |
MoveTargetOutOfBoundsException | |
NoAlertPresentException | |
NoSuchAttributeException | |
NoSuchCookieException | |
NoSuchFrameException | |
NoSuchWindowException | |
StaleElementReferenceException | |
TimeoutException | |
UnableToSetCookieException | |
UnexpectedAlertPresentException | |
UnexpectedTagNameException | |
Advanced / Extended Use Cases | |
----------------------------- | |
* Import unittest for assertions | |
References | |
---------- | |
https://www.geeksforgeeks.org/selenium-python-tutorial/ | |
https://www.lambdatest.com/blog/selenium-wait-for-page-to-load/ | |
https://selenium-python.readthedocs.io/waits.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment