如何使用 Selenium 和 Python 打印隐藏的产品名称
How to print the hidden product names using Selenium and Python
有史以来第二次post。对这一切还很陌生。在 for 循环中进行简单的打印调用时遇到问题,不显示任何文本,但移动光标就像它那样。如果我检查长度,它与打印调用中移动的空格数相匹配 (12)
from selenium import webdriver
from selenium.webdriver.common.by import By
browser= webdriver.Chrome()
browser.get('https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dcomputers-intl-ship&field-keywords=&ref=nb_sb_noss&crid=JKZZMSQ4Q71E&sprefix=%2Ccomputers-intl-ship%2C114')
elem_list= browser.find_element(By.CSS_SELECTOR, "div.a-section.a-spacing-medium._octopus-search-result-card_style_apbSearchResultsContainer__bCqjb")
items = elem_list.find_elements(By.XPATH, '//span[@data-component-type="s-product-image"]')
for item in items:
print(item.text)
如果我运行
print(len(items))
我得到 12。但是一旦我 运行 上面提到的 for 循环,我只得到 12 个空格。我做错了什么?
更新
我认为问题在于数据组件类型是图像。但是在检查了所有元素之后,在我尝试执行的页面上显示项目列表的最佳做法是什么?
产品名称包含在alt
<img>
元素的属性。
解决方案
要使用 you have to induce WebDriverWait for visibility_of_all_elements_located() 提取产品名称,您可以使用以下任一 定位器策略 :
使用CSS_SELECTOR:
driver.get("https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dcomputers-intl-ship&field-keywords=&ref=nb_sb_noss&crid=JKZZMSQ4Q71E&sprefix=%2Ccomputers-intl-ship%2C114")
print([my_elem.get_attribute("alt") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "span[data-component-type='s-product-image']>a>div>img")))])
使用 XPATH:
driver.get("https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dcomputers-intl-ship&field-keywords=&ref=nb_sb_noss&crid=JKZZMSQ4Q71E&sprefix=%2Ccomputers-intl-ship%2C114")
print([my_elem.get_attribute("alt") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[@data-component-type='s-product-image']/a/div/img")))])
控制台输出:
['Apple Pencil (2nd Generation)', 'Sceptre 24" Professional Thin 75Hz 1080p LED Monitor 2x HDMI VGA Build-in Speakers, Machine Black (E248W-19203R Series)', 'Roku Streaming Stick 4K 2021 | Streaming Device 4K/HDR/Dolby Vision with Roku Voice Remote and TV Controls', 'Original HP 67XL Black High-yield Ink Cartridge | Works with HP DeskJet 1255, 2700, 4100 Series, HP ENVY 6000, 6400 Series...', 'Seagate Portable 2TB External Hard Drive Portable HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox - 1-Year Rescue Service (...', 'Logitech MK270 Wireless Keyboard and Mouse Combo for Windows, 2.4 GHz Wireless, Compact Mouse, 8 Multimedia and Shortcut K...', 'Original HP 67 Black/Tri-color Ink Cartridges (2-pack) | Works with HP DeskJet 1255, 2700, 4100 Series, HP ENVY 6000, 6400...', 'HP 24mh FHD Monitor - Computer Monitor with 23.8-Inch IPS Display (1080p) - Built-In Speakers and VESA Mounting - Height/T...', 'iPhone Charger, TAKAGI Lightning Cable 3PACK 6FT Nylon Braided USB Charging Cable High Speed Data Sync Transfer Cord Compa...', 'Original HP 63XL Black High-yield Ink Cartridge | Works with HP DeskJet 1112, 2130, 3630 Series; HP ENVY 4510, 4520 Serie...', 'Roku Express 4K+ 2021 | Streaming Media Player HD/4K/HDR with Smooth Wireless Streaming and Roku Voice Remote with TV Cont...', 'Logitech C920x HD Pro Webcam, Full HD 1080p/30fps Video Calling, Clear Stereo Audio, HD Light Correction, Works with Skyp...']
有史以来第二次post。对这一切还很陌生。在 for 循环中进行简单的打印调用时遇到问题,不显示任何文本,但移动光标就像它那样。如果我检查长度,它与打印调用中移动的空格数相匹配 (12)
from selenium import webdriver
from selenium.webdriver.common.by import By
browser= webdriver.Chrome()
browser.get('https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dcomputers-intl-ship&field-keywords=&ref=nb_sb_noss&crid=JKZZMSQ4Q71E&sprefix=%2Ccomputers-intl-ship%2C114')
elem_list= browser.find_element(By.CSS_SELECTOR, "div.a-section.a-spacing-medium._octopus-search-result-card_style_apbSearchResultsContainer__bCqjb")
items = elem_list.find_elements(By.XPATH, '//span[@data-component-type="s-product-image"]')
for item in items:
print(item.text)
如果我运行
print(len(items))
我得到 12。但是一旦我 运行 上面提到的 for 循环,我只得到 12 个空格。我做错了什么?
更新
我认为问题在于数据组件类型是图像。但是在检查了所有元素之后,在我尝试执行的页面上显示项目列表的最佳做法是什么?
产品名称包含在alt
<img>
元素的属性。
解决方案
要使用
使用CSS_SELECTOR:
driver.get("https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dcomputers-intl-ship&field-keywords=&ref=nb_sb_noss&crid=JKZZMSQ4Q71E&sprefix=%2Ccomputers-intl-ship%2C114") print([my_elem.get_attribute("alt") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "span[data-component-type='s-product-image']>a>div>img")))])
使用 XPATH:
driver.get("https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dcomputers-intl-ship&field-keywords=&ref=nb_sb_noss&crid=JKZZMSQ4Q71E&sprefix=%2Ccomputers-intl-ship%2C114") print([my_elem.get_attribute("alt") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[@data-component-type='s-product-image']/a/div/img")))])
控制台输出:
['Apple Pencil (2nd Generation)', 'Sceptre 24" Professional Thin 75Hz 1080p LED Monitor 2x HDMI VGA Build-in Speakers, Machine Black (E248W-19203R Series)', 'Roku Streaming Stick 4K 2021 | Streaming Device 4K/HDR/Dolby Vision with Roku Voice Remote and TV Controls', 'Original HP 67XL Black High-yield Ink Cartridge | Works with HP DeskJet 1255, 2700, 4100 Series, HP ENVY 6000, 6400 Series...', 'Seagate Portable 2TB External Hard Drive Portable HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox - 1-Year Rescue Service (...', 'Logitech MK270 Wireless Keyboard and Mouse Combo for Windows, 2.4 GHz Wireless, Compact Mouse, 8 Multimedia and Shortcut K...', 'Original HP 67 Black/Tri-color Ink Cartridges (2-pack) | Works with HP DeskJet 1255, 2700, 4100 Series, HP ENVY 6000, 6400...', 'HP 24mh FHD Monitor - Computer Monitor with 23.8-Inch IPS Display (1080p) - Built-In Speakers and VESA Mounting - Height/T...', 'iPhone Charger, TAKAGI Lightning Cable 3PACK 6FT Nylon Braided USB Charging Cable High Speed Data Sync Transfer Cord Compa...', 'Original HP 63XL Black High-yield Ink Cartridge | Works with HP DeskJet 1112, 2130, 3630 Series; HP ENVY 4510, 4520 Serie...', 'Roku Express 4K+ 2021 | Streaming Media Player HD/4K/HDR with Smooth Wireless Streaming and Roku Voice Remote with TV Cont...', 'Logitech C920x HD Pro Webcam, Full HD 1080p/30fps Video Calling, Clear Stereo Audio, HD Light Correction, Works with Skyp...']