-1

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.

enter image description here

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

enter image description here

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.

Muzzamil
  • 2,823
  • 2
  • 11
  • 23

1 Answers1

4

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.

Community
  • 1
  • 1
CEH
  • 5,701
  • 2
  • 16
  • 40
  • Yes, I walk through this post https://stackoverflow.com/questions/36999739/what-does-0-double-equals-dollar-zero-mean-in-chrome-developer-tools. but I have faced this issue multiple time. Normal solutions not works if this '== $0' attached to element. Although I have checked there is no Iframe and it is not working with //span[contains(text(), 'Registration with prefilled user's data')] and this "//span[contains(., 'Registration with prefilled user's data')]" too – Muzzamil Nov 25 '19 at 15:17
  • 1
    This ==$0 does not affect Selenium -- it does not actually EXIST on the DOM. If you `print(driver.page_source)` it will not be there. I even ran test Selenium code where I open the browser, inspect element so "==$0" appears on the element, and still located it with no problem. There is something else in your code that is not included in this question, such as additional HTML or more of your code sample (besides just XPath you tried). If you want this issue solved then you should help us by including that. – CEH Nov 25 '19 at 15:28
  • 2
    @Christine, you may want to double check your replacement as you're using single quotes around the text, but the text also contains a single quote, _("user's")_, which may be throwing it off – MivaScott Nov 25 '19 at 16:02
  • @MivaScott Good call. XPath usually throws a specific error such as "path is not valid" in cases where quotations cause interference, but these quotation marks may be the cause of the issue. – CEH Nov 25 '19 at 16:31
  • @MivaScott I think single quote was the issue and I missed it completely. Thank you Christine for detailed answer. – Muzzamil Nov 26 '19 at 08:08