python - 如何在数组中循环url并在驱动程序中打印css选择器元素,Python Selenium
问题描述
想要使用 selenium web 驱动程序查找每个产品的销售数量(给定数组中的 url)
这是我的代码
urls=["https://shopee.com.my/Pantene-Shampoo-750ml-i.52968176.7669592698/","https://shopee.com.my/PureMAE-Ylang-Ylang-and-Grapefruit-Shampoo-250ML-i.238433989.4820842024/","https://shopee.com.my/Some-By-Mi-Cica-Peptide-Anti-Hair-Loss-Shampoo-285ml-Tonic-150ml-Treatment-50ml-i.18874494.3274522052/","https://shopee.com.my/-JULYME-Anti-Hair-Loss-Perfume-Hair-Shampoo-500ml-i.219158269.6815232839/"]
for url in urls:
driver.get(url)
print(driver.find_element_by_css_selector('div.aca9MM').text)
预期结果是:
3.4k 13 5.3k 1.9k
但我在第 5 行出错,只能打印出第一个 3.4k
- - - - - - - - - - - - - - 输出 - - - - - - - - - - - ---------
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:6: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
3.4k
---------------------------------------------------------------------------
NoSuchElementException Traceback (most recent call last)
<ipython-input-70-0468875b1355> in <module>()
3 for url in urls:
4 driver.get(url)
----> 5 print(driver.find_element_by_css_selector('div.aca9MM').text)
3 frames
/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
241 alert_text = value['alert'].get('text')
242 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here
--> 243 raise exception_class(message, screen, stacktrace)
244
245 def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div.aca9MM"}
(Session info: headless chrome=95.0.4638.69)
Stacktrace:
#0 0x5607a4253623 <unknown>
#1 0x5607a3f5ad43 <unknown>
#2 0x5607a3f905f0 <unknown>
#3 0x5607a3fc4337 <unknown>
#4 0x5607a3fad5fd <unknown>
#5 0x5607a3fc20ac <unknown>
#6 0x5607a3fad9e3 <unknown>
#7 0x5607a3f84c0c <unknown>
#8 0x5607a3f860d5 <unknown>
#9 0x5607a4277954 <unknown>
#10 0x5607a4286f6d <unknown>
#11 0x5607a4286c8b <unknown>
#12 0x5607a42875b2 <unknown>
#13 0x5607a42bfe8b <unknown>
#14 0x5607a4287811 <unknown>
#15 0x5607a426c831 <unknown>
#16 0x5607a4290218 <unknown>
#17 0x5607a42903aa <unknown>
#18 0x5607a42aa3bf <unknown>
#19 0x7ffad38736db <unknown>
解决方案
urls=["https://shopee.com.my/Pantene-Shampoo-750ml-i.52968176.7669592698/","https://shopee.com.my/PureMAE-Ylang-Ylang-and-Grapefruit-Shampoo-250ML-i.238433989.4820842024/","https://shopee.com.my/-JULYME-Anti-Hair-Loss-Perfume-Hair-Shampoo-500ml-i.219158269.6815232839/"]
driver = webdriver.Chrome('/Users/YOURNAME/Desktop/chromedriver')
#for every url in the list of urls
try:
for url in urls:
driver.get(url)
print(driver.find_element_by_css_selector('div.aca9MM').text)
except:
print("error")
3.4k
13
error
您提供的第三个 URL 上似乎没有 div.aca9MM 。
推荐阅读
- python - 作为文件夹中的音乐文件,将它们从 .ogg 转换为 .mp3 (pytnon)
- javascript - 锄头,您可以查看然后编辑 javascript 警报框的元素吗?
- javascript - 使用 Typescript 将嵌套的 Json 反序列化为类
- java - Spring Data JPA,休眠@Table
- javascript - React-Native 嵌套导航导致出现 2 个菜单
- c++ - 将值 caputring lambda 传递给需要函数指针的函数
- output - logstash 限制输出文件的大小
- kubernetes - Pod 在 Kubernetes 中启动但不工作
- c# - 使用 c# 发送包含嵌入图像(Base64)的电子邮件
- javascript - Node/NestJs:如何从方法中注入类?