Selenium Flashcards
(24 cards)
Find Element
driver.find_element(By.METHOD, locator)
Find Elements
driver.find_elements(By.METHOD, locator)
Wait for Presence of element
wait.until(EC.presence_of_element_located(By.CSS_SELECTOR, ‘.class’)
Wait for visibility
wait.until(EC.visibility_of_element_located(By.CSS_SELECTOR, ‘.class’)
Wait for Clickable
wait.until(EC.element_to_be_clickable(By.CSS_SELECTOR, ‘.class’)
Action Chain
from selenium.webdriver.common.action_chains import ActionChains action = ActionChain(driver) action.move_to_element(element) action.perform()
Select
from selenium.webdriver.support.ui import Select selector = Select(context.driver.find_element(*SORT_SELECTOR)) selector.select_by_index(2) selector.select_by_visible_text('text') selector.select_by_value('text')
Get text from an Element
element.text
Get value of CSS on element
element.value_of_css_property('text-decoration')
Get attribute of HTML Element
element.get_attribute(‘alt’)`
Reload Page
driver.refresh()
Set Up Driver
from selenium import webdriver from selenium.webdriver.chrome.service import Service service = Service(executable_path='./env/bin/chromedriver') driver = webdriver.Chrome(service=service)
Loop through list in assertion
assert any(dog[‘name’] == name for dog in data),
Import By
from selenium.webdriver.common.by import By
Import Expected Conditions
from selenium.webdriver.support import expected_conditions as EC
Setup Wait
from selenium.webdriver.support.ui import WebDriverWait wait = WebDriverWait(driver, 10, 0.1)
assert type
Note lack of Parethesis on dict
assert type(my_var) is dict assert type(my_var) == str assert type(my_var) is list assert type(my_var) is int
Capitalization
string.lower() string.upper() string.capitalize() string.title()
Get coordinates of a location
element.location['x'] element.location['y'] scroll_by_coord = 'window.scrollTo(%s,%s);' % ( x, y )
Selenium
Run Javascript
driver.execute_script(“arguments[0].value = ‘hello world’”, element)
Selenium
CSS Selector for input html
(By.CSS_SELECTOR, 'input[placeholder="Last Name"]')
Selenium
Assign active element
element = driver.switch_to.active_element
Selenium Keys
from selenium.webdriver.common.keys import Keys Keys.TAB Keys.ARROW_LEFT Keys.RETURN Keys.SPACE
Selenium XPATHs
(By.XPATH, '//*[@type="submit"]') '//*[@aria-label="Go to Home"]' //*[contains(text(), "example")] (//*[@type="button"])[2] //div[contains' '(@class, "some-class")]ancestor::div[contains' '(@class, "some-class")]/a