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?
Asked
Active
Viewed 1.9k times
14

David
- 15,750
- 22
- 90
- 150
-
Did you try using the `.getAttribute("script")` on your `element`? – Mark Rowlands Dec 10 '13 at 15:04
-
Thanks, yes, but it doesn't work. – David Dec 10 '13 at 15:15
2 Answers
26
The best you can do is use GetAttribute
and access it's innerHTML
.
element.GetAttribute("innerHTML");

Arran
- 24,648
- 6
- 68
- 78
-
2Regardless 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