简体   繁体   中英

How to get text from span tag in Python

I have a python code and I don't use a function (just to inform you), which needs to copy only the text between TEXT, but what I'm getting is just NONE. I used:

email = driver.find_element_by_xpath('//*[@id="email"]').click()

email = driver.find_element_by_xpath('//*[@id="email"]')

email = driver.find_element_by_xpath('//*[@id="email"]').click()

[enter image description here][1]

[enter image description here][2]

I am new to Python, so if someone can explain to me how I do it and show the code already made, I will be grateful, because my goal here is to learn and not just copy and paste, understand?

I need to copy the emails that are inside that, as they change according to each access to the page.

NOTE: I have already researched the site and it even has something similar, but it is not the way I understand and program it. They are useless!

Examples [Code]: https://i.stack.imgur.com/L2CuH.png [Site]: https://i.stack.imgur.com/Wmgnn.png

email = driver.find_element_by_xpath('//*[@id="email"]').click()

email = driver.find_element_by_xpath('//*[@id="email"]')

email = driver.find_element_by_xpath('//*[@id="email"]').click()

The first time you create email , it contains None , because that's what .click() is obviously returning (in fact, it's returning nothing, which is converted by Python to None). Then, you overwrite email with the actual element object, but overwrite it a second time with None . What you should do is, get the element, .click() it, get the email into another variable, and .click() it again:

email_element = driver.find_element_by_xpath('//*[@id="email"]')
email_element.click()
email = email_element.text
email_element.click()
print(email)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM