-1

I am trying to extract some information from the amazon website using selenium. But I am not able to scrape that information using xpath in selenium.

In the image below I want to extract the info highlighted. enter image description here

This is the code I am using

try:
    path = "//div[@id='desktop_buybox']//div[@class='a-box-inner']//span[@class='a-size-small')]"
    seller_element = WebDriverWait(driver, 5).until(
        EC.visibility_of_element_located((By.XPATH, path)))
except Exception as e:
    print(e)

When I run this code, it shows that there is an error with seller_element = WebDriverWait(driver, 5).until( EC.visibility_of_element_located((By.XPATH, path))) but does not say what exception it is.

I tried looking online and found that this happens when selenium is not able to find the element in the webpage.

But I think the path I have specified is right. Please help me.

Thanks in advance

[EDIT-1]

This is the exception I am getting

Message:
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sashaank
  • 880
  • 2
  • 20
  • 54

4 Answers4

2
//div[class='a-section a-spacing-none a-spacing-top-base']//span[class='a-size-small a-color-secondary']

XPath could be something like this. You can shorten this.

CSS selector could be and so forth.

.a-section.a-spacing-none.a-spacing-top-base
.a-size-small.a-color-secondary
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
1

I think the reason is xpath expression is not correct.

Take the following element as an example, it means the span has two class:

<span class="a-size-small a-color-secondary">

So, span[@class='a-size-small') will not work.

Instead of this, you can ues xpath as

//span[contains(@class, 'a-size-small') and contains(@class, 'a-color-secondary')]

or cssSelector as

span.a-size-small.a-color-secondary
Peter Quan
  • 788
  • 4
  • 9
0

Amazon is updating its content on the basis of the country you are living in, as I have clicked on the link provided by you, there I did not find the element you are looking for simply because the item is not sold here in India.

So in short if you are sitting in India and try to find your element, it is not there, but as you change the location to "United States". it is appearing there.

Solution - Change the location

Swaroop Humane
  • 1,770
  • 1
  • 7
  • 17
-1

To print the Ships from and sold by Amazon.com of an element you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and get_attribute():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.a-section.a-spacing-none.a-spacing-top-base > span.a-size-small.a-color-secondary"))).get_attribute("innerHTML"))
    
  • Using XPATH and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='a-section a-spacing-none a-spacing-top-base']/span[@class='a-size-small a-color-secondary']"))).text)
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


Outro

Link to useful documentation:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352