banner



How To Find Xpath Of An Element

It is mutual knowledge by now that Selenium is the most oft used automation testing tool. With its diversity of features, it facilitates various functionalities. One of these is the employ of locators to find web elements when testing a website. Among various locators, XPath is particularly useful.

This article will examine Xpath, and how to utilize it to notice web elements in automatic Selenium testing.

Below topics are covered in this commodity:

Note: Please refer to Locators in Selenium before going frontwards with this article.

Introduction to XPath

XPath is a language to query XML documents. Usable on both HTML and XML documents, it allows testers to navigate through the XML construction of whatsoever certificate. It consists of a path expression along with some atmospheric condition.

Syntax of XPath

Below is the syntax for Xpath:

Xpath =//tagname[@Attribute='value']

Wherein:

  1. //: Used to select the electric current node.
  2. tagname: Name of the tag of a particular node.
  3. @: Used to select the select aspect.
  4. Aspect: Name of the attribute of the node.
  5. Value: Value of the attribute

To dive deeper into XPath fundamentals, report this article on Effective ways to use XPath in Selenium.

Before exploring XPath functions, let's await at a simple instance to understand.

How to find elements by XPath in Selenium: Instance

This case will launch Google Chrome and navigate to yahoo.com to create a yahoo account. The intent is to locate the fields using XPath.

Find element by Xpath

  1. Go to the Get-go name tab and right click >> Inspect.
  2. On inspecting the web element, it volition show an input tag and attributes like grade and id.
  3. Utilise the id and these attributes to construct XPath which, in turn, volition locate the first name field.

selenium find element by xpath

Let's write XPath in the elements tab.

Note: Use Ctrl+F to write XPath in the elements tab as shown below.

selenium find element by xpath example

As seen to a higher place, a elementary XPath is used to locate the firstName tab. Based on the XPath syntax, first use // which means anywhere in the document. Hither, input is the tag name that has an id aspect with the value "usernamereg-firstname".

On writing the XPath, it has highlighted the element which implies that this item element was located using XPath.

Notation: 1 tin can besides locate the same chemical element using the name attribute every bit it has locator value for the name tag as well. Using the name locator, the XPath is: //input{@name="firstname"].

Refer to the snapshot below for clarity:

find element by xpath in Selenium


Also Read: How to Discover Element by Text in Selenium: Tutorial


At present let's try automating this using Selenium. Hither is the Java program written in Eclipse for the aforementioned:

import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public grade XPathexample { public static void main(Cord[] args) { System.setProperty("webdriver.chrome.driver", "Your-Chrome-Driver-Path"); // Provide the path to your chrome driver. Make certain your chrome driver version is updated. WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().deleteAllCookies(); commuter.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.become("https://login.yahoo.com/account/create"); commuter.findElement(By.xpath("//input[@id='usernamereg-firstName']")).sendKeys("Your-Name"); // Will ship values to Kickoff Name tab } }

On executing the code above, Chromedriver will launch Google Chrome, navigate to Yahoo signup folio and enter the value for first name tab every bit shown beneath.

Similarly, fill in all the details and find elements by XPath in Selenium.

import java.util.concurrent.TimeUnit; import org.openqa.selenium.Past; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public grade XPathexample { public static void main(Cord[] args) { System.setProperty("webdriver.chrome.commuter", "Your-Chrome-Driver-Path"); // Provide the path to your chrome driver. Brand certain your chrome driver version is updated. WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); commuter.manage().deleteAllCookies(); driver.manage().timeouts().pageLoadTimeout(twoscore, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(thirty, TimeUnit.SECONDS); driver.get("https://login.yahoo.com/business relationship/create"); driver.findElement(By.xpath("//input[@id='usernamereg-firstName']")).sendKeys("Your-Name"); // Will send values to Showtime Name tab driver.findElement(By.xpath("//input[@id='usernamereg-lastName']")).sendKeys("Your-Last_name"); //xpath for terminal name box commuter.findElement(By.xpath("//input[@id='usernamereg-yid']")).sendKeys("email@yahoo.com"); //xpath for email box driver.findElement(By.xpath("//input[@id='usernamereg-phone']")).sendKeys("123456789"); //xpath for phone number box driver.findElement(By.xpath("//select[@id='usernamereg-calendar month']")).click(); //xpath for usermonth box driver.findElement(By.xpath("//input[@id='usernamereg-mean solar day']")).sendKeys("01"); //xpath for userday box driver.findElement(By.xpath("//input[@id='usernamereg-twelvemonth']")).sendKeys("1999");// xpath for user year commuter.findElement(By.xpath("//button[@id='reg-submit-button']")).click();// xpath for submit push button } }

On executing the above plan it will fill in all the details and hit the Go along push to complete the form submission.

Run Selenium Tests on Real Devices for Complimentary

While this post has discussed a variety of ways to locate elements on a web folio using the XPath locator in Selenium Webdriver, ane should use Occam'due south razor – the simplest and logical options while selecting elements to ensure minimal rework in the event of a page redesign.

Try running the lawmaking detailed above using Selenium. Carry in heed that Selenium tests must be run on a real device cloud to get completely accurate results. BrowserStack's cloud Selenium grid of 2000+ real browsers and devices allows testers to automatic visual UI tests in real user conditions. Simply sign up, select a device-browser-OS combination, and start running tests for free.

How To Find Xpath Of An Element,

Source: https://www.browserstack.com/guide/find-element-by-xpath-in-selenium#:~:text=Go%20to%20the%20First%20name,locate%20the%20first%20name%20field.

Posted by: santimandry.blogspot.com

0 Response to "How To Find Xpath Of An Element"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel