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

What is Selenium?
In simple words, Selenium automates browsers. It is a robust set of tools that supports rapid development of test automation for web-based applications. Cool Features: Works on every system,...

Difference b/w Manual testing and Automated testing
Manual Testing Time consuming and tedious. Huge investment in human resources. Less reliable because in manual testing, due to repetitive tasks and time constraints, we might skip few test...

Why JAVA is Platform Independent?
Java is platform independent language because Java has a JVM {Java Virtual Machine} which converts a java code into a byte code which can run on any OS. That means, JVM is working as a sub-platform to...

High level Steps to target for cracking Interview for Selenium with java
1. Prepare well to explain the framework and its high level flow using different tools/component used in the framework development. 2. Prepare well with the selenium commands. 3.Prepare well with Basic/Oops...

Introduction of Selenium
Selenium is one of the most popular open-source automated testing Tools available today. Selenium automates web applications for testing purposes, but is certainly not limited to just that.Introducing...
N
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