How to get the contents of a script tag in Selenium

后端 未结 2 1599
日久生厌
日久生厌 2021-01-18 01:46

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?

相关标签:
2条回答
  • 2021-01-18 02:01

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

    element.GetAttribute("innerHTML");
    
    0 讨论(0)
  • 2021-01-18 02:19

    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.

    0 讨论(0)
提交回复
热议问题