将PhantomJS与Selenium Webdriver和Python一起使用

时间:2022-01-13 01:51:13

I'm currently using Selenium Webdriver to do some validation on pages. The Webdriver is being driven by PhantomJS. I know that in PhantomJS, you can listen to the network using the example like the one below: (from https://github.com/ariya/phantomjs/wiki/Network-Monitoring).

我目前正在使用Selenium Webdriver在页面上进行一些验证。 Webdriver由PhantomJS驱动。我知道在PhantomJS中,你可以使用如下例子来收听网络:(来自https://github.com/ariya/phantomjs/wiki/Network-Monitoring)。

var page = require('webpage').create();
page.onResourceRequested = function (request) {
    console.log('Request ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function (response) {
    console.log('Receive ' + JSON.stringify(response, undefined, 4));
};
page.open(url);

How can I achieve this functionality within the Webdriver? Can I bind a function to the DesiredCapabilities?

如何在Webdriver中实现此功能?我可以将函数绑定到DesiredCapabilities吗?

1 个解决方案

#1


0  

What are you trying to achieve here? It is possible to inject javascript. So with that you could create a object that listens to the page and logs this into an object that you grab later on when you made some actions.

你想在这里实现什么目标?可以注入javascript。因此,您可以创建一个侦听页面的对象,并将其记录到您稍后在执行某些操作时抓取的对象中。

Ill give it a try but I'm not sure what phantomJS does.

我试试看,但我不确定phantomJS是做什么的。

browser.execute_script("    
var requests= [];
var received = [];
var page = require('webpage').create();
page.onResourceRequested = function (request) {
    requests.push('Request ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function (response) {
    received.push('Receive ' + JSON.stringify(response, undefined, 4));
};
page.open(url);");

Later (if your still on the same page) to get the requests:

稍后(如果您仍然在同一页面上)获取请求:

browser.execute_script("function(){return requests }()");

and the received connections:

和收到的连接:

browser.execute_script("function(){return received}");

#1


0  

What are you trying to achieve here? It is possible to inject javascript. So with that you could create a object that listens to the page and logs this into an object that you grab later on when you made some actions.

你想在这里实现什么目标?可以注入javascript。因此,您可以创建一个侦听页面的对象,并将其记录到您稍后在执行某些操作时抓取的对象中。

Ill give it a try but I'm not sure what phantomJS does.

我试试看,但我不确定phantomJS是做什么的。

browser.execute_script("    
var requests= [];
var received = [];
var page = require('webpage').create();
page.onResourceRequested = function (request) {
    requests.push('Request ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function (response) {
    received.push('Receive ' + JSON.stringify(response, undefined, 4));
};
page.open(url);");

Later (if your still on the same page) to get the requests:

稍后(如果您仍然在同一页面上)获取请求:

browser.execute_script("function(){return requests }()");

and the received connections:

和收到的连接:

browser.execute_script("function(){return received}");