如何在数组中循环url并在驱动程序Python Selenium中打印css选择器元素



想要找到每个产品的销售数量(数组中给定的URL(使用硒web驱动程序

这是我的代码

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.4k135.3k1.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。


最新更新