SegFaulter
SegFaulter

Reputation: 61

Using Selenium to get Inner span text not working

I have the following code that's supposed to go through multiple web pages and get email addresses.

for value in data:
        driver.get(value)
        try:
            Email = driver.find_element_by_xpath("//*[@id='email']")
            print(colored(Email.get_attribute('innerHTML'),'green'))    
        except Exception:
            print(colored("No Email found for "+ value, 'red'))

This is the html:

<li id="email">
<span class="label">Email</span>
[email protected]
</li>

The issue is that I couldn't get the code to print only [email protected]. What gets printed out is <span class="label">Email</span>[email protected]

Any help is appreciated!

Upvotes: 1

Views: 137

Answers (1)

Guy
Guy

Reputation: 50809

The element contains the text of the child elements as well, you can remove the text of the span

email = driver.find_element_by_id('email')
label = email.find_element_by_class_name('label')
text = email.text.replace(label.text, '').strip()
print(colored(text,'green'))

Upvotes: 1

Related Questions