node.js - Selenium 网络驱动程序 : HTML renders differently for Firefox vs. PhantomJS

标签 node.js firefox heroku selenium-webdriver phantomjs

我正在 Node JS 中使用 Selenium Webdriver 进行 Google 搜索。当我在本地计算机上将浏览器设置为 Firefox 时,Google 结果页面将按预期呈现;这和我作为人类进行谷歌搜索时看到的一样。

现在,我正在尝试在我的 Heroku 服务器上执行相同的操作。我似乎无法在服务器上安装 Firefox,所以我使用 PhantomJS。它成功地进行了 Google 搜索,但页面中缺少一些数据(我认为它是稍后由 Javascript 添加的)。

如何使 PhantomJS 结果页面看起来与 Firefox 一样?我可以让 PhantomJS 看起来像 Firefox 吗?

var driver = new webdriver.Builder().forBrowser('phantomjs').build();

driver.get('http://www.google.com/ncr');
driver.findElement(By.name('q')).sendKeys('empire boston');
driver.findElement(By.name('btnG')).click();
driver.wait(until.titleIs('empire boston - Google Search'), 10000);

driver.getTitle().then(function(title) {
    console.log('Page title is: ' + title);
});

driver.getPageSource().then(function(html) {
    console.log("HTML: " + html);
});

最佳答案

一个快速的胜利是均衡窗口尺寸,因为默认情况下 PhantomJS 似乎更喜欢长条而不是像样的矩形。

PhantomJS 使用WebKit用于渲染,而 Firefox 使用 Gecko ,因此不可避免地存在细微差异。

不同的 PhantomJS 版本也是针对不同的 WebKit 版本构建的。 Apparently PhantomJS 2.x 使用 WebKit 538.x,这使得它 equivalent to Safari 7 or 8 。相比之下,我当前的 PhantomJS 1.9.8 uses WebKit 534.34,相当于 Safari 5。

Google 确定 Safari 5 是“旧”浏览器,因此可能 render its search pages differently 。 (我认为 JavaScript 不太可能无法运行 - 除非您明确禁用它。)

因此升级到 PhantomJS 2.x 无疑会减少与 Firefox 的渲染差异。

关于node.js - Selenium 网络驱动程序 : HTML renders differently for Firefox vs. PhantomJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35110145/

相关文章:

python - 在 Heroku 上部署 Python(2.7)/Django(1.5) 应用程序时出现数据库完整性错误

javascript - 路线 ('next' )在 ExpressJS 中做什么?

javascript - 在 Express 和 Node JS 中显示常见的 HTML

node.js - NoSQL 架构白皮书

html - 提交按钮 CSS 问题

node.js - Mongoose 无法连接到本地主机 : MongoError: failed to connect to server [127. 0.0.1:27017]

node.js - 在 Heroku 上部署 ember 应用程序

node.js - Nodemailer HTML 无法与 sendgrid 一起使用

javascript - 使用 Greasemonkey 用图像替换链接、图像?

firefox - 当浏览器不支持 http2 时回退到 http1.1