0

I m trying to get the price text (159$) from the following html using python selenium

<span class="a-price a-text-price a-size-medium apexPriceToPay" data-a-size="b" data-a-color="price">
    <span class="a-offscreen">$159.99</span>
    <span aria-hidden="true">$159.99</span>
</span>

price = driver.find_element_by_css_selector('span.a-offscreen')
price.text()

this getting "" not the desired price

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Your locator strategy works per the html code you provided. Are you looking for only `$159` value or are you looking for the text value as it is, i.e. `$159.99`? Also, is the class `a-offscreen' is redundant, I mean, when you locate the element with the CSS, `span.a-offscreen`, is it showing only one element (and pointing to the text in the html page) or are you seeing other elements first? – Anand Gautam Mar 18 '22 at 09:37

1 Answers1

0

You can use the below locator:

//span[@data-a-color='price']//span[@class='a-offscreen']

to get the first $159.99 from this HTML:

<span class="a-offscreen">$159.99</span>

Code:

time.sleep(2)
print(driver.find_element(By.XPATH, "//input[@name='fsuser']").text)

or

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@name='fsuser']"))).text)

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
cruisepandey
  • 28,520
  • 6
  • 20
  • 38