Home > Blockchain >  How to loop url in array and print css selector element in driver, Python Selenium
How to loop url in array and print css selector element in driver, Python Selenium

Time:11-14

Wants to find the number of sold of each product (given urls in array) using selenium web driver

This is my code

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)

expected result is :

3.4k 13 5.3k 1.9k

but I get error in line 5 and can only print out the first one 3.4k

----------------------------output------------------------------

/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>

CodePudding user response:

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

Seems like there is no div.aca9MM on the third URL that you provided.


  • Related