使用perl客户端使用BrowserMob Proxy和selenium web驱动程序捕获HTTP请求标头/参数

时间:2016-05-17 02:06:09

标签: perl selenium browsermob-proxy

我正在尝试使用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,则它无效。
  3. 但是,我只能捕获第一个请求。随后的硒驱动器发出的请求无法捕获。例如,以下代码仅捕获第一个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);
    

1 个答案:

答案 0 :(得分:0)

使用create_new_har代替new_har

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