Reputation: 23
Here is the HTML:
<div class="discount-promo" style="" xpath="1">-50%<span class="text">2a Unidad</span></div>
I extract OK text from span:
s = root.find_element_by_xpath('.//div[@class="discount-promo"]//span[@class="text"]')
s.text
'2a Unidad'
but for text inside div:
d = root.find_element_by_xpath('.//div[@class="discount-promo"]')
d.text
'-50%\n2a Unidad'
for this case I need to extract exactly "-50%", or whatever other value it takes, without having to use regex, just by XPath.
regards
Upvotes: 1
Views: 796
Reputation: 33361
This short code should give you the parent element text only:
parent_element = root.find_element_by_xpath('.//div[@class="discount-promo"]')
print(driver.execute_script('return arguments[0].firstChild.textContent;', parent_element).strip())
Upvotes: 0
Reputation: 29042
You can use this XPath-1.0 expression:
.//div[@class="discount-promo"]/text()[1]
Or, in a whole:
d = root.find_element_by_xpath('.//div[@class="discount-promo"]/text()[1]')
The output should be as expected.
Upvotes: 1