Home > front end >  How a write a common xpath for same text displayed for different html tags?
How a write a common xpath for same text displayed for different html tags?

Time:08-22

I want to write a common xpath for the result displayed for my searched text 'Automation Server'

The same text is displayed for 'td' html tags as well as for 'div' html tags as shown below, and I wrote XPath as below based on my understanding by going through different article

displayed_text = //td[contains(text(),'Automation Server') or div[contains(text(),' Automation Server ')]

<td role="cell" mat-cell="" >Automation Server</td>

<div > Automation Server </div>

CodePudding user response:

The OR-like operator in XPath is |.

The XPath you are looking for is

//td[contains(text(),'Automation Server')] | //div[contains(text(),'Automation Server')]

CodePudding user response:

A few alternatives to JeffC answer, using common properties for both:

1. use the * as a wildcard for any element:

//*[contains(@class,'ng-star-inserted') and normalize-space(text())='Automation Server']

2. use in addition the local-name() function to narrow down the names of the elements:

//*[local-name()[.='td' or .='div']][contains(@class,'ng-star-inserted') and normalize-space(text())='Automation Server']

The normalize-space() function can be used to clean-up the optional white space, so a = operator can be used.

CodePudding user response:

You could use the following XPath to test the local-name() of the element in a predicate and whether it's text() contains the phrase:

//*[(local-name() = "td" or local-name() = "div") and contains(text(), "Automation Server")]
  • Related