Finding Elements by XPATH and Using CSS - SELENIUM

Finding Elements by XPATH

1. Finding elements by XPATH and ID
2. Finding elements by XPATH and NAME
3. Finding elements by XPATH and LINK
4. Finding elements by XPATH and Text

Finding elements by XPATH and ID:-

Type  //form[@id=’search_mini_form’]/div/input

Accelerate your career with Selenium Training and become an expertise in Selenium.

Finding elements by XPATH and NAME:-

Type    //input[@name=’q’]

Finding elements by XPATH and LINK:-

Click   //a[@id=’navigation-top-cat-label-707′]
Click   //ul[@id=’nav’]/li[3]/a
Click   //a[@href=’https://www.rightstart.com/search/result/?ocat=707′]

Partial Match on Attribute Content:-

There are times where only part of the ID is dynamic. For example, if you wanted to access the element that has the text “This element has an ID that changes every time the page is loaded” in it, you will use //div[contains(@id,’time_’)]. This is due to the first part of the ID always being static. The locator could also use starts-with instead of contains to make the XPath query stricter in what is returned.
We can use two methods to find the elements with text

  • Contains
  • Starts-with

Syntax:- //element[text()=’inner text’].
Example for Starts with: for link Booster seats
//a[starts-with(text(),’Boost’)]
Example for contains: for link Car seat bases
//a[contains(text(), ‘Seat’)]
Finding elements by the text they contain can be quite useful when working with web pages that have been created dynamically.

MindMajix YouTube Channel

Checkout Selenium Interview Questions

Contains:-

If we know part of the text then we have to use this command.
Syntax: //element[contains(text(),’inner text’)]

//a[contains(text(),’Car Seat Bases’)]

For the above example the command is :
//a[contains(text(),’Car Seat Bases’)]

Starts with:-

Syntax:- //element[starts with(text(),’inner text’)]
If we know the starting of the text then we have to use this command.

//a[starts-with(text(),’Boost’)]

For the above example the command is:
//a[starts-with(text(),’Boost’)]

Checkout Selenium Tutorial

Finding Elements by using CSS

locators are a way to tell selenium which specific element we want it to act on Why CSS

  • Because they are faster
  • Because they are more readable

MORE READABLE:-

//input[@id=”myId”] vs css=input#myId
//input[@class=”myClass”] vs  css=input.myClass
//input[@name=”myName”] vs  css=input[name=myName] //*[@id=”myId”] vs css=#myId
//table[@id=”myId”]//tr[@class=”myClass”]//td[3] vs css=table#myId tr.myClass td:nth(3)

Explore Selenium Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Selenium Training Nov 26 to Dec 11View Details
Selenium Training Nov 30 to Dec 15View Details
Selenium Training Dec 03 to Dec 18View Details
Selenium Training Dec 07 to Dec 22View Details
Last updated: 27 Sep 2024
About Author

 

Madhuri is a Senior Content Creator at MindMajix. She has written about a range of different topics on various technologies, which include, Splunk, Tensorflow, Selenium, and CEH. She spends most of her time researching on technology, and startups. Connect with her via LinkedIn and Twitter .

read less
  1. Share:
Selenium Articles