Selenium Locators Cheat Sheet



Python selenium commands cheat sheet

Frequently used python selenium commands – Cheat Sheet

As we can see, Selenium supports the following locators: ClassName – A ClassName operator uses a class attribute to identify an object. CssSelector – CSS is used to create style rules for webpages and can be used to identify any web element. Id – Similar to class, we can also identify elements by using the ‘id’ attribute. Cheat Sheet for Selenium Automation This cheat sheet has examples on common methods used in Selenium Automation. Suggestion is to learn Core Java before moving to Selenium Automation. If you already know Java then Selenium is peanut for you. Once you've identified the target elements and attributes you'd like to use for your test, you'll need to craft locators using one of Selenium's strategies. Selenium is able to find and interact with elements on a page by way of various locator strategies. The list includes (sorted alphabetically): Class in the HTML. Cardinality (Selenium): XPath and CSS may specify a node set or a single node; DOM must specify a single node. When a node set is specified, Selenium returns just the first node. Content (XPath): Generally should use normalize-space when operating on display text. Selenium Cheat Sheet - Ruby. GitHub Gist: instantly share code, notes, and snippets.

To import webdriver module in python use below import statement

Driver setup:
Firefox:
firefoxdriver = webdriver.Firefox(executable_path=”Path to Firefox driver”)

To download: Visit GitHub

Chrome:
chromedriver = webdriver.Chrome(executable_path=”Path to Chrome driver”)

To download: Visit Here

Internet Explorer:
iedriver = webdriver.IE(executable_path=”­Pat­h To­ IEDriverServer.exe”)

To download: Visit Here

Edge:
edgedriver = webdriver.Edge(executable_path=”­Pat­h To­ MicrosoftWebDriver.exe”)

To download: Visit Here

Opera:
operadriver = webdriver.Opera(executable_path=”­Pat­h To­ operadriver”)

To download: visit GitHub

Safari:

SafariDriver now requires manual installation of the extension prior to automation

Browser Arguments:

–headless

To open browser in headless mode. Works in both Chrome and Firefox browser

–start-maximized

To start browser maximized to screen. Requires only for Chrome browser. Firefox by default starts maximized

–incognito

To open private chrome browser

–disable-notifications

To disable notifications, works Only in Chrome browser

Example:

or

To Auto Download in Chrome:

To Auto Download in Firefox:

We can add any MIME types in the list. MIME for few types of files are given below.

  1. Text File (.txt) – text/plain
  2. PDF File (.pdf) – application/pdf
  3. CSV File (.csv) – text/csv or “application/csv”
  4. MS Excel File (.xlsx) – application/vnd.openxmlformats-officedocument.spreadsheetml.sheet or application/vnd.ms-excel
  5. MS word File (.docx) – application/vnd.openxmlformats-officedocument.wordprocessingml.document
    Zip file (.zip) – application/zip

Note:
The value of browser.download.folderList can be set to either 0, 1, or 2.

Selenium xpath cheat sheet

0 – Files will be downloaded on the user’s desktop.
1 – Files will be downloaded in the Downloads folder.
2 – Files will be stored on the location specified for the most recent download

Disable notifications in Firefox

firefoxOptions.set_preference(“dom.webnotifications.serviceworker.enabled”, false);
firefoxOptions.set_preference(“dom.webnotifications.enabled”, false);

Open specific Firefox browser using Binary:

Open specific Chrome browser using Binary:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = “”
driver = webdriver.Chrome(chrome_options=options, executable_path=””)
driver.get(‘http://google.com/’)

Read Browser Details:

driver.title
driver.window_handles
driver.current_window_handles
driver.current_url
driver.page_source

Go to a specified URL:

driver.get(“http://google.com”)
driver.back()
driver.forward()
driver.refresh()

Locating Elements:

driver.find_element_by_ – To find the first element matching the given locator argument. Returns a WebElement

driver.find_elements_by_ – To find all elements matching the given locator argument. Returns a list of WebElement

By ID

<input id=”q” type=”text” />

element = driver.find_element_by_id(“q”)

By Name

<input id=”q” name=”search” type=”text” />

element = driver.find_element_by_name(“search”)

By Class Name

<div class=”username” style=”display: block;”>…</div>

element = driver.find_element_by_class_name(“username”)

By Tag Name

<div class=”username” style=”display: block;”>…</div>

element = driver.find_element_by_tag_name(“div”)

By Link Text

<a href=”#”>Refresh</a>

element = driver.find_element_by_link_text(“Refresh”)

Selenium Locators Cheat Sheet

By Partial Link Text

<a href=”#”>Refresh Here</a>

element = driver.find_element_by_partial_link_text(“Refresh”)

By XPath

<form id=”testform” action=”submit” method=”get”>

Username: <input type=”text” />
Password: <input type=”password” />

</form>

element = driver.find_element_by_xpath(“//form[@id=’testform’]/input[1]”)

By CSS Selector

<form id=”testform” action=”submit” method=”get”>

<input class=”username” type=”text” />
<input class=”password” type=”password” />

</form>

element = driver.find_element_by_css_selector(“form#testform>input.username”)

Important Modules to Import:

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import Select

from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

from selenium.common.exceptions import NoSuchElementException

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

Selenium Locators Cheat Sheet Download

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.firefox.options import Options

Python Selenium commands for operation on elements:

button/link/image:

click()
get_attribute()
is_displayed()
is_enabled()

Text field:

send_keys()
clear()

Checkbox/Radio:

is_selected()
click()

Select:

Find out the select element using any element locating strategies and then select options from list using index, visible text or option value.

Element properties:

is_displayed()
is_selected()
is_enabled()

These methods return either true or false.

Read Attribute:

get_attribute(“”)

Get attribute from a disabled text box

driver.find_element_by_id(“id”).get_attribute(“value”);

Screenshot:

Note: An important note to store screenshots is that save_screenshot(‘filename’) and get_screenshot_as_file(‘filename’) will work only when extension of file is ‘.png’. Otherwise content of the screenshot can’t be viewed

Selenium Locators Cheat Sheet 2020

Read articles for more details about taking screenshot and element screenshot

The list here contains mostly used python selenium commands but not exhaustive. Please feel free to add in comments if you feel something is missing and should be here.

3 Responses

  1. […] Previous: Previous post: Execute Python Selenium tests in Selenium GridNext: Next post: Python selenium commands cheat sheet […]

  2. Thank you very much

  3. Hi Sir,

    I am trying to do the sorting in selenium with python using For loop could u please help me is there any way that i can do it ?

    Sorting in descending order in the below website

    website: https://jqueryui.com/sortable/

Developers new to Selenium frequently run into the same 3 challenges:

- Tests are slow
- Tests are brittle
- Tests are high maintenance

The Big Challenge is Access to Information

All of these items are solvable, the problem is the lack of information: there's either too much to sift through, not enough for your context, or it’s out of date.

Unfortunately there are more resources to dig through than you have time or know what to do with.

With videos, documentation, forums, meetups, conferences, books, and mailing lists, the signal to noise ratio is all out of whack.

They may help you cobble together parts of a solution, solving short term problems. But without guidance, your tests will still be slow, brittle, and rife with false positives. This leads to a lack of trust in your tests.

Use This Book to Get Confidence in Your Selenium Tests

Here are the missing pieces you will learn in this guidebook:

  • Decompose a web application to identify what to test
  • Select which browsers to test with
  • Pick the best language for you and your team
  • Setup Selenium to work with each browser
  • Write maintainable, performant Selenium tests
  • Build an integrated feedback loop to automate test runs and find issues fast
  • Setup your own infrastructure or connect to a cloud provider
  • Dramatically improve test times with parallelization
  • Navigate the Selenium landscape to find information on your own

How do you stack up? Odds are you have some of these points nailed down. But wouldn't you rest easier knowing you had all of them?

You Can Make This Happen … and 'The Selenium Guidebook' Will Teach You How

Write tests you and your team can trust with the techniques in The Selenium Guidebook. You will release software with confidence, knowing that your application has stable, fast tests that cover what matters.

Who This Is For

Selenium Locators Cheat Sheet

This guidebook is for anyone who is using Selenium: test automation engineers, manual and exploratory testers who need to learn automation, and developers who need to implement test automation.

Learn Necessary Skills

To do automated testing well, you don't need to be a developer, but you should be able to write some code. Don't worry, I'll show you what you need to know, with simple examples, to help you quickly put these concepts into practice.

See What People Are Saying

Quick browse through the 'Selenium Guidebook' tells me it's def. worth it, and I don't know a thing about ruby. Thanks, @TourDeDave

— ken pace (@kennypace) December 19, 2013

@kevindewalt@TourDeDave I'd love to eventually incorporate this with the new Selenium docs.

Selenium Cheat Sheet Python

— uǝsɟloʇ sɐǝɹpuɐ (@tolfsen) December 18, 2013

(Andreas is a core committer of the Selenium project)

Buy this book. NOW. 'Selenium Guidebook - A step-by-step guide on how to use Selenium successfully' by @TourDeDavehttps://t.co/GVYTNWXZ75

— Jason Huggins (@hugs) December 19, 2013

(Jason is the creator of Selenium)

Get A Free Sample Of The Book!