【问题标题】:Using Selenium to get information within the tag 'h1' and id使用 Selenium 获取标签“h1”和 id 中的信息
【发布时间】:2020-12-28 02:46:49
【问题描述】:

我正在尝试获取以下信息:“Jarrow Formulas, Methyl Folate, 400 mcg, 60 Veggie Caps”

您可以查看图片,非常感谢:

我使用了这段代码,但没有成功:

driver = webdriver.Chrome(chrome_path)
driver.get("https://www.iherb.com/c/Vitamin-B?sr=2")
wait = WebDriverWait(driver, 10)

item_name = list()

#close the pop up
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"svg[data-ga-event-action='list-close']"))).click()

#store all the links in a list
item_links = [item.get_attribute("href") for item in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".absolute-link-wrapper > a.product-link")))]

for item_link in item_links:
    driver.get(item_link)item_name.append(driver.find_element_by_css_selector('[id="name"]').text) #this code doesnt work

【问题讨论】:

  • 不要使用 [id="name"],而是尝试使用 #name 可能吗?

标签: python selenium xpath web-scraping webdriverwait


【解决方案1】:

要打印 文本 value,您可以使用以下任一 Locator Strategies

  • 使用xpathtext属性:

    print(driver.find_element_by_xpath("//section[@class='column image-fixed']//following::section[2]//div[@id='product-summary-header']//h1[@id='name']").text)
    
  • 使用xpathget_attribute()

    print(driver.find_element_by_xpath("//section[@class='column image-fixed']//following::section[2]//div[@id='product-summary-header']//h1[@id='name']").get_attribute("innerHTML"))
    
  • 控制台输出:

    Jarrow Formulas, Methyl Folate, 400 mcg, 60 Veggie Caps
    

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

  • 使用xpathtext属性:

    driver.get('https://ca.iherb.com/pr/Jarrow-Formulas-Methyl-Folate-400-mcg-60-Veggie-Caps/42778')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//section[@class='column image-fixed']//following::section[2]//div[@id='product-summary-header']//h1[@id='name']"))).text)
    
  • 使用XPATHget_attribute()

    driver.get('https://ca.iherb.com/pr/Jarrow-Formulas-Methyl-Folate-400-mcg-60-Veggie-Caps/42778')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//section[@class='column image-fixed']//following::section[2]//div[@id='product-summary-header']//h1[@id='name']"))).get_attribute("innerHTML"))
    
  • 控制台输出:

    Jarrow Formulas, Methyl Folate, 400 mcg, 60 Veggie Caps
    
  • 注意:您必须添加以下导入:

    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找到相关讨论


参考文献

链接到有用的文档:

【讨论】:

猜你喜欢
  • 2021-07-03
  • 2018-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
  • 2016-05-04
  • 1970-01-01
相关资源
最近更新 更多