我正在尝试获取以下信息:'Jarrow配方,叶酸甲酯,400 mcg,60粒素食胶囊'
您可以查看图片,非常感谢:
我使用了这段代码,但没有成功:
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
要打印文本 value
,可以使用以下两种定位策略之一:
使用xpath
和文字属性:
print(driver.find_element_by_xpath("//section[@class='column image-fixed']//following::section[2]//div[@id='product-summary-header']//h1[@id='name']").text)
使用xpath
和get_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
理想情况下,您需要为引入WebDriverWait,visibility_of_element_located()
并且可以使用以下两种定位策略之一:
使用xpath
和文字属性:
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)
使用XPATH
和get_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
您可以在如何使用Selenium检索WebElement的文本中找到相关的讨论-Python
链接到有用的文档:
get_attribute()
方法 Gets the given attribute or property of the element.
text
属性返回 The text of the element.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句