Selenium Webdriver:对于Firefox和PhantomJS,HTML的呈现方式不同 [英] Selenium Webdriver: HTML renders differently for Firefox vs. PhantomJS

查看:150
本文介绍了Selenium Webdriver:对于Firefox和PhantomJS,HTML的呈现方式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Node JS中的Selenium Webdriver来执行Google搜索。当我在本地机器上将浏览器设置为Firefox时,Google结果页面按预期呈现;这与我作为人类进行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版本构建。 显然 PhantomJS 2.x使用WebKit 538.x,这使它等效于Safari 7或8 。相比之下,我目前的PhantomJS 1.9.8 使用 WebKit 534.34,它相当于Safari 5。



Google将Safari 5确定为旧浏览器,因此可能会以不同的方式呈现其搜索页面。 (我认为JavaScript可能无法运行 - 除非您明确禁用它)。



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


I am using Selenium Webdriver in Node JS to do a Google search. When I set the browser as Firefox on my local machine, the Google results page renders as expected; it's the same as I see when I do the Google search as a human.

Now, I'm trying to do the same on my Heroku server. I can't seem to get Firefox on the server, so I'm using PhantomJS. It successfully does the Google search, but some data is missing from the page (I presume it is added later by Javascript).

How can I make the PhantomJS results page look the same as Firefox? Can I make PhantomJS appear to be 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);
});

解决方案

One quick win is to equalise your window dimensions, as PhantomJS seems to prefer a tall strip rather than a decent rectangle by default.

PhantomJS uses WebKit for rendering, while Firefox uses Gecko, so there's an inevitable possibility of slight differences.

Different PhantomJS versions are also built against different WebKit versions. Apparently PhantomJS 2.x uses WebKit 538.x, which makes it equivalent to Safari 7 or 8. By contrast, my current PhantomJS 1.9.8 uses WebKit 534.34, which is equivalent to Safari 5.

Google determines Safari 5 to be an "old" browser and will therefore potentially render its search pages differently. (I don't think it's likely that JavaScript has failed to run - not unless you've explicitly disabled it.)

So upgrading to PhantomJS 2.x would doubtless reduce rendering differences vs. Firefox.

这篇关于Selenium Webdriver:对于Firefox和PhantomJS,HTML的呈现方式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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