抓取指定区域的文本.
网站:https://www.kobo.com/tw/zh/ebook/NXUCYsE9cD6OWhvtdTqQQQ.
图片:
码:
BookTitle = driver.find_elements_by_xpath('//p[@class="title product-field"]')
BookTitle[0].getWindowHandle()
HTML:
<span translate="no">大塊文化</span>
解决方法:
要从指定的区域提取文本大块文化,您需要为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()
>控制台输出:
大塊文化