Reputation: 147
[enter image description here][1]
<mat-chip role="option" cdkdrag="" container="body" class="cdk-drag mat-chip mat-focus-indicator mat-primary mat-standard-chip mat-chip-with-trailing-icon ng-star-inserted" tabindex="-1" aria-disabled="false" style="" xpath="1"><div class="mat-chip-ripple"></div> Table data <mat-icon role="img" aria-hidden="false" aria-label="icon-drag-doubledot" class="mat-icon notranslate icon-drag material-icons mat-icon-no-color" data-mat-icon-type="font"></mat-icon><mat-icon role="img" matchipremove="" aria-hidden="false" aria-label="icon-close" class="mat-icon notranslate mat-chip-remove mat-chip-trailing-icon icon-close material-icons mat-icon-no-color" data-mat-icon-type="font"></mat-icon></mat-chip>
From the above HTML tags, i want to extract those Table Data text. I am unable to get the text using below xpath
//mat-chip[@role='option']//div
//mat-chip[@role='option']//div/following-sibling::text()[1]
while executing it's throwing an error 'Failed: invalid selector: The result of the xpath expression "//mat-chip[@role='option']//div/following-sibling::text()[2]" is: [object Text]. It should be an element.'
Can anyone please help
[enter image description here][2]
Upvotes: 1
Views: 736
Reputation: 4870
Selenium expects an element not a text()-node. So use this: You need this:
//mat-chip[@role='option']/div
Although I doubt that [@role='option'] has any benefits.
If you are certain that there will be only one div inside that mat-chip, you could speed up the XPath by adding [1], like this:
//mat-chip[@role='option']/div[1]
This tells the XPath engine to stop searching after it has found this first div.
Upvotes: 0
Reputation: 8978
That's right, the text belongs to this element //mat-chip[@role='option']
I checked it locally, all good. If doesn't work on your end then the problem somewhere else
P.S. proof
Upvotes: 0