For Testing
For Testing

Reputation: 421

driver.getPageSource() converts signs < to <

I am using WebDriver and java to get page source. Using FirefoxDriver I am trying to verify some text on the page source but when I use driver.getPageSource, its converting some of the signs such as < to $lt; and > to > because of which its hard for me to verify content.

Can someone please guide me how to avoid this?

<noscript>
    <div id="noScriptContainer">
       <p>JavaScript is not enabled! Either you have disabled it or your browser does not support it. Because of this, you will not be able to view our pages or use our site features. Please turn on JavaScript in your browser settings or upgrade your browser version to use our site. </p>
    </div>
</noscript>

Converted to =

<noscript>
     &lt;div id="noScriptContainer"&gt;
                &lt;p&gt;JavaScript is not enabled! Either you have disabled it or your browser does not support it. Because of this, you will not be able to view our pages or use our site features. Please turn on JavaScript in your browser settings or upgrade your browser version to use our site. &lt;/p&gt;
     &lt;/div&gt;

Upvotes: 1

Views: 1145

Answers (2)

Stan
Stan

Reputation: 3461

Yes, it's a problem for child elements. You can either use javascript as already told or url decode what you got and receive the initial source code.

String result = java.net.URLDecoder.decode(url, "UTF-8");

Upvotes: 0

aholt
aholt

Reputation: 2971

It is generally best practice to not use the getPageSource() method of WebDriver, but to rather use JavaScriptExecutor to get the page source through javascript.

String pageSource = ((JavaScriptExecutor)driver).executeScript("return document.documentElement.outerHTML;").toString();

Upvotes: 1

Related Questions