Home > Enterprise >  Writing Xpath with attribute and text
Writing Xpath with attribute and text

Time:05-26

I'm a newbie to Selenium and I need to be able to xpath the following element with attribute and text.

I am using

//*[text()='Over Due'] 

but I need it with attribute also to narrow it down to the single element.

<div style="padding-right: 10px;"><mat-icon role="img"  style="vertical-align: bottom;" aria-hidden="true" data-mat-icon-type="font"> av_timer </mat-icon> Over Due </div>

CodePudding user response:

You can use attribute with your existing XPath like

//*[text()='Over Due' and @attribute='']

OR with child expression also

//div[text()='Over Due' and *[text()='some text']]

CodePudding user response:

Try the following XPath-1.0 expression which includes the text node and part of the attributes:

//div[normalize-space(text())='Over Due' and contains(@style,'padding-right: 10px;') and contains(mat-icon/@class,'mat-icon')]

If this does not suffice, add further contains(...) expressions.

  • Related