Selenium Selectors Cheat Sheet

Posted : admin On 1/3/2022
  1. Selenium-only variation ⌦ Not supported by Selenium pace character expression CSS3 or XPath 2.0 DOM abbreviations: gEBI getElementById gEBTN getElementsByTagName Rosetta Stone and Cookbook Sprinkled with Selenium usage tips, this is both a general-purpose set of recipes for each technology as well as a cross-reference to map from one to.
  2. Java selenium commands cheat sheet - Selenium Blog says: May 11, 2018 at 11:32 am Previous: Previous post: Execute Python Selenium tests in Selenium GridNext: Next post: Python selenium commands cheat sheet.

Selenium Cheat Sheet in Python. Install Selenium. To install Selenium webdriver, pip3 install selenium. By CSS Selector. This part of the Selenium tutorial includes the Selenium Cheat Sheet. In this part, you will learn various aspects of Selenium that are possibly asked in interviews. Also, you will have a chance to understand the most important Selenium terminologies. Become a Certified Professional.

Java selenium commands cheat sheet

Frequently used java selenium commands – Cheat Sheet

Visit python selenium commands cheat sheet here.

Driver setup:

Firefox:

System.se­tPr­ope­rty­(“we­bdr­ive­r.g­eck­o.d­riv­er”, “­Pat­h To­ g­eck­odr­ive­r”);

To download: Visit GitHub

Chrome:

System.se­tPr­ope­rty­(“we­bdr­ive­r.chrome.d­riv­er”, “­Pat­h To­ chromedr­ive­r”);

To download: Visit Here

Internet Explorer:

System.se­tPr­ope­rty­(“we­bdr­ive­r.ie.d­riv­er”, “­Pat­h To­ IEDriverServer.exe”);

To download: Visit Here

Edge:

System.se­tPr­ope­rty­(“we­bdr­ive­r.edge.d­riv­er”, “­Pat­h To­ MicrosoftWebDriver.exe”);

To download: Visit Here

Opera:

