Locating Element By Xpath - Part 2

Xpath using Single Attribute
Try to locate a particular web element by XPath- Using Single Attribute. 
Syntax:
//tagname[@attribute='value']
Or
//*[@attribute=’value’]

TagName: If you want particular HTML Tag the then you should mentioned tagname after the double slash (//).

‘*’ : If you want matches any HTML tag then you should use ‘*’ after the  double slash(//). 

Scenario :
Enter User Name and Password  
Click on the Login button.
Following is the steps to locate the element using Relative XPath on webpage.
Right click on Email or Phone web element on the page and select inspect option in the context menu.
Press CTRL+F then you will get search box for xpath there you should frame your Xpath.
After framing the Xpath , if element is found then you will get the count of the elements in the right corner of the search box(1 of 1) else 0 of 0.
same way you have to follow for other elements (Password and Login button).

Script:

from selenium import webdriver
from selenium.webdriver.common.by import By
chromePath=
"C:\\BrowserDriveres\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.facebook.com/")
driver.find_element(By.XPATH,
"//input[@type='email']").send_keys("Hello")
driver.find_element_by_xpath(
"//*[@name='pass']").send_keys("hello")
driver.find_element(By.XPATH,
"//input[@value='Log In']").click()


Multiple Attribute using by Xpath:
In some situations single attribute is not enough to identify the web element on the page, in such cases you should go for multiple attribute.
Syntax: //tagname[attribute1=value1][attribute2=value2][..]
Or
//*[attribute1=value1][attribute2=value2][..]

I will try to locate email web element using xpath with class attribute.
But here you will get two web elements with same attribute , in such cases we will go for multiple attributes
Script:

from selenium import webdriver
from selenium.webdriver.common.by import By
chromePath=
"C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.facebook.com/")
driver.find_element(By.XPATH,
"//input[@class='inputtext'][@name='email']").send_keys("Hello")
driver.find_element_by_xpath(
"//*[@class='inputtext'][@name='pass']").send_keys("hello")
driver.find_element(By.XPATH,
"//input[@value='Log In'][@type='submit']").click()

Using ‘And’ in Xpath
In AND expression, two conditions are used, both conditions should be true to find the element. It fails to find element if any one of the condition is false.
Syntax:
//tagname[attribute1=value1 and attribute2=value2]
Or
//*[attribute1=value1 and attribute2=value2]

we will try to locate the Email or Phone Text box by XPath- Using And. we are using two attributes type and data-testid for finding the webelement on the page 
Ex: //input[@type='email' and @data-testid='royal_email']
Script:

from selenium import webdriver
from selenium.webdriver.common.by import By
chromePath=
"C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.facebook.com/")
driver.find_element(By.XPATH,
"//input[@type='email' and @data-testid='royal_email']").send_keys("Hello")
driver.find_element_by_xpath("//*[@type='password' and @data-testid='royal_pass']").send_keys("hello")
driver.find_element(By.XPATH,"//input[@value='Log In' and @type='submit']").click()



Using ‘OR’ in Xpath 
In OR expression, two conditions are used, if any one of the condition is true or both are true to find the element. It fails to find the element if both the conditions are false.

Syntax:
//tagname[attribute1=value1 or attribute2=value2]
Or
//*[attribute1=value1 or attribute2=value2]

we will try to locate the Email or Phone Text box by XPath- Using OR. we are using two attributes type and data-testid for finding the webelement on the page, in this case it will find the element If any one of the attribute matches

Ex: //input[@type='email' or @data-testid='royal_email']
Script: 
from selenium import webdriver
from selenium.webdriver.common.by import By
chromePath=
"C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.facebook.com/")
driver.find_element(By.XPATH,
"//input[@type='email' or @data-testid='royal_email']").send_keys("Hello")
driver.find_element_by_xpath("//*[@type='password' or @data-testid='royal_pass']").send_keys("hello")
driver.find_element(By.XPATH,"//input[@value='Log In' or @type='submit']").click()


Using ‘Contains()’ in Xpath 
Contains() is a method used in XPath expression. It is used to identify an element when the value of any attribute changes dynamically, for example, login , logout  or any labels information. The contain feature has an ability to find the element with partial text.

Syntax: //tagname[contains(@attribute,attribute value)]
Or
//*[contains(@attribute,attribute value)]

