Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get HTML code of a WebElement in Selenium

I am new at testing so my apologies in advance if my question sounds a bit primary.

I am using Selenium and Java to write a test.

I know that webElement.getAttribute("innerHTML"); brings me the innerHTML, for example for the element below:

<a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button" style="position: absolute; border-radius: 0px 0px 4px 4px;">
    <span class="ui-icon ui-icon-closethick">close</span>
</a>

it returns:

<span class="ui-icon ui-icon-closethick">close</span>

but I need something that brings me the inner attribute of WebElement "a", something like below:

href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button" style="position: absolute; border-radius: 0px 0px 4px 4px;"
like image 700
LoveLovelyJava Avatar asked Aug 26 '15 18:08

LoveLovelyJava


4 Answers

If you want the HTML of the element itself, you can use

webElement.getAttribute("outerHTML");

It will return the HTML of the element itself plus all the children elements. I'm not sure if that's exactly what you want. I don't think there is a way to just get the HTML of the selected element only.

like image 177
JeffC Avatar answered Nov 16 '22 10:11

JeffC


You can read innerHTML attribute to get source of the content of the element or outerHTML for source with the current element.

Example :- Now suppose your Element is as below

<tr id="myRow"><td>A</td><td>B</td></tr>

Inner element Output

<td>A</td><td>B</td>

Outer element Output

<tr id="myRow"><td>A</td><td>B</td></tr>

Live Example :-

http://www.java2s.com/Tutorials/JavascriptDemo/f/find_out_the_difference_between_innerhtml_and_outerhtml_in_javascript_example.htm

Below you will find the syntax which require as per different binding. Change the innerHTML to outerHTML as per required.

Python:

element.get_attribute('innerHTML')

Java:

elem.getAttribute("innerHTML");

C#:

element.GetAttribute("innerHTML");

Ruby:

element.attribute("innerHTML")

JS:

element.getAttribute('innerHTML');

If you want whole page HTML use below code :-

driver.getPageSource();
like image 20
Shubham Jain Avatar answered Nov 16 '22 12:11

Shubham Jain


webElement.getAttribute("href");
webElement.getAttribute("class");
.
.
.

or to get them all:

Object[] attr = ((JavascriptExecutor)seleniumdriver).executeScript("return arguments[0].attributes);", webElement);
like image 4
tdrury Avatar answered Nov 16 '22 10:11

tdrury


If we have this:

<a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"
style="position: absolute; border-radius: 0px 0px 4px 4px;">
<span class="ui-icon ui-icon-closethick">close</span></a>

and we need to get all attributes of "a" which will be this:

href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"
    style="position: absolute; border-radius: 0px 0px 4px 4px;"

We can use this code:

webElement.getAttribute("outerHTML").split(">")[0]

where webElement is "a".

Or more precisely:

String s = we.getAttribute("outerHTML");

s = s.substring(2, s.indexOf(">"));
like image 2
LoveLovelyJava Avatar answered Nov 16 '22 12:11

LoveLovelyJava