Thursday, June 10, 2021

Different ways to write XPATH




XPath is a technique in Selenium that allows you to navigate the structure of a webpage’s HTML. XPath is a syntax for finding elements on web pages. 
Using XPath in Selenium helps find elements that are not found by locators such as ID, class, or name. XPath in Selenium can be used on both HTML and XML documents. Below are the syntax of writing the XPath can help you to handle the dynamic element.

Different ways to write XPaths

•// TagName[@AttributeName='AttributeValue']

•//TagName[@AttributeName='AttributeValue']/*[@AttributeName='AttributeValue']

•//TagName[@AttributeName='AttributeValue' and @AttributeName='AttributeValue']

•//TagName[@AttributeName='AttributeValue' or @AttributeName='AttributeValue']

•//TagName[@AttributeName='AttributeValue']/TagName[n]/TagName[n]

 

Selects first child element --> /div/TagName[1]

Selects last child element --> /div/TagName[last()]

Selects the second to last child element --> /div/TagName[las()-1]

 

• //TagName[starts-with(@AttributeName,'AttributeValue’)]

• //TagName[contains(@AttributeName,'AttributeValue’)]

• //TagName[text()=’Value’]

 • //TagName[contains(text(), 'Value’)]

• //TagName[text()=’Value’]//preceding::TagName[n]

• //TagName[@AttributeName=’AttributeValue’]//preceding::TagName[n]

• //TagName[text()=’Value’]//following::TagName[n]

• //TagName[@AttributeName=’AttributeValue’]//following::TagName[n]

 • //TagName[@AttributeName=’AttributeValue’]//parent::div

• //TagName[@AttributeName=’AttributeValue’]//ancestor:div

• //h3[contains(text(), 'Value’)]/following-sibling::div/label/input

 • //div[div[p[contains(text(),’Value’)]]]/preceding-sibling::div/div/span[3]/span

No comments:

Post a Comment

How to install Java on EC2

***************************************** How to install Java on EC2 ***************************************** To be continued, In this post...

All Time Popular Post