In this example, we will try to locate to identify the element by using partial text value of the attribute. In the below XPath expression partial value 'mail' is used in place of email web text box.

The value of the name attribute is ‘email’ , but using partial value ‘mail’
Ex://input[contains(@name,’mail’)]

Like that you can use contains() method  for all attributes
Examples:
1)   //input[contains(@type,’mail’)]
2)   //*[contains(@data-testid,’_email’)
3)   //input[contains(@id,’mail’)]
4)   //*[contains(@href,'recover')]
Script:
from selenium import webdriver
from selenium.webdriver.common.by import By
chromePath=
"C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.facebook.com/")
driver.find_element(By.XPATH,
"//input[contains(@id,'mail')]").send_keys("Hello")
driver.find_element_by_xpath(
"//*[contains(@data-testid,'pass')]").send_keys("hello")
driver.find_element(By.XPATH,
"//*[contains(@data-testid,'login_button')]").click()

Using ‘Starts-with() in Xpath 
It is used to identify an element, when we know initial part of the attributes value (means starting with the specified text) of an element. It is used to find the element where some portion of attribute value changes dynamically.

Syntax:
//tagname[starts-with(@attribute,attributevalue)]
Or
//*[starts-with(@attribute,attributevalue)]

Ex: //a[starts-with(@title,’Already’)]

In below example, XPath finds those element whose 'title' starting with “Lightning” and print the text message.
Script: 
from selenium import webdriver
from selenium.webdriver.common.by import By
chromePath=
"C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.rediff.com/")
List=[]
List=driver.find_elements_by_xpath(
"//a[starts-with(@title,'Lightning')]")
for textmsg in List:
   
print(textmsg.text)


Using ‘last() in Xpath 
last() method" selects the last element from the specified tagname out of all element present.

I want to access las link from below example
If you frame xpath like this ,it will select 9 elements
//div[@id='SIvCob']/a

Below is the example  to access last element from the list
//div[@id='SIvCob']/a[last()]
Script:

from selenium import webdriver
from selenium.webdriver.common.by import By
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.google.com/")
print(driver.find_element_by_xpath("//div[@id='SIvCob']/a[last()]").text)
Now you want to access last but two element frame like this
//div[@id='SIvCob']/a[last()-2]

Using ‘position() in Xpath 
The position function returns a number equal to the context position from the expression evaluation context.

Syntax:
//tagname[position()=value] 
Ex: //a[position()=1]

A node's position in a context is not zero-based. The first node has a position of 1.
I want to access ‘Log in’ from the link elements using position() method using Xpath. will locate link element  which is located on 2nd position from the top.
//ul[@class='uiList pageFooterLinkList _509- _4ki _703 _6-i']/li[position()=2]
Script:

from selenium import webdriver
from selenium.webdriver.common.by import By
chromePath=
"C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.facebook.com/")
print(driver.find_element_by_xpath("//ul[@class='uiList pageFooterLinkList _509- _4ki _703 _6-i']/li[position()=2]").text)

Few more example for Position():
Selects the first six link elements that are children of the 'uiList’  element

driver.find_element_by_xpath("//ul[@class='uiList pageFooterLinkList _509- _4ki _703 _6-i']/li[position()<6]")

You can use all relational operator for position() method
Ex 1:
//ul[@class='uiList pageFooterLinkList _509- _4ki _703 _6-i']/li[position()<=6]

Ex 2:
//ul[@class='uiList pageFooterLinkList _509- _4ki _703 _6-i']/li[position() > 6]

Ex 3:
//ul[@class='uiList pageFooterLinkList _509- _4ki _703 _6-i']/li[position() != 6]

Using ‘index’ in Xpath
By providing the index position in the square brackets, we could move to the nth element.

Syntax:
//tagname[index]

Ex: //input[4] It will select 4th input tag element from the top.

Note : you can use it for any tags
Ex: //a[5]
In the below script to print “Log In” link text using index
Script:

from selenium import webdriver
from selenium.webdriver.common.by import By
chromePath=
"C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.facebook.com/")
print(driver.find_element_by_xpath("//ul[@class='uiList pageFooterLinkList _509- _4ki _703 _6-i']/li[2]").text)

Comments