345. XPath Optimization Strategy Two - Use the combination of attributes to make the XPath more specific

We can use the combination of attributes in XPath statements to make the XPath more specific.

Using the combination of attributes in XPath statements will be explained in the below steps:

1. Open in Firefox Browser
2. Select HTML tab in FireBug options as shown below:

3. Right click on the 'Check box' option and select 'Inspect Element with FireBug' as shown below:

4. Ensure that the html code related to the Inspected Check box option got high lighted under the HTML tab as shown below:

5. Observe that the high lighted Check Box html code has two attributes, i.e. type and name  as shown below:

6. So now lets use both the attributes in XPath Statement to locate the check box option of the page by following the next steps.

7. Select 'Firepath' tab and enter the XPath Statement  //input[@type='checkbox' and @name='selected(1234)'] into the XPath field as shown below and click on 'Eval' button:

8. Observe that the check box option is high lighted on the page as shown below:

So to conclude we have used and boolean operator in XPath statement to use more than one attributes.

When to use more than one attributes in XPath Statements?

Case 1:

There may be cases where the elements cant be located using the single attribute, then we have to use more than one attributes in the XPath statements to identify the elements.

Case 2:

And also we can use more than one attributes in the XPath Statements even if the element can be located using a single attribute. By doing this our confidence on the XPath Statement in locating the elements increases. The more the attributes in XPath Statements, the more confident we will be that the XPath Statement will locate the elements without failure.

Please comment below to feedback or ask questions.

XPath Optimization Strategy Three - If there is really nothing to distinguish find the first unique element from its siblings and add the index value to it will be explained in the next post.

No comments: