【问题标题】:Extract the Value of a webpage title by scraping the element通过抓取元素提取网页标题的值
【发布时间】:2021-12-23 05:38:30
【问题描述】:

我对 Python/Coding 很陌生,所以请与我交流。

但是,我正在尝试从网页标题中提取文本(由用户输入),方法是抓取页面的“webelement”并使用 Selenium 查找其值。

但是,它一直只返回值“none”,而不是我期望看到的值(在本例中为“BLACK BELTED WRAP COAT”。

代码如下:


title = driver.find_elements(By.XPATH,('/html/body/div[4]/div/div[3]/div[4]/div[1]/div[1]/form/div/div[2]/a/h2'))

//其余代码隐藏,但如果您需要更多,请告诉我。 (我是新手,不想发垃圾邮件)


知道是什么原因造成的吗?

我输入的源网址是:https://www.riverisland.com/p/black-belted-wrap-coat-782866

这运行没有错误,但返回一个意外的值(如下图所示)。

enter image description here enter image description here

感谢它,如果我遗漏了什么,我们深表歉意。 姜

【问题讨论】:

  • 您是否仔细检查了您在其中提供的第二个参数?

标签: python selenium selenium-webdriver web-scraping


【解决方案1】:

如果您要查找元素,请使用 find_element 而不是 find_elementsfind_elements 将返回一个 webelements 列表。

试试下面的代码:

Imports required for Explicit waits
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver.get("https://www.riverisland.com/p/black-belted-wrap-coat-782866")

wait = WebDriverWait(driver,30)

# Click on Accept cookies
wait.until(EC.element_to_be_clickable((By.NAME,"accept-all"))).click()

title = wait.until(EC.visibility_of_element_located((By.XPATH,"//h2[@data-localize='Product_Title']")))
print(title.text)
BLACK BELTED WRAP COAT

【讨论】:

    【解决方案2】:

    要打印文本BLACK BELTED WRAP COAT,您可以使用以下任一Locator Strategies

    • 使用css_selectorget_attribute("innerHTML")

      print(driver.find_element(By.CSS_SELECTOR, "h2.product-title.ui-product-title").get_attribute("innerHTML"))
      
    • 使用xpathtext属性:

      print(driver.find_element(By.XPATH, "//h2[@class='product-title ui-product-title']").text)
      

    理想情况下,您需要为visibility_of_element_located() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

    • 使用CSS_SELECTORtext 属性:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[name='accept-all']"))).click()
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h2.product-title.ui-product-title"))).text)
      
    • 使用XPATHget_attribute("innerHTML")

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@name='accept-all']"))).click()
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[@class='product-title ui-product-title']"))).get_attribute("innerHTML"))
      

    控制台输出:

    BLACK BELTED WRAP COAT
    
    • 注意:您必须添加以下导入:

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

    您可以在How to retrieve the text of a WebElement using Selenium - Python找到相关讨论


    参考文献

    链接到有用的文档:

    【讨论】:

    • 这对我有很大帮助,我已经使用了: driver = webdriver.Chrome() title = print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, " //h2[@class='product-title ui-product-title']"))).get_attribute("innerHTML")).但是,这会引发超时错误。 raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Stacktrace: Backtrace:
    • @tisGinge TimeoutException 是由于存在您需要单击的 Accept Cookie 按钮。现在我已经添加了一条线来处理它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2018-12-27
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    相关资源
    最近更新 更多