1

I'm using Python and Selenium to parse a web page. I want to get the numbers 45, 77, and 298 from this element:

<li id="guru" rel="popover-srs" title="" 
data-content="<ul><li>Radicals<span>45</span></li><li>Kanji<span>77</span></li><li>Vocabulary<span>298</span></li></ul>" data-original-title="<div class='srs-logo guru'>
</div>">
<span>420</span>
Guru</li>

These numbers appear only when you hover over with the mouse. Even though I found the element like so:

print(driver.find_element(By.ID, 'guru').text)

I get just the output:

420
Guru

How do I get the numbers in the span values (45, 77, 298)?

1 Answer 1

1

The data you are looking for is inside the data-content attribute of that element, not a text content.
What you can do here is to get the data-content attribute value and then to extract these values form there.
Something like the following:

data_content = driver.find_element(By.ID, 'guru')get_attribute("data-content")
parts = data_content.split('<span>')
for index, part in enumerate(parts):
    if index > 0:
        part = part.replace('<span>','')
        partitions = part.split('</span>')
        print(partitions[0])
Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome, my friend :)

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.