72. Using | to combine XPath Statements






We can combine two XPath statements into a single XPath statement using  '|'  .

For example, if you have two XPath statements as shown below:

//p[@id='para1']

//p[@id='para2']

Now if you want to combine the above two statements into a single XPath statement, the you have to use | as shown below:

//p[@id='para1'] | //p[@id='para2']

Lets Implement This:

1. Open http://compendiumdev.co.uk/selenium/basic_web_page.html in Firefox Browser
2. In 'Firepath' tab of 'FireBug' options, enter //p[@id='para1']  XPath Statement into the XPath field and click on 'Eval' Button as shown below:


3. Ensure that the first paragraph element on the page got high lighted as shown below:


4. Now enter a different XPath statement //p[@id='para2'] into the XPath field and click on 'Eval' Button as shown below:


5. Ensure that the second paragraph element on the page got high lighted as shown below:


6. Now I would like to combine the above two XPath statements into a single XPath statement which identifies the first and second paragraph text elements. In order to combine two XPath statements we have to use | operator. So lets enter the combined XPath statement  //p[@id='para1'] | //p[@id='para2'] into the XPath field and click on 'Eval' button as shown below:


7. Observe that the first and second paragraph texts on the page got high lighted on the page, on using the above combine XPath statement as shown below:



Please comment below to feedback or ask questions.

XPath Optimization will be explained in the next post.




2 comments:

  1. Is it possible to link the Xpath and ID in a single line of code

    ReplyDelete
  2. Yes, we can provide ID details inside XPATH.

    ReplyDelete

Followers

Labels