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 waits
In Selenium web driver, the biggest challenge is to synchronize your script with the Web application under test. To make this synchronization work, we have concept of waits. Although we can use Thread.sleep()...

Free selenium video tutorial
Hi All, Hope you are doing good. We uploaded few videos on Selenium components like IDE, RC and Webdriver on youtube channel you can watch them for free of cost. Kindly search with below link: ...
S

How to Shape Your Software Testing Career?
When I met with the students they ask or can say its a frequently ask question "How much Scope of Testing is?" "What the pay package I will get?" "Is career secure in Software Testing?" "I will learn...

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...

Wait Handler implementation in WebDriver
public void waitForElementPresence(By by) { Wait<WebDriver> wait = new WebDriverWait(driver, GLOBAL_TIME_OUT); wait.until(ExpectedConditions.presenceOfElementLocated(by)); } ...
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