5

I'm trying to get the value of the element "total-price" from this page.

My html looks like this:

<div class="data">
<div class="data-first">Ydelse pr. måned</div>
<div class="data-last">
<span class="total-price">[3.551 Kr][1].</span>
</div>
</div>

My code is as follows:

monthlyCost = driver.find_element_by_xpath("//span[@class='total-price']")
print monthlyCost.text

The strange thing is the property is present in the webelement.

enter image description here

However, if I try and print it or assign it to an object it comes out empty. Why?

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Frank
  • 197
  • 1
  • 2
  • 14

1 Answers1

7

When you debug it, you're actually adding a pause and unintentionally wait for the page to load.

Plus, the price is loaded dynamically with an additional XHR request and has an intermediate "xxx" value which is substituted with a real value later in the load process. Things are becoming more complicated since there are multiple elements with total-price class and only one of them is becoming visible.

I'd approach it with a custom Expected Condition:

from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC

class wait_for_visible_element_text_to_contain(object):
    def __init__(self, locator, text):
        self.locator = locator
        self.text = text

    def __call__(self, driver):
        try:
            elements = EC._find_elements(driver, self.locator)
            for element in elements:
                if self.text in element.text and element.is_displayed():
                    return element
        except StaleElementReferenceException:
            return False

The working code:

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://www.leasingcar.dk/privatleasing/Citro%C3%ABn-Berlingo/eHDi-90-Seduction-E6G')

# wait for visible price to have "Kr." text
wait = WebDriverWait(driver, 10)
price = wait.until(wait_for_visible_element_text_to_contain((By.CSS_SELECTOR, "span.total-price"), "Kr."))
print price.text

Prints:

3.551 Kr.
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195