Home > Net >  Selenium - Can not get url from product page
Selenium - Can not get url from product page

Time:10-04

I'm trying to get all information from this product's page.

Currently I can get name, brand name, price.. But I can't get the product url.

I was trying with:

product_name = driver.find_elements_by_xpath("//*[starts-with(@id, 'testId-pod-displaySubTitle-')]")

And then capture this like: product_name.get_attribute('href'), but without success.

Why I can't get this information from this attribute? what am I failing at?

CodePudding user response:

The products are wrapped inside a div with id testId-searchResults-products, so you can use to find all div elements within this div and then fetch the href from the a tag to get the results-

This piece of code (tested on a Mac with Selenium beta 4) returns all the product links

driver.get("https://www.falabella.com/falabella-cl/collection/ofertas-mujer-ropa-v2")
wait = WebDriverWait(driver,30)
wait.until(EC.visibility_of_element_located((By.ID,'testId-Dropdown-desktop-button')))

sectionclass = driver.find_element(By.ID,"testId-searchResults-products")
alldivs = sectionclass.find_elements(By.CSS_SELECTOR,"div.jsx-4001457643.search-results-4-grid.grid-pod")

for i in range(len(alldivs)):
    all_text = alldivs[i].find_element(By.CSS_SELECTOR,'div > a').get_attribute("href")

And the output is something link this

https://www.falabella.com/falabella-cl/product/15009660/Vestido-Fluido-Punos-Elasticos-Prarie-Mujer/15009666

https://www.falabella.com/falabella-cl/product/882018735/Chaqueta-Mujer/882018753

https://www.falabella.com/falabella-cl/product/881384821/Pantalon-de-Algodon-Mujer/881384856

. . . .

and so on.

CodePudding user response:

If you have product_name list like this :

product_name = driver.find_elements_by_xpath("//*[starts-with(@id, 'testId-pod-displaySubTitle-')]")

You could get product url using the ancestor a tag :

product_name = driver.find_elements_by_xpath("//*[starts-with(@id, 'testId-pod-displaySubTitle-')]")
for product in product_name:
    url = product.find_element_by_xpath(".//ancestor::a").get_attribute('href')
    print(url)
  • Related