2

我是 Selenium 的新手,想知道是否有人能指出我正确的方向。

我正在尝试获取页面的页面源,但我注意到 IE 驱动程序返回的内容与 FirefoxDriver 不同。

此外,InternetExplorerDriver.getPageSource() 返回的字符串与我在 IE 上单击查看页面源时看到的字符串不同。

我正在运行 IE 8 和 Firefox 22。

对于此页面上的示例:http://stackoverflow.com/questions/16455217/webdriver-save-the-location-of-the-id-in-the-page

当我调用 getPageSource() 时,IE 返回了类似这样的内容。

"<HTML><HEAD><TITLE>selenium - Webdriver / Save the location of the ID in the page - Stack Overflow</TITLE><LINK rel="shortcut icon" href="https://cdn.sstatic.net/stackoverflow/img/favicon.ico"><LINK rel="apple-touch-icon image_src" href="https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png">

虽然 Firefox 返回了这个。

"<!DOCTYPE html>

<title>selenium - Webdriver / Save the location of the ID in the page - Stack Overflow</title>
<link href="https://cdn.sstatic.net/stackoverflow/img/favicon.ico" rel="shortcut icon" />
<link href="https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png" rel="apple-touch-icon image_src" />

IEDriver 有没有办法以与 FirefoxDriver 相同的方式返回 pageSource?

4

1 回答 1

6

不,这是因为该getPageSource方法不会像在浏览器中手动那样返回页面源,而是返回 DOM 的文本表示。JavadocgetPageSource更好地解释了它:

java.lang.String getPageSource()

获取最后加载页面的来源。如果页面在加载后被修改(例如,通过 Javascript),则不能保证返回的文本就是修改后的页面的文本。请查阅所使用的特定驱动程序的文档,以确定返回的文本是反映页面的当前状态还是 Web 服务器上次发送的文本。返回的页面源是底层 DOM 的表示:不要期望它以与从 Web 服务器发送的响应相同的方式进行格式化或转义。把它想象成艺术家的印象。

于 2013-07-19T03:47:36.730 回答