driver.getPageSource()转换符号<到< [英] driver.getPageSource() converts signs < to <

查看:203
本文介绍了driver.getPageSource()转换符号<到<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WebDriver和Java获取页面源代码。使用FirefoxDriver我试图验证页面源代码中的一些文本,但是当我使用driver.getPageSource时,它将一些标志转换为<到$ lt;和>到>因为这对我来说是难以核实的内容。有人请指导我如何避免这种情况?

 < noscript> 
< div id =noScriptContainer>
< p> JavaScript未启用!要么你禁用它,要么你的浏览器不支持它。因此,您将无法查看我们的网页或使用我们的网站功能。请在您的浏览器设置中打开JavaScript,或者升级您的浏览器版本以使用我们的网站。 < / p为H.
< / div>
< / noscript> b


$ p $转换为= < NOSCRIPT>
& lt; div id =noScriptContainer& gt;
& lt; p& gt; JavaScript未启用!要么你禁用它,要么你的浏览器不支持它。因此,您将无法查看我们的网页或使用我们的网站功能。请在您的浏览器设置中打开JavaScript,或者升级您的浏览器版本以使用我们的网站。 &安培; LT; / P&安培; GT;
& lt; / div& gt;


解决方案

通常最好的做法是不要使用getPageSource )的WebDriver方法,而是使用JavaScriptExecutor通过javascript获取页面源代码。


$ b

 字符串pageSource =((JavaScriptExecutor)驱动程序).executeScript(return document.documentElement.outerHTML;)。toString(); 


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;

解决方案

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();

这篇关于driver.getPageSource()转换符号&lt;到&lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