Monday, February 27, 2017

Selenium WebDriver - How to Identify Web Elements Using Selenium XPath

תוצאת תמונה עבור ‪selenium Xpath‬‏The XML path language (XPath) is a language that we can use for investigating specific nodes of an XML document based on specific search criteria.

Selenium WebDriver supports the XML path language as a strategy to locate elements using flexible XPath queries, this fact is very important, because all the major browsers (Firefox, IE, and chrome) support it.

In Addition: 
  • Not like the CSS strategy, XPath can identify parent element using his chilled element.
  • XPath is the least preferable strategy to locate elements due to a reduce performance when compared to other strategies.
  • A single / slash at the beginning of the XPath, instructs the XPath locator to search for element starting from the root node.
  • A double // slash at the beginning of the XPath, instructs the XPath locator to search the element anywhere. 
For more information about XPath, you can use the following address: 

XPath expressions that we can use to locate elements


Finding elements with absolute path







Firefox.Navigate().GoToUrl("http://www.machtested.com/");             
WebElement = Firefox.FindElement(By.XPath("/html/body/div[3]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div/div/ul/li[3]/a "));

Finding elements with relative path

Using the element relative path, we can locate the element directly and without the need to provide the full location in the DOM.

In addition, when using a relative path, there are many elements that may be matched to the search query, therefore it will return the first element found in the DOM.

Example:
Firefox.Navigate().GoToUrl("http://www.machtested.com/");  
WebElement = Firefox.FindElement(By.XPath("//li[3]/a"));

Finding elements based on index

To overcome the "First element returned" issue that we saw in the previous example, we can the element Index that will be used as another filter.

Example:
Firefox.Navigate().GoToUrl("http://www.machtested.com/");  
WebElement = Firefox.FindElement(By.XPath("//li[3]"));

Finding elements using attributes

We can locate elements using their attributes in XPath (Similar to the same option in CSS).

Example:









We will search for the input element (In that case it’s the name field) with the following attributes:
ID attributes:
WebElement = Firefox.FindElement(By.XPath("//input[@id='ContactForm1_contact-form-name']"));

Name attribute:
WebElement = Firefox.FindElement(By.XPath("//input[@name='name']"));

Size attributes:
WebElement = Firefox.FindElement(By.XPath("//input[@size='30']"));

Using Logical operators to filter elements

In some scenarios, you will need to use more than one attribute to locate an element, using logical operators such as 'Or'/ 'AND' will help you to achieve better results in the element filtration process.

Examples:
Case 1 (Or):
WebElement = Firefox.FindElement(By.XPath("//input[@size='30' or @name='name']"));

Case 2 (And) :
WebElement = Firefox.FindElement(By.XPath("//input[@size='30' and @name='name']"));

Locate element regarding the type of the attribute (* Wildcard)

We can use the * wildcard to search based on an attribute value, the important thing is that the search is made regarding the attribute type.

Example:
If we search for the string "Email", XPath will query all elements and their attributes to validate if there is any attribute value that matched to it.

HTML Code:
<input style="width:140px" name="email" type="text">

Code:
WebElement = Firefox.FindElement(By. XPath("//input[@*='email']")); 

No comments:

Post a Comment

My Presentations