Home > Back-end >  How do I get these tags in hover element using Python Selenium
How do I get these tags in hover element using Python Selenium

Time:11-02

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)?

CodePudding user response:

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])
  • Related