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

Constructor Overloading
public class ConstructorOverloading { public ConstructorOverloading(){ System.out.println("default"); } public ConstructorOverloading(String a){ System.out.println("a"); } public ConstructorOverloading(int...
S

Sarthak C.

0 0
0

Script we have to write even before hooks in cucumber
In Cucumber, we have Before & After hook – it will run before and after each scenarios. Problem Statement: What if we have to open the database connectivity (only once) before executing the...
S

Sarthak C.

0 0
0

TestNG Annotations and its sequence
public class TestNGAnnotations { @BeforeMethod public void beforeM() { System.out.println("Before Method"); } @AfterMethod public void afterMethod() { System.out.println("After Method"); } @BeforeClass...
S

Sarthak C.

0 0
0

ListIterator Example
public class ListIteratorEx { public static void main(String args) { List<String> li = new ArrayList<String>(); li.add("java1"); li.add("java2"); li.add("java3"); li.add("java4"); ListIterator...
S

Sarthak C.

0 0
0

Example of DependsOnMethod in TestNG
public class dependsonM { @Test public void login() { System.out.println("login"); } @Test (dependsOnMethods = {"login"}) public void email() { //Intentionally I am failing this testcase Assert.assertTrue(false);...
S

Sarthak C.

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