3

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.

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
sulav_lfc
  • 772
  • 2
  • 14
  • 34

2 Answers2

3

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());

Output snapshot

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

Community
  • 1
  • 1
Vicky
  • 2,999
  • 2
  • 21
  • 36
  • I thought OP is looking for a solution for `Selenium` which does not support `jquery` selector currently – Saifur Mar 02 '15 at 14:09
  • @Saifur Hi saifur...selenium supports jquery(provided jquery is used/loaded in the DOM otherwise we can inject it into DOM and use it) i almost use jquery in all my Test scripts...I have attached a screenshot of the output which i got using jquery...pls refer the screenshot attached in the above post – Vicky Mar 02 '15 at 19:22
  • I apologize. I misunderstood what you meant. And, with `executeScript` of course it can be accomplished. Good idea! – Saifur Mar 02 '15 at 19:40
1

The reason the xpath doesn't work is due to two reasons:

  1. In python the selenium xpath method doesn't support '/text()' in the xpath statement. I think you can use it as a condition to select the DOM element but not to return the text.
  2. The xpath is too broad for your use case. You need to deselect the children from the parent div.

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')
Diaz
  • 241
  • 1
  • 3