0

I'm trying to pull the text from a title off a specific webpage The tag I'm trying to target looks like this:

<h1 class="d2l-page-title d2l-heading vui-heading-1 bsi-set-solid">TEXT HERE</h1>

I can verify that my set XPATH is correct because all the other elements I asked it to print are correct. This is my output:

timer active

h1
<selenium.webdriver.chrome.webdriver.WebDriver (session="d9de1b525830fdf573c314afaa1001f1")>
{'y': 166.0, 'x': 21.0}
{'width': 419, 'height': 48}
DONE!

This is my script NOTE: you can find some of my failed experiments littered around in comments.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("user-data-dir=/Users/michael/Desktop/selenium")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://website.com")
xpx1 = "/html/body/div[2]/div/div[1]/div/a"
xpx2 = "/html/body/div/div[2]/div/form/input[1]"
xpx3 = "/html/body/div/div[2]/div/form/input[2]"
xpx4 = "/html/body/div/div[2]/div/form/input[3]"
# bs page
xpx5 = "/html/body/div[3]/div[2]/div[1]/div/div[2]/div/div[1]/div/div/div/h1"
xpx6 = "/html/head/meta[12]"

uni = driver.find_element_by_xpath(xpx1).click()
#uni.click()
username = driver.find_element_by_xpath(xpx2).send_keys("XXXXXX")
password = driver.find_element_by_xpath(xpx3).send_keys("XXXXXX")
submit = driver.find_element_by_xpath(xpx4).click()


print("timer active")
driver.implicitly_wait(8)
titlec = driver.find_element_by_xpath(xpx5)


for element in driver.find_elements_by_xpath(xpx5):
    print element.text
    print element.tag_name
    print element.parent
    print element.location
    print element.size
#linkc = driver.find_element_by_xpath(xpx6)
#print(driver.find_element_by_xpath(xpx5).getText()
#print(titlec)
print("DONE!")


1
  • 1
    Try withprint element.get_attribute('textContent') Commented Mar 31, 2020 at 13:22

1 Answer 1

2

You can try to get the text content using the below.

print element.get_attribute('textContent')
Sign up to request clarification or add additional context in comments.

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.