python web页面元素提取_如何使用Selenium WebDriver和Python提取元素内的文本?

抓取指定区域的文本.

图片:

码:

BookTitle = driver.find_elements_by_xpath('//p[@class="title product-field"]')

BookTitle[0].getWindowHandle()

HTML:

大塊文化

解决方法:

要从指定的区域提取文本大块文化,您需要为visible_of_element_located()引入WebDriverWait,并且可以使用以下解决方案:

>代码块:

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()

options.add_argument("start-maximized")

options.add_argument("--disable-extensions")

options.add_argument('disable-infobars')

driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')

driver.get('https://www.kobo.com/tw/zh/ebook/NXUCYsE9cD6OWhvtdTqQQQ')

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='電子書詳細資料']//following::ul[1]//li/a[@class='description-anchor']/span"))).text)

driver.quit()

>控制台输出:

大塊文化

标签:selenium,selenium-webdriver,xpath,webdriverwait,python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值