2

Actually, I scrape asin on amazon webpage with this code:

asin = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "averageCustomerReviews"))).get_attribute("data-asin")

But i have a problem: when the product has not customer reviews, i get an error with this code: timeout, asin not found. I want to scrape asin on this part of page source code, for example:

https://www.amazon.fr/dp/B08HNBD7XX

enter image description here

Anyone know how to make for to scrape asin on this part of source code please?

Barry the Platipus
  • 9,594
  • 2
  • 6
  • 30
JohnDoe
  • 95
  • 7
  • If you used xpath you can use | to check for multiple different values in css selector its , so just grab that value by its td class and you should be fine. – Arundeep Chohan Dec 24 '21 at 05:06
  • Thanks for answer @Armande Chohan, you know how to build this code please? – JohnDoe Dec 24 '21 at 12:05

1 Answers1

1

Wrap up your code block in a try-except{} inducing WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategy:

try:
    asin = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, "averageCustomerReviews"))).get_attribute("data-asin")
    print("data-asin attribute was found")
    continue
except TimeoutException:
    print("data-asin attribute wasn't found")
    continue
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hello and thank you for your answer @DebanjanB. Unfortunately with your code, if I can't find asin, my code crashes. I absolutely have to get asin in this part of the source code, because it is present on every article page. – JohnDoe Dec 24 '21 at 12:01
  • @JohnDoe The code within this answer is just an improvemt to the logic which you wanted to implement. I don't see any element as `(By.ID, "averageCustomerReviews")` in the snapshot either. – undetected Selenium Dec 24 '21 at 13:26
  • try-except is already in place in my code. But i just want to know how to make for scrap asin in source code on snapshot. I've tested: ``` asin_n = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "(//td[@class='.prodDetAttrValue'])"))).text ``` But not work – JohnDoe Dec 24 '21 at 15:43