Introduction to Xpath

What is XPath?

Identifying the element by using the XML path to the locator type is known as XPath.

Identifying the WebElement by “XPATH” locator. It takes String as an argument. If an element isn't identified it throws an exception as NoSuchElementException.

Syntax: driver.find_element(By.XPATH,"xpath_value")

We have two types of XPath

  • Absolute Xpath
  • Relative Xpath



Absolute XPath:

It uses a complete html root path to the required WebElement.

In below example we are trying to access edit box by using Absolute XPath.

Ex: "/html/body/div/div[2]/div[2]/div/div/div/div[2]/form/input[1]"


Relative Xpath:

It uses the direct path of a WebElement using (id,className,attribute values , sub-string,etc) to perform action on it.

Ex : "//*[@id='user_input']"

Single slash define '/'     :  finding the element inside the parent element
Double slash define  '//' :   finding the child or nested-child element inside the parent element

1. Write the Xpath using the tag name along with attribute and its value


EX : //tag[@attribute='value']

   "//input[@id='user_input']"
  


2. Write the Xpath to find the element in any tag using attribute and value by using “ * “ after the double slash “ // “.


EX : "//*[@attribute='value']"

    "//*[@id='user_input']"
  


3. Write the Xpath to find the element by partial value of attribute by using Contains in xpath ( Need to give partial value of attribute) :


Syntax : "//tag[contains(@attribute,'partial value of attribute')]"
 Ex :     "//input[contains(@id,'user')]"  , "//a[contains(text(),'Form')]"


4. Write the Xpath to find the element by Starts-With text in attribute value ( Need to give partial value) :

   Syntax: "//tag[starts-with(@attribute,'partial value of attribute')]"
   Ex :    "//input[starts-with(@id,'us')]"


5. Identify an element using a text attribute.


"//*[text()='text_value']"


Absolute_XPath.py

from selenium import webdriver
import time
from selenium.webdriver.common.by import By

driver = webdriver.Chrome("/Users/admin/Documents/Others/Drivers/chromedriver.exe")  # Using Chrome Driver

driver.get("http://dummypoint.com/seleniumtemplate.html")
time.sleep(2)

ele = driver.find_element(By.XPATH, "/html/body/div/div[2]/div[2]/div/div/div/div[2]/form/input[1]")
ele.send_keys("Code2Lead_Absolute_XPath")

# OR

driver.find_element(By.XPATH, "/html/body/div/div[2]/div[2]/div/div/div/div[2]/form/input[1]").send_keys("Skill2Lead_Absolute_XPath")

time.sleep(2)
driver.quit()
   


Relative_XPath.py

from selenium import webdriver
import time
from selenium.webdriver.common.by import By

driver = webdriver.Chrome("/Users/admin/Documents/Others/Drivers/chromedriver.exe")  # Using Chrome Driver

driver.get("http://dummypoint.com/seleniumtemplate.html")
time.sleep(2)

ele = driver.find_element(By.XPATH,"//*[@id='user_input']")
ele.send_keys("Relative_XPath")

# OR

driver.find_element(By.XPATH,"//*[@id='user_input']").send_keys("Relative_XPath")


time.sleep(2)
driver.quit()