I have to find element with “== $0” after end tag of “span”. Below is the HTML Code of element.
<div _ngcontent-c6="" class="col-12"> <span _ngcontent-c6="">Registration with prefilled user's data</span> </div>
Although while I have copied the html code it is removing “== $0” itself. so I am attaching image also.
I have tried to find out solution but it was not working. I have tried xpath that normally works like .//span[text()=’Registration with prefilled user’s data’], but no sucess. I just found that “we can access this element in chrome console with syntax ‘ $0’ and it is working fine there
but I do’t know how to find it with xpath and CSS or any recommended locator’s strategies in Selenium.
Note: Please don’t mention any work around say use className or css with class name like div.col-12 span as I knew already this. My problem is handling elements with == $0.
Advertisement
Answer
So the text, == $0
, is not exactly what you think it means. This is just a feature of Chrome dev tools, and not an actual element on the page. It’s a property used by dev tools that allows you to test scripts via console. This has been discussed here, and it does not affect Selenium’s ability to locate elements on the page.
The issue might be the selector that you are using, or possibly a hidden iframe
element higher in the DOM that is obscuring the element.
You can try this XPath:
//span[contains(text(), "Registration with prefilled user's data")]
I just swapped out the text()='text'
query with contains(text(), 'text')
which may account for any hidden whitespace within the span
element.
This XPath is correct for the span
element given there are no special cases on the page. So, if this does not work for you, it would be recommended to post the full HTML or link to the page you are automating, so that we can all help you better.