1

This is my 2nd day into learning Selenium. I would like to extract text between these html tags called .

HTML Code Sample:

<div id="media-buttons" class="hide-if-no-js"/>

<textarea id="DescpRaw" class="ckeditor" name="DescpRaw" rows="13" cols="100" style="visibility: hidden; display: none;">

Cactus spines are produced from specialized structures 
called areoles, a kind of highly reduced branch. Areoles 
are an identifying feature of cacti. 

</textarea>
</div>

Required results:

Cactus spines are produced from specialized structures 
called areoles, a kind of highly reduced branch. Areoles 
are an identifying feature of cacti. 

I have tried with Selenium driver below, but it comes out empty.

String bodyhtml = driver.findElement(By.xpath("//textarea[@name='DescpRaw']")).getText();

Thank you!

  • Have you tried getting the `innerHTML` attribute? I think that's what you are wanting. Hint: If you open a browser's debug window, you can usually find the attribute you're looking for. On Chrome or FF, press the key. – Ron Norris Jul 21 '17 at 20:35

2 Answers2

1
String bodyhtml = driver.findElement(By.xpath("//textarea[@name='DescpRaw']")).getAttribute("innerHTML");

also I recommend using ID since it is available and it is faster.

String bodyhtml = driver.findElement(By.id("DescpRaw")).getAttribute("innerHTML");
Cavan Page
  • 525
  • 2
  • 12
0

A couple things...

  1. If an element has an ID, you should always prefer to use the ID. By HTML standards, it should be unique on the page so it is the ideal identifier for any element.

  2. The problem you are running into is that the TEXTAREA is hidden. You can tell this given style="visibility: hidden; display: none;" on the element. Selenium is designed to interact with the webpage as a user would. Any element that isn't visible, Selenium won't interact with. The ideal situation would be to figure out how to expose or make visible the textarea field... click some button/link/whatever and then get the text from it. With a TEXTAREA field you will likely need to .getAttribute("value") on the element.

    A couple alternatives to making the element visible are to use Javascript to grab the element text or to use .getAttribute("innerHTML") as others have suggested.

Community
  • 1
  • 1
JeffC
  • 22,180
  • 5
  • 32
  • 55