如何使用Selenium WebDriver和Python提取元素内的文本?

时间:2019-04-09 10:09:41

标签: python selenium selenium-webdriver xpath webdriverwait

抓取指定区域的文本。

网站:https://www.kobo.com/tw/zh/ebook/NXUCYsE9cD6OWhvtdTqQQQ

图片:

https://imgur.com/a/qK1uA9L

代码:

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

HTML:

<span translate="no">大塊文化</span>

4 个答案:

答案 0 :(得分:1)

您做错了事:

BookTitle[0].getWindowHandle()不想在这里做任何事情

只需尝试:

driver.find_element_by_css_selector("a[class='description-anchor']>span").text

答案 1 :(得分:1)

要从指定区域提取文本大块文化,您需要为visibility_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()
    
  • 控制台输出:

    大塊文化
    

答案 2 :(得分:0)

尝试以下代码。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver.get("https://www.kobo.com/tw/zh/ebook/NXUCYsE9cD6OWhvtdTqQQQ")
element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'a.description-anchor span[translate="no"]')))
print(element.text)

答案 3 :(得分:0)

您也可以使用

driver.find_element_by_css_selector('span[translate="no"]')

CSS选择器应该比XPath快

编辑根据DebanjanB评论进行编辑-谢谢