System.se­tPr­ope­rty­(“we­bdr­ive­r.opera.d­riv­er”, “­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

Note:

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

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

Private browser in Firefox:

Note: you may not see any indication that browser is private. To check, type ‘about:config’ and search for ‘browser.privatebrowsing.autostart’

Disable notifications in Firefox

Read Browser Details:

driver.getTitle();
driver.getWindowHandle();
driver.getWindowHandles();
driver.getCurrentUrl();
driver.getPageSource();

Go to a specified URL:

driver.get(“http://google.com”)
driver.navigate().to(“http://google.com”)
driver.navigate().to(new URL(“http://google.com”))
driver.navigate().back()
driver.navigate().forward()
driver.navigate().refresh()

Locating Elements:

driver.findEelement(By) – To find the first element matching the given locator argument. Returns a WebElement

driver.findElements(By) – To find all elements matching the given locator argument. Returns a list of WebElement

By ID

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

WebElement element = driver.findElement(By.id(“q”))

Selenium Selectors Cheat Sheet

By Name

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

WebElement element = driver.findElement(By.name(“search”));

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

WebElement element = driver.findElement(By.className(“username”));

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

WebElement element = driver.findElement(By.tagName(“div”));

By Link Text

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

WebElement element = driver.findElement(By.linkText(“Refresh”));

By Partial Link Text

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

WebElement element = driver.findElement(By.partialLinkText(“Refresh”));

By XPath

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

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

</form>

WebElement element = driver.findElement(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>

WebElement element = driver.findElement(By.cssSelector(“form#testform>input.username”));

Java Selenium commands for operation on Elements:

button/link/image:

click()
getAttribute()
isDisplayed()
isEnabled()

Text field:

sendKeys()
clear()

Checkbox/Radio:

isSelected()
click()

Select:

Select select = new Select(WebElement);

select.selectByIndex();
select.selectByValue();
select.selectByVisibleText();

select.deselectAll();
select.deselectByIndex();
select.deselectByValue();
select.deselectByVisibleText();

getFirstSelectedOption()
getAllSelectedOptions() – Returns List

Element properties:

isDisplayed()
isSelected()
isEnabled()

Read Attribute:

getAttribute(“”)

Get attribute from a disabled text box

driver.findElement(By).getAttribute(“value”);

Screenshot:

image storage without using any extra libraries.

Universal Wait:

Wait using jQuery:

Note: jQuery must be defined in Web page otherwise WebDriverException will be thrown. Hence first validate jQuery is defined then check for async calls

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

Selenium Selectors Cheat Sheet


For Python version the link is here


Install Java

To install Java go to this link


IDE

You can use any text editor. I recommend Eclipse as it is free and have extensive support. For list of popular editors , this are the links


Download Selenium

Download selenium webdriver in this link


Dirty our hands !


Import Selenium



Browsers support (Firefox , Chrome , Internet Explorer, Edge , Opera)

Driver setup:

Chrome:

System.se­tPr­ope­rty­('we­bdr­ive­r.chrome.d­riv­er', “'Pat­h To­ chromedr­ive­r');

To download: Visit Here


Firefox:

System.se­tPr­ope­rty­('we­bdr­ive­r.g­eck­o.d­riv­er', '­Pat­h To­ g­eck­odr­ive­r');

To download: Visit GitHub


Internet Explorer:

System.se­tPr­ope­rty­('we­bdr­ive­r.ie.d­riv­er', 'Pat­h To­ IEDriverServer.exe');

To download: Visit Here

Edge:

System.se­tPr­ope­rty­('we­bdr­ive­r.edge.d­riv­er', 'Pat­h To­ MicrosoftWebDriver.exe');

To download: Visit Here


Opera:

System.se­tPr­ope­rty­('we­bdr­ive­r.opera.d­riv­er', 'Pat­h To­ operadriver');

To download: visit GitHub

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:


Alternative


Launch URL


Retrieve Browser Details:


Navigation


Locating Elements

By id

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


By Class Name

<input class=”gLFyf” type=”text” />


By Name

<input name=”z” type=”text” />


By Tag Name

<div id=”login” >…</div>


By Link Text

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


By XPath

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

Username: <input type=”text” />

Password: <input type=”password” />

</form>


By CSS Selector

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

Username: <input type=”text” />

Password: <input type=”password” />

</form>


Clicking / Input text

Clicking button


Send Text


Waits

Implicit Waits

An implicit wait instructs Selenium WebDriver to poll DOM for a certain amount of time, this time can be specified, when trying to find an element or elements that are not available immediately.

Selenium Selectors Cheat Sheet Download


Explicit Waits

Explicit wait make the webdriver wait until certain conditions are fulfilled . Example of a wait


List of explicit waits

  • alertIsPresent()
  • elementSelectionStateToBe()
  • elementToBeClickable()
  • elementToBeSelected()
  • frameToBeAvaliableAndSwitchToIt()
  • invisibilityOfTheElementLocated()
  • invisibilityOfElementWithText()
  • presenceOfAllElementsLocatedBy()
  • presenceOfElementLocated()
  • textToBePresentInElement()
  • textToBePresentInElementLocated()
  • textToBePresentInElementValue()
  • titleIs()
  • titleContains()
  • visibilityOf()
  • visibilityOfAllElements()
  • visibilityOfAllElementsLocatedBy()
  • visibilityOfElementLocated()



Loading a list of elements like li and selecting one of the element



Read Attribute


Get CSS

CSS values varies on different browser, you may not get same values for all the browser.


Capture Screenshot

This will saved the file as in the path of destFile.


isSelected()

isSelected() method in selenium verifies if an element (such as checkbox) is selected or not. isSelected() method returns a boolean.


isDisplayed()

isDisplayed() method in selenium webdriver verifies and returns a boolean based on the state of the element (such as button) whether it is displayed or not.

Selenium Selectors Cheat Sheet Pdf


Selenium Selectors Cheat Sheet Excel

isEnabled()

is_enabled() method in selenium python verifies and returns a boolean based on the state of the element (such as button) whether it is enabled or not.

Selenium Cheat Sheet Python

Minimum modules to import


Created : 17 December 2019