Wednesday, August 21, 2019

selenium - xpath

ps:
single slash / : absolute path,from root
double slash //: relative path,from anywhere

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



  • DOM

The HTML DOM (Document Object Model)
When a web page is loaded, the browser creates a Document Object Model of the page.
The HTML DOM model is constructed as a tree of Objects:
DOM HTML tree


  • XPath

defined as XML path. It is a syntax or language for finding any element on the web page using XML path expression. XPath is used to find the location of any element on a webpage using HTML DOM structure. The basic format of XPath is explained below with screen shot.
XPath contains the path of the element situated at the web page. Standard syntax for creating XPath is.

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

// : Select current node.
Tagname: Tagname of the particular node.
@: Select attribute.
Attribute: Attribute name of the node.
Value: Value of the attribute.

1) Basic XPath:
XPath expression select nodes or list of nodes on the basis of attributes like ID , Name, Classname, etc. from the XML document as illustrated below.

Xpath=//input[@name='uid']

2) Contains():
Contains() is a method used in XPath expression. It is used when the value of any attribute changes dynamically, for example, login information.
The contain feature has an ability to find the element with partial text as shown in below example.

Xpath=//*[contains(@id,'message')]

3) Using OR & AND:

Xpath=//input[@type='submit' and @name='btnLogin']

4) Start-with function:

Xpath=//*[starts-with(@id,'message')]

5) Text():

Xpath=//td[text()='UserID']

6) XPath relative

XPath=//*[@id='seleniumsetupTable']/tbody/tr[3]/td[2]

7) table

Predicates are numbers or HTML attributes enclosed in a pair of square brackets "[ ]" that distinguish a child element from its siblings. 

//table/tbody/tr[2]/td[2]
//table/tbody/tr[2]/td[2]/table/tbody/tr/td[2]
//table[@width=\"270\"]/tbody/tr[4]/td

8)How To Handle Dynamic Tables In Selenium Webdriver
https://www.guru99.com/handling-dynamic-selenium-webdriver.html

//To locate table.
  IWebElement mytable = driver.FindElement(By.xpath("html/body/table/tbody"));

  //To locate rows of table.
  IList < WebElement > rows_table = mytable.FindElements(By.tagName("tr"));

  //To calculate no of rows In table.
  int rows_count = rows_table.Count();

IWebElement tableElement = driver.FindElement(By.XPath("/html/body/table"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
IList<IWebElement> rowTD;
foreach (IWebElement row in tableRow)
{
   rowTD = row.FindElements(By.TagName("td"));

   if(rowTD.Count > 9)
   {
      if(rowTD[8].Text.Equals("Suspended") && rowTD[10].Text.Equals("Publishing Failed");
      //test failed
   }
}

9)Axes
A step consists of:

an axis (defines the tree-relationship between the selected nodes and the current node)
a node-test (identifies a node within an axis)
zero or more predicates (to further refine the selected node-set)

preceding, following,
parent,child
ancestor,descendant,
preceding-sibling,following-sibling

axisname::nodetest[predicate]


//xxxxx//following-sibling::td[1]
//xxxxx//preceding-sibling::td[@class='xx']//i[@value='xxxx']

10)Chained Xpath
//xxxx//xxxx//xxx

11) locate element in an array
(//xxx[xxx])[x]







No comments:

Post a Comment

API interview questions

  https://www.katalon.com/resources-center/blog/web-api-testing-interview-questions/ Top 50+ Web API Testing Interview Questions [Ultimate l...