-1

I am trying to get html content of an html tag using HtmlUnitDriver. The code i have written is:

WebElement table_element = wd.findElement(By.id("genericTableFormtable"));
String str=table_element.getAttribute("outerHTML");
int index=str.indexOf("active");

If I use ChromeDriver then i get the code in the str, but if I use HtmlUnitDriver then I get an exception in my further code as str is null. Please tell why this is happening

honey preet
  • 101
  • 2
  • 16
  • I suspect that there is no attribute outerHTML when you use the rendering engine HtmlUnitDriver. Why do you need outerHTML? – Smile4ever Oct 18 '16 at 07:12
  • i need a part of html code that's why i used outerHTML – honey preet Oct 18 '16 at 07:40
  • Please add to your question exactly what part from which string you need. – Smile4ever Oct 18 '16 at 08:03
  • I have added a new line of code to my question. See I am supposed get table's html code from my first two lines, then i just have to check whether I am having active keyword in that code or not. That's why i need its html code – honey preet Oct 18 '16 at 08:14

1 Answers1

0

Try innerHTML instead of outerHTML

WebElement table_element = wd.findElement(By.id("genericTableFormtable"));    
String str=table_element.getAttribute("innerHTML");
int index=str.indexOf("active");

Also, if active is an attribute, simply get it with getAttribute. To check if a link contains the attribute active, you could do something like this:

List<WebElement> table_links = wd.findElement(By.tagName("a"));
foreach(WebElement table_link in table_links){
    if(table_link.getAttribute("active") != null){
         // this link is active
    }
}

Or just with indexof:

List<WebElement> table_links = wd.findElement(By.tagName("a"));
foreach(WebElement table_link in table_links){
    if(table_link.getAttribute("innerHTML").indexOf("active") > -1){
         // this link is active
    }
}
Smile4ever
  • 3,491
  • 2
  • 26
  • 34
  • I am using HtmlUnitDriver. String str=table_element.getAttribute("innerHTML"); – honey preet Oct 18 '16 at 08:53
  • I am using HtmlUnitDriver. String str=table_element.getAttribute("innerHTML"). After execution of this line str is null – honey preet Oct 18 '16 at 08:54
  • Debug and see which attributes are inside table_element. – Smile4ever Oct 18 '16 at 12:32
  • why i need to check attributes. You are not getting my point. See table's html code is something like this.
    – honey preet Oct 18 '16 at 14:42
  • why i need to check attributes. You are not getting my point. See table's html code is something like this.
    I want all this html in a string returned by the method. If i use chromedriver then i get it with the same code i have written but if i use HtmlUnitDriver then i get null in the string
    – honey preet Oct 18 '16 at 14:44