0

I am trying to scrape the text ("9/10") in the span contained in <span class"_1JRNOw">. I had thought the code below would do it but I get a Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@class='_1JRNOw']"} error.

driver.find_element_by_xpath("//span[@class='_1JRNOw']").text

Can anyone see what I've done wrong?

enter image description here

2 Answers 2

1

Try this one see if it works for you:

ele = driver.find_element_by_xpath("//*[@class='_1JRNOw']/span")
print("Text is: " + ele.text)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks enthusiasticCoder. It gives the same error unfortunately.
would it be possible to share the URL of the page? I might be able to help you more.
Thanks guys. I've spent most of the weekend trying to figure out why these were not working. It turns out that after logging in through an iframe the content was contained in an ordinary frame so switching back before running the statements did the job. Thanks again
1

If Glad text is going to remain same, not changing dynamically (Assumption).

You could create an xpath based on Glad that could fetch you 9/10.

xpath //span[text()='Glad']//following-sibling::span/span

and try it like this :

driver.find_element_by_xpath("//span[text()='Glad']//following-sibling::span/span").text

you could put some wait, or Explicit waits to have more stable automation script.

Update :

Use this xpath

(//div[@data-kib-type='oauth']//descendant::span)[1]

But you'd have to change the [1] to something else, may be [3] or [4]. Please do that in chrome dev tools.

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

4 Comments

Thank you @cruisepandey. Glad is dynamic unfortunately but the @ will not change.
hmm.. @ is actually of no use for us in xpath 1.0, I need to see more outer HTML to give you more reliable locator.
I've added more html. Thanks
Please see above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.