Python Selenium无法提取内部文本

2024-10-02 10:32:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从特定网页的标题中提取文本 我试图瞄准的标记如下所示:

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

我可以验证我的集合XPATH是否正确,因为我要求它打印的所有其他元素都是正确的。这是我的输出:

timer active

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

这是我的剧本 注意:你可以在评论中找到一些我失败的实验

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!")



Tags: divbyhtmldriverseleniumbodyelementfind

热门问题