noobcode0000
noobcode0000

Reputation: 243

How do I retrieve text from a text node in Selenium

So, essentially I want to get the text from the site and print it onto console. This is the HTML snippet:

<div class="inc-vat">
   <p class="price">
        <span class="smaller currency-symbol">£</span>
        1,500.00
        <span class="vat-text">&nbsp;inc. vat</span>    
   </p>
</div>

Here is an image of the DOM properties: enter image description here

How would I go abouts retrieving the '1,500.00'? I have tried to use self.browser.find_element_by_xpath('//*[@id="main-content"]/div/div[3]/div[1]/div[1]/text()') but that throws an error which says The result of the xpath expression is: [object Text]. It should be an element. I have also used other methods like .text but they either only print the '£' symbol, print a blank or throw the same error.

Upvotes: 1

Views: 337

Answers (1)

cruisepandey
cruisepandey

Reputation: 29372

You can use below css :

p.price

sample code :-

elem = driver.find_element_by_css_selector("p.price").text.split(' ')[1]
print(elem)

Upvotes: 1

Related Questions