'python selenium nested span element

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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cruisepandey