visit
<form method="POST" action="//accounts.lambdatest.com/register">
<input type="hidden" name="_token" value="W4D7bCygQ4abq2Xa3ckZ2rwG3Wk7f9enPXIIPeuF">
<div class="col-sm-12 google-sign-form"><p class="signup-titel" style="text-align: center;">SIGN UP FOR FREE</p></div> <div class="col-sm-12 google-sign-form">
<input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 ">
<input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 ">
<input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 ">
<input type="password" placeholder="Desired Password*" name="password" class="form-control sign-up-input-2 "> <input type="phone" placeholder="Phone*" name="phone" value="" class="form-control sign-up-input-2 "> <p class="terms-cond"><label for="terms_of_service" class="woo">
<input type="checkbox" name="terms_of_service" id="terms_of_service" value="1" class="form-check-input" style="position: relative; margin-left: 0px;"> I agree to the <a target="_blank" href="//www.lambdatest.com/terms-of-service">Terms
of Service</a>
and <a target="_blank" href="//www.lambdatest.com/privacy">Privacy Policy</a></label></p> <button type="submit" class=" btn sign-up-btn-2 btn-block">Signup for Free</button></div>
<div class="col-sm-12 link-sect"><p class="login-in-link test-left">Already have an account? <a href="/login">Login</a></p></div></form>
//form/div[@class=’ col-sm-12 google-sign-form’]/input[@name=’ name’]
//tagname[@attribute name= ‘value’]
/html/body/div[1]/section/div/div[2]/div/form/div[2]/input[3]
//input[@name=’email’]
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class RelativeXpathExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", " path to chromedriver ");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//accounts.lambdatest.com/register");
//Relative xpath for organization field
driver.findElement(By.xpath("//input[@name='organization_name']")).sendKeys("Lambdatest Org");
//Relative xpath for full name field
driver.findElement(By.xpath("//input[@name='name']")).sendKeys("Sadhvi Singh");
driver.close();
}
}
Using Basic XPath
This is the common and syntactical approach of writing the XPath in Selenium which is the combination of a tagname and attribute value. Here are few basic XPath examples in Selenium:< input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 " >
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XpathExampleWithAndOR {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", " path to chromedriver ");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//accounts.lambdatest.com/register");
//Finding the organization field via xpath using OR
driver.findElement(By.xpath("//input[@name='organization_name' or @placeholder='Organization/Company Name']")).sendKeys("Lambdatest");
//Finding the full name field via xpath using AND
driver.findElement(By.xpath("//input[@name='name' and @placeholder='Full Name*']")).sendKeys("Lambdatest");
//Finding the work email field via xpath using OR, where only one of the attribute defined is correct whereas the other incorrect and does not match, still this should work as one of them meets the condition.
driver.findElement(By.xpath("//input[@name='email' or @id='not present']")).sendKeys("Lambdatest");
//Finding the password field via xpath using AND, where only one of the attribute defined is correct whereas the other incorrect and does not match,this should NOT work as one of them does not meets the condition.
driver.findElement(By.xpath("//input[@name='password' and @id='not present']")).sendKeys("Lambdatest");
}
}
Please Note: Both ‘and’ and ‘or’ should be case sensitive. In case you tend to use, ‘OR’ or ‘AND’, you will get an error in console stating invalid xpath expression
<input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 ">
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XpathContainsExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "path to chromedriver");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//www.lambdatest.com");
//Finding the element 'Start testing' having text as same, here we will locate element using contains through xpath
driver.findElement(By.xpath("//a[contains(text(), 'TESTING')]")).click();
driver.close()
}
}
<input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 ">
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XpathStartsWithExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "path to chromeDriver");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//www.lambdatest.com");
//Finding the element 'Start testing' having text as same, here we will locate element using contains through xpath
driver.findElement(By.xpath("//a[starts-with(text(), 'START')]")).click();
driver.close();
}
}
<button type="submit" class=" btn sign-up-btn-2 btn-block">Signup for Free</button>
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XpathWithTextExample {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "path to the chromedriver");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("//www.lambdatest.com/blog");
//Finding the blog with text as 'complete guide on TestNG annotations' element
driver.findElement(By.xpath("//a[text()='Complete Guide On TestNG Annotations For Selenium WebDriver']")).click();
driver.close();
}
}
<div class="col-sm-12 google-sign-form"><input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 "> <input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 "> <input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 ">
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XpathWithIndex {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ss251550\\eclipse-workspace_automate\\Automate_Active_MQ_Process\\ChromeDriver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//accounts.lambdatest.com/register");
driver.manage().window().maximize();
//finding the element through index using the reference of the form field containing the fields in it.
driver.findElement(By.xpath("//div[@class='col-sm-12 google-sign-form']/input[2]")).sendKeys("sadhvi singh");
driver.close();
}
}
<ul class="nav navbar-nav navbar-right">
<li><a href="//www.lambdatest.com/feature">Live</a></li>
<li><a href="//www.lambdatest.com/selenium-automation">Automation</a></li> <li><a href="//www.lambdatest.com/blog">Blog</a></li>
<li><a href="//www.lambdatest.com/pricing">Pricing</a></li>
<li><a href="//www.lambdatest.com/support/">Support</a></li>
<li class="sign-in"><a href="//accounts.lambdatest.com/login">Login</a></li>
<li class="login"><a href="//accounts.lambdatest.com/register">Free Sign Up</a>
</li>
</ul>
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XpathUsingChaining {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "path of chromeDriver");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//www.lambdatest.com/");
driver.manage().window().maximize();
//locating the 'login' link using xpath chaining and clicking on it.
driver.findElement(By.xpath("//ul[@class='nav navbar-nav navbar-right']//li[@class='sign-in']")).click();
//Verifying the current URL on which we post clicking on it.
String currentURL= driver.getCurrentUrl();
System.out.println(currentURL);
driver.close();
}
}
Console Output
1. Following: This XPath axis helps to locate elements following the current node. Mentioned below is the DOM structure of the LambdaTest Registration Page.
<div class="col-sm-12 google-sign-form">
<input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 ">
<input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 ">
<input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 ">
<input type="password" placeholder="Desired Password*" name="password" class="form-control sign-up-input-2 ">
<input type="phone" placeholder="Phone*" name="phone" value="" class="form-control sign-up-input-2 ">
<p class="terms-cond">
<label for="terms_of_service" class="woo">
<input type="checkbox" name="terms_of_service" id="terms_of_service" value="1" class="form-check-input" style="position: relative; margin-left: 0px;"> I agree to the
<a target="_blank" href="//www.lambdatest.com/terms-of-service">Terms
of Service</a>
and
<a target="_blank" href="//www.lambdatest.com/privacy">Privacy Policy</a>
</label>
</p>
<button type="submit" class=" btn sign-up-btn-2 btn-block">Signup for Free</button>
</div>
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XpathUsingFollowing {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", " path of chromedriver ");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//www.lambdatest.com/");
driver.manage().window().maximize();
//Locate element with the link blog using following axes
driver.findElement(By.xpath("//ul[@class='nav navbar-nav navbar-right']//following::li[3]")).click();
driver.close();
}
}
2. Following sibling: This is one concept that people tend to get confused with. All you get to clear yourself with this concept, is siblings. In case of following all nodes under the current node are the targeted ones irrespective they are under(children) the context node or not but follows below the context node. In case of following siblings, all following nodes of the context node, that shares the same parent, are applicable. In this case all siblings are referred to as children of the parent node. So if you are referencing one of the children and wish to navigate to other children of the same parent that follows it, following sibling does the business.
For example, using the LambdaTest homepage links, in the DOM structure below, one of the children has been referenced, and from it, we will navigate to its siblings.<ul class="nav navbar-nav navbar-right">
<li><a href="//www.lambdatest.com/feature">Live</a></li>
<li><a href="//www.lambdatest.com/selenium-automation">Automation</a></li> <li><a href="//www.lambdatest.com/blog">Blog</a></li>
<li><a href="//www.lambdatest.com/pricing">Pricing</a></li>
<li><a href="//www.lambdatest.com/support/">Support</a></li>
<li class="sign-in"><a href="//accounts.lambdatest.com/login">Login</a></li>
<li class="login"><a href="//accounts.lambdatest.com/register">Free Sign Up</a>
</li>
</ul>
package ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XpathWithFollowingSibling {
public static void main(String[] args) {
// TODO Auto-generated method stub'
System.setProperty("webdriver.chrome.driver", "path of chromeDriver");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//www.lambdatest.com/");
driver.manage().window().maximize();
//locating the 'sign-up' link using xpath following sibling and clicking on it.
driver.findElement(By.xpath("//li[@class='sign-in']//following-sibling::li")).click();
//Verifying the current URL on which we post clicking on it.
String currentURL= driver.getCurrentUrl();
System.out.println(currentURL);
driver.close();
}
}
Console Output
3. Preceding:This method helps in locating elements before the current node, as in the preceding element from the current node with XPath in Selenium. Below is a demonstration of the DOM structure.
<input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 ">
<input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 ">
<input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 ">
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XpathUsingPreceeding {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "path of chromedriver");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//www.lambdatest.com/");
driver.manage().window().maximize();
//Fnding the automation link using the blog link
driver.findElement(By.xpath("//a[text()='Blog']//preceding::li[1]")).click();
driver.close();
}
}
4. Preceding-Sibling: This is a concept similar to following sibling, the only difference in functionality is that of preceding. So, in this case, you can switch among the siblings, but in this, you will switch from the context node being a child and move to the preceding node, you wish to locate. Both the children will share the same parent. Using the same example as mentioned in the following sibling, we will now move from the sign-up link to the login link using preceding-sibling. The syntax for the same is
package ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XpathWithPrecedingSibling {
public static void main(String[] args) {
// TODO Auto-generated method stub'
System.setProperty("webdriver.chrome.driver", "path of chromedriver");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//www.lambdatest.com/");
driver.manage().window().maximize();
//locating the 'login' link using xpath preceding sibling and clicking on it.
driver.findElement(By.xpath("//li[@class='login']//preceding-sibling::li[1]")).click();
//Verifying the current URL on which we post clicking on it.
String currentURL= driver.getCurrentUrl();
System.out.println(currentURL);
driver.close();
}
}
Console Output
5. Child: As the name specified this approach is used when we intend to locate all child elements of the current node. A basic use case of this approach could be when we wish to circulate all the data in a table through the rows, in this case we can find all the children of a particular table. For example, using the below referenced DOM structure, we can create an XPath in Selenium as follows:
<div class="col-sm-12 google-sign-form"><input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 "> <input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 "> <input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 "> <input type="password" placeholder="Desired Password*" name="password" class="form-control sign-up-input-2 "> <input type="phone" placeholder="Phone*" name="phone" value="" class="form-control sign-up-input-2 "> <p class="terms-cond"><label for="terms_of_service" class="woo"><input type="checkbox" name="terms_of_service" id="terms_of_service" value="1" class="form-check-input" style="position: relative; margin-left: 0px;"> I agree to the <a target="_blank" href="//www.lambdatest.com/terms-of-service">Terms
of Service</a>
and <a target="_blank" href="//www.lambdatest.com/privacy">Privacy Policy</a></label></p> <button type="submit" class=" btn sign-up-btn-2 btn-block">Signup for Free</button></div>
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XpathUsingChild {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "path of chrome driver");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//accounts.lambdatest.com/register");
driver.manage().window().maximize();
//Finding the work email filed using the child locator xpath axes
driver.findElement(By.xpath("//div[@class='col-sm-12 google-sign-form']/child::input[3]")).sendKeys("sadhvi singh");;
driver.close();
}
}
6. Parent: This method is used to select the parent node of the current node. For example, for the below referenced DOM structure, the parent node is located through XPath.
<div class="col-sm-12 google-sign-form"><input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 "> <input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 "> <input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 "> <input type="password" placeholder="Desired Password*" name="password" class="form-control sign-up-input-2 "> <input type="phone" placeholder="Phone*" name="phone" value="" class="form-control sign-up-input-2 "> <p class="terms-cond"><label for="terms_of_service" class="woo"><input type="checkbox" name="terms_of_service" id="terms_of_service" value="1" class="form-check-input" style="position: relative; margin-left: 0px;"> I agree to the <a target="_blank" href="//www.lambdatest.com/terms-of-service">Terms
of Service</a>
and <a target="_blank" href="//www.lambdatest.com/privacy">Privacy Policy</a></label></p> < button type="submit" class=" btn sign-up-btn-2 btn-block">Signup for Free< /button ></div>
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 class XpathUsingParent {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "path of chromedriver");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//accounts.lambdatest.com/register");
driver.manage().window().maximize();
//Finding the parent form usnig the password field as the current node.
WebElement parentDiv=driver.findElement(By.xpath("//input[@name='password']//parent::div"));
System.out.print(parentDiv.getAttribute("class"));
driver.close();
}
}
Console Output
7. Descendants: This approach is used to locate elements via XPath for all the children and sub children of the current node. For the below DOM structure, the descendants for XPath in Selenium are located as:
<div class="col-lg-3 col-md-4 col-sm-6 sign-form"><a href="//www.lambdatest.com"><img height="38" src="/images/logo.svg" class="img-responsive mb-4 mx-auto d-block"></a> <h1 class="title text-center font-weight-light">Hello! Welcome back</h1> <input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"> <input type="password" name="password" placeholder="Password" class="form-control mt-3 form-control-lg"> <button type="submit" class="btn btn-primary btn-lg btn-block mt-3">LOGIN</button> <div class="form-group form-check mt-2"><input type="checkbox" name="remember" id="remember" class="form-check-input"> <label for="remember" class="form-check-label">Remember Me</label></div> <div class="row mt-2"><div class="col pr-2 text-pass"><a href="//accounts.lambdatest.com/password/reset">Forgot Password?</a></div> <div class="col pl-2 text-pass text-right">No account? <a href="/register">Sign up</a></div></div></div>
import java.util.List;
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 class XpathUsingDescendants {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "path of chromedriver");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//accounts.lambdatest.com/login");
driver.manage().window().maximize();
//Finding the remember me grandchildren of the login form with field value as remember me.
WebElement rememberMe=driver.findElement(By.xpath("//div[@class='col-lg-3 col-md-4 col-sm-6 sign-form']//descendant::label"));
rememberMe.click();
System.out.print(rememberMe.getText());
driver.close();
}
}
Console Output
8. Ancestors: In this method the context node, parent or its grandparents are selected via the Ancestors axes. For example, for the below DOM structure the ancestor will be defined as:
<form method="POST" action="//accounts.lambdatest.com/login"><input type="hidden" name="_token" value="W4D7bCygQ4abq2Xa3ckZ2rwG3Wk7f9enPXIIPeuF"> <div class="row justify-content-sm-center"><div class="col-lg-3 col-md-4 col-sm-6 sign-form"><a href="//www.lambdatest.com"><img height="38" src="/images/logo.svg" class="img-responsive mb-4 mx-auto d-block"></a> <h1 class="title text-center font-weight-light">Hello! Welcome back</h1> <input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"> <input type="password" name="password" placeholder="Password" class="form-control mt-3 form-control-lg"> <button type="submit" class="btn btn-primary btn-lg btn-block mt-3">LOGIN</button> <div class="form-group form-check mt-2"><input type="checkbox" name="remember" id="remember" class="form-check-input"> <label for="remember" class="form-check-label">Remember Me</label></div> <div class="row mt-2"><div class="col pr-2 text-pass"><a href="//accounts.lambdatest.com/password/reset">Forgot Password?</a></div> <div class="col pl-2 text-pass text-right">No account? <a href="/register">Sign up</a></div></div></div></div></form>
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 class XpathUsingAncestor {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "path of chromedriver");
WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//accounts.lambdatest.com/login");
driver.manage().window().maximize();
//Finding the parent div using the password field of the login page.
WebElement parentDiv=driver.findElement(By.xpath("//input[@name='password']//ancestor::div[1]"));
System.out.println(parentDiv.getAttribute("class"));
WebElement parentForm=driver.findElement(By.xpath("//input[@name='password']//ancestor::form"));
System.out.println(parentForm.getAttribute("action"));
driver.close();
}
}
Console Output
Previously published at