UrbanPro
true

Learn Selenium from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

CheatSheet for xpath/css used in Selenium (UI Automation Testing)

S
Sarthak C.
18/11/2021 0 0

Locators

 

Dynamic XPath

String a = "//html/body/div[2]/header/nav/div[2]/div/ul[2]/li[7]/div/div[2]/span/div/div/div[";

                     int i = 1;

                     String b = "]";

 

                     String xp = a + i + b;

 

 

 

===========================================

 

 

 

 

Xpath

 

By.xpath("//span[@class = 'close-icon close']"

 

 

//div[@id="pancakes"]/button[0]

 

//div[@id='' and @value='']

 

 

 

//input[@id='identifierId']

 

Contains:

//img[contains(@src,'Profile')]

 

Starts-with

//img[starts-with(@alt,'Visit on...')]

 

text:

//*[text()='abc']

 

//a[contains(text(),'abc')]

 

Mulitple Attributes:

//HTMLTAG[@att1=''][@att2='']

 

//HTMLTAG[@att1='' and @att2='']

 

//HTMLTAG[@att1='' or @att2='']

 

 

last:

(//input[@class=''])[last()]

(//input[@class="whsOnd zHQkBf"])[last()]

(//input[@class="whsOnd zHQkBf"])[last()-1]

 

 

Position:

(//input[@class="whsOnd zHQkBf"])[position()=1]

(//input[@class="whsOnd zHQkBf"])[position()=2]

 

here index start from 1

 

 

following:

 

//*[@id='FirstName']/following::input[@type='text']

//*[@id='FirstName']/following::input

 

preceding:

//*[@id='LastName']/preceding::input[@type='text']

//*[@id='LastName']/preceding::input

 

 

ancestor:

.//*[@class=’container-fluid’]//ancestor::div[1] – Returns 13 nodes

.//*[@class=’container-fluid’]//ancestor::div[2] – Returns 7 nodes

.//*[@class=’container-fluid’]//ancestor::div[3] – Returns 5 nodes

.//*[@class=’container-fluid’]//ancestor::div[4] – Returns 3 nodes

.//*[@class=’container-fluid’]//ancestor::div[5] – Returns 1 node

 

child:

//nav[@class=’fusion-main-menu’]//ul[@id=’menu-main’]/child::li

 

decendant:

//nav[@class=’fusion-main-menu’]//*[@id=’menu-main’]//descendant::li

 

 

 

 

 

Naurki.com > More

ul.midSec.menu>li:nth-child(5) a div

 

css

 

input[name=email][type=text]

 

  1. If we have id

 

HTMLTag#idname

input#Email

 

  1. If we have class

HTMLTag.classname

input.abc

 

  1. Prefix

css=input#Passwd[name^=’Pass’]

 

  1. Sufix

css=input#Passwd[name$=’wd’]

 

  1. Substring

css=input#Passwd[name*=’wd’]

 

  1. contain text

css=input:contains('abc')

 

css=input[name*='sw']

 

 

  1. child selector

 

label>input

 

 

  1. child element

with space

 

  1. <ul id="fruit">

    <li>Apple</li>

    <li>Orange</li>

    <li>Banana</li>

</ul>

 

css="ul#fruit li:nth-of-type(2)

 

 

10.

 

Not contain

css = "div[class*=calendar-day-]:not([class*='unavailable'])"

 

 

 

 

===========================================

 

id

 

driver.findElement(By.id("txtPassword")).sendKeys("abc@123");

 

driver.findElement(By.id("Email")).sendKeys("atyagi.mails");

 

 

===========================================

 

 

name

 

driver.findElement(By.name("signIn")).click();

 

 

 

 

 ================================

dom

 

following ways: document.forms[0].elements[0]

 

document.forms[‘loginForm’].elements[‘username’]

 

document.forms[‘loginForm’].username

 

document.getElementById(‘username’)

 

 =================================

  1. //HTML-TAG[@att = 'val']

 

//div[@class = 'mTxt'] = Job

 

//div[@class = 'mTxt'] = Rec

 

-----------------------

 

2.

 

//descendant::HTML-TAG[@att = 'val'][num]

 

//descendant::div[@class = 'mTxt'][1]

 

------------------------------

3.

 

//HTML-TAG[text() = 'txtVal']

 

//div[text() = 'Jobs']

 

//input[@class = 'sdTxt w85']

 

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Selenium Web Drive Wait Methods
Hi I am Rajesh, Here we have three types of wait methods in Selenium: 1.Implicitly Wait: Wait if required, otherwise, if the accuracy will good program will move to next level In General most of the...

What Is The Difference Between Driver.close() And Driver.quit()?
• Driver.close(): It is used to close the browser or page currently in focus. close() is a webdriver command which closes the browser window which is currently in focus. During the automation process,...

Xpath Cheatsheet
Xpath Descendant selectors h1 //h1 ? div p //div//p ? ul > li //ul/li ? ul > li > a //ul/li/a div > * //div/* :root

Difference between driver.close() and driver.quit () method
close () -By using this command, we can close the current browser or page. quit() - By using this command, we can shut down the web driver instance or destroy the web driver instance(Close all the windows).

Write a script to open firefox without using firefox driver class?
Write a script to open firefox without using firefox driver class? Solution:Runtime.getRuntime().exec("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
X

Looking for Selenium Classes?

The best tutors for Selenium Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Selenium with the Best Tutors

The best Tutors for Selenium Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more