我正在尝试从特定网页上提取标题中的文本 我尝试定位的标记如下所示:<h1 class="d2l-page-title d2l-heading vui-heading-1 bsi-set-solid">TEXT HERE</h1>我可以验证我设置的 XPATH 是否正确,因为我要求它打印的所有其他元素都是正确的。这是我的输出:timer activeh1<selenium.webdriver.chrome.webdriver.WebDriver (session="d9de1b525830fdf573c314afaa1001f1")>{'y': 166.0, 'x': 21.0}{'width': 419, 'height': 48}DONE!这是我的脚本 注意:你可以在评论中找到我的一些失败的实验。from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionschrome_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 pagexpx5 = "/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!")
添加回答
举报
0/150
提交
取消