Home > Software design >  Find Span Within Span Using Python and Selenium
Find Span Within Span Using Python and Selenium

Time:10-02

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

CodePudding user response:

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.

CodePudding user response:

Try this one see if it works for you:

ele = driver.find_element_by_xpath("//*[@class='_1JRNOw']/span")
print("Text is: "   ele.text)
  • Related