1

How can I extract the value from a html file in this form using selenium:

<body>
   <div class="some stuff">
      <span class="some other stuff">the number wanted</span>
   </div>
</body>

I tried using the get_attribute function on the element but that can only return the class.

Code so far

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("website")
elem = driver.find_element_by_xpath('xpath to wherever')
print(elem.get_attribute("value")) #tried originally
driver.close()

Also important to note that the website must be signed in the particular website I was trying wouldnt work for most people. Thanks for any help.

3
  • Can we see your code so far? Commented Mar 29, 2020 at 22:42
  • added code now, with generalised bits Commented Mar 29, 2020 at 22:45
  • I've added an answer, if it doesn't work let us know. Commented Mar 29, 2020 at 22:48

2 Answers 2

1

If your data is within the < span >, you can use .text to get the inner text of the element.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("website")
elem = driver.find_element_by_xpath('xpath to wherever')
print(elem.text) #Get the inner text of elem
driver.close()
Sign up to request clarification or add additional context in comments.

Comments

1

Other approach you can use .get_attribute("innerHTML"), it can be used to get text inside the span tag.

While .get_attribute ("value") can be used for other component type such as input

Comments

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.