14

I'm using Selenium with C#. I have code which returns me a script tag as an IWebElement. How do I get the content from it?

David
  • 15,750
  • 22
  • 90
  • 150

2 Answers2

26

The best you can do is use GetAttribute and access it's innerHTML.

element.GetAttribute("innerHTML");
Arran
  • 24,648
  • 6
  • 68
  • 78
  • 2
    Regardless of your age (and if you're not who I think you are, you're probably quite confused), this is absolutely spot on. Thanks. – David Dec 10 '13 at 15:23
  • @David, some say it's impossible to find someone who's younger than me ;) Glad it helped. – Arran Dec 10 '13 at 15:42
2

You can try this:

WebElement element = driver.findElement(By.tagName("script"));
String htmlCode = (String) ((JavascriptExecutor) driver).executeScript("return arguments[0].innerHTML;", element);

P.S. is in Java but you can do same in C# with some small edit.

Andrian Durlestean
  • 1,730
  • 1
  • 19
  • 30