1

我正在尝试使用 Selenium::Remote::Driverand Browsermob::Proxy 捕获 http 请求参数。伪代码如下。我无法获取 http 标头。

任何帮助,将不胜感激。

use Selenium::Remote::Driver;
use Data::Dumper;

my $proxy = Browsermob::Proxy->new(trace => 2);
my $driver = Selenium::Remote::Driver->new(
browser_name => 'firefox',
proxy        => $proxy->selenium_proxy(1)
);

$proxy->new_har( payload => {
        initialPageRef => '1' }, captureHeaders => 'true',
    captureContent => 'true',
    captureBinaryContent => 'true');

$driver->get('http://www.google.com/');
print Dumper($proxy->har);

更新:

  1. 模块 Net::HTTP::Spore::Role::Middleware 使用不推荐使用的方法(Class::MOP::load_class)。我将其更改为 Class::Load::load_class
  2. Selenium::Remote::Driver 的参数“httpProxy”必须是没有 URL 方案的特定 IP 地址。如果它是 $proxy->selenium_proxy,它就不起作用。

但是,我只能捕获第一个请求。无法捕获 selenium 驱动器发出的后续请求。例如,下面的代码只捕获第一个 GET 请求而不是第二个。

use Data::Dumper;
use Browsermob::Proxy;
use Selenium::Remote::Driver;

my $proxy = Browsermob::Proxy->new(
server_port => 8080,
);


$proxy->new_har( payload => {
        initialPageRef => 'payload is optional' }, captureHeaders => 'true',
    captureContent => 'true',
    captureBinaryContent => 'true', port => $proxy->port );

my $driver = Selenium::Remote::Driver->new(
browser_name => 'firefox',
proxy        =>  { 'proxyType' => 'manual', 'httpProxy' =>     '127.0.0.1:'.$proxy->port}, 
);
$driver->get('http://www.google.com/');
$driver->find_element("//input[\@name='q']")->send_keys("selenium");
$driver->find_element("//button[\@name='btnG']")->click;
print Dumper($proxy->har);
4

1 回答 1

0

使用create_new_har而不是new_har.

$proxy->create_new_har(
    payload => {
        initialPageRef => 'payload is optional'
    },
    captureHeaders => 'true',
    captureContent => 'true',
    captureBinaryContent => 'true'
);
于 2016-05-17T04:39:33.470 回答