I have a html structure as follows.
<div id="description">
wanted text
<div class="text-smaller normal wine-user-description">
<a href = "/users/user1"> unwanted text</a>
</div>
</div>
I'm using selenium to open the url and extract the required text from above block. Below is the code
val = self.driver.find_element_by_xpath('//div[@id="description"]').text
But the above code returns all the text (both wanted and unwanted). I even tried
val = self.driver.find_element_by_xpath('//div[@id="description"]/text()').text
but i get some xpath error. This is the first time i'm using selenium and i'm having some hard times. It would be really helpful if someone could help me.
Try using the below jquery to get the text inside the first node
$('#description')[0].childNodes[0].nodeValue
I tried the above code with your HTML it worked for me.If jquery is not used in your site this will not work then you have to inject jquery into the DOM and then try it..For injecting jquery into the DOM view this article
String node_text=(String)((JavascriptExecutor)driver).executeScript("return $('#description')[0].childNodes[0].nodeValue");
System.out.println(node_text.trim());

I tried using java not python if u r using python then instead of using JavascriptExecutor use browser.execute_script for more information refer this post
The reason the xpath doesn't work is due to two reasons:
However, we can try to get the individual text without changing your code like this:
val =
self.driver.find_element_by_xpath('//div[@id="description"]').get_attribute('textContent')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With