Selenium Quick Reference All Commands
Selenium Quick Reference All Commands
Instead of having machines with all those browsers, I just use Endtest.
It's a platform for Codeless Automated Testing where you can create, manage and execute tests on
real browsers on Windows and macOS machines and mobile devices.
3. Open a website
the_url = "https://example.com"
driver.get(the_url)
4. Find an element
Let's try to find this element:
<a href="/sign-up" id="register" name="register" class="cta nav-link">Sign Up</a>
Find element by ID
the_id = 'register'
element = driver.find_element_by_id(the_id)
the_css_selector = 'a[href="/sign-up"]'
element = driver.find_element_by_css_selector(the_css_selector)
5. Click on an element
the_id = 'register'
element = driver.find_element_by_id(the_id)
element.click()
8. Take a screenshot
the_path = 'C:/tests/screenshots/1.png'
driver.save_screenshot(the_path)
Selenium does not offer Screenshot Comparison but we know who does.
9. Upload a file
This works by using the send_keys method to write the local path of the file in the input type="file"
element.
Let's use this example:
<input type="file" multiple="" id="upload_button">
the_file_path = 'C:/tests/files/example.pdf'
the_id = 'upload_button'
element = driver.find_element_by_id(the_id)
element.send_keys(the_file_path)
the_iframe_id = 'payment_section'
the_element_id = 'card_number'
the_iframe = driver.find_element_by_id(the_iframe_id)
driver.switch_to.frame(the_iframe)
element = driver.find_element_by_id(the_element_id)
element.send_keys('41111111111111')
driver.switch_to.default_content()
16. Refresh
driver.refresh()
17. Hover
the_id = "register"
the_element = driver.find_element_by_id(the_id)
hover = ActionChains(driver).move_to_element(the_element)
hover.perform()
the_id = 'register'
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, the_id)))