7

I was wondering if there was a way to get the entire html code between two tags of an element, along with the element tag then store it in a string.

Lets say I use the following to create a list of web elements, then fill the list with all the web elements.

List<WebElement> element = driver.findElements(By.xpath("//*"));
//Some for loop after this to access each value

If I use the following to get the 3rd web element, it prints only the tags name, as it should:

System.out.println(element.get(3).getTagName()); 

so it prints the paragraph element "p" or "input" for example if it is the 3rd web element stored

But I was wondering if its possible to get the entire html code line for the web element and print it rather then only the tag name "p" for example?

e.g.

<p> some text </p>

Is there some way to accomplish this?

timbre timbre
  • 12,648
  • 10
  • 46
  • 77
Xians Wu
  • 159
  • 13
  • is using selenium a requirement in this case? for instance using javascript, it coulld be easier: http://stackoverflow.com/questions/2631019/how-to-print-entire-html-element-in-javascript – timbre timbre Mar 30 '17 at 22:03

1 Answers1

2

You can read outerHTML attribute to get the entire element.

element.getAttribute("outerHTML");

Or in your case:

System.out.println(element.get(3).getAttribute("outerHTML")); 

Hope it helps!

Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43