Home > Software design >  getting the text from attribute value in html
getting the text from attribute value in html

Time:11-23

I want to get the country code of the products form this website: https://www.skincarisma.com/products/olay/fresh-effects-s-wipe-out-refreshing-make-up-removal-cloths

here is the html

I tried

country = driver.find_element(By.XPATH,'//div[@]//img[@alt]').text

and

country = driver.find_element(By.XPATH,'//div[@]//img').text

but it failed to fetch (apparently because my code is wrong)

What should be the path?

trying: getting the coutnry code

CodePudding user response:

The text is contained in the alt attribute, therefore:

country = driver.find_element(By.XPATH,'//div[@]//img').get_attribute("alt")

CodePudding user response:

A more reliable way of getting that information would be:

[...]
wait = WebDriverWait(driver, 25)

url = 'https://www.skincarisma.com/products/olay/fresh-effects-s-wipe-out-refreshing-make-up-removal-cloths'

driver.get(url)
country = wait.until(EC.presence_of_element_located((By.XPATH, '//product-info//div[@]//img'))).get_attribute('alt')
print(country)

Result:

US
  • Related