【问题标题】:What is the minimum perl script for Selenium?Selenium 的最小 perl 脚本是什么?
【发布时间】:2021-01-04 00:13:50
【问题描述】:

我使用自制软件安装了最新版本的 geckodriver 和 chromedriver。我更喜欢前者,但如果他们工作,我愿意使用任何一个。我已经使用 cpan 控制台安装了 Selenium::Remote::Driver 模块,并且它安装时没有任何警告。我目前正在使用 https://metacpan.org/pod/Selenium::Remote::Driver 上的示例代码 sn-ps 。

use Selenium::Remote::Driver;
 
my $driver = Selenium::Remote::Driver->new;
$driver->get('http://www.google.com');
$driver->quit();

当我尝试运行脚本时,我收到以下错误:

Selenium server did not return proper status at /Library/Perl/5.18/Selenium/Remote/Driver.pm line 544.

现在,我不知道自己在做什么。而且我正在从一些可能应该弃用的网络资源中工作。在开始之前,我是否需要手动运行 geckodriver(或 chromedriver)?如果没有,我是否需要至少指定在我的代码中调用哪个?两者都在我的路径环境中可用。我还没有安装第三个组件吗?可能是浏览器插件?

我唯一的目标是(此时)将它加载到浏览器中的位置(此时最好不要无头,以便我可以看到它在做什么)。

【问题讨论】:

标签: selenium perl selenium-webdriver


【解决方案1】:

虽然特定于 Chrome,但以下是最小的 Selenium 解决方案:

use FindBin          qw( $RealBin );
use Selenium::Chrome qw( );

my $web_driver = Selenium::Chrome->new(
   binary => "$RealBin/chromedriver.exe",
);

$web_driver->get('https://www.stackoverflow.com/');

$web_driver->shutdown_binary();

我想处理异常,所以我实际使用了这个:

use FindBin             qw( $RealBin );
use Selenium::Chrome    qw( );
use Sub::ScopeFinalizer qw( scope_finalizer );

my $web_driver;
my $guard = scope_finalizer {
   if ($web_driver) {
      $web_driver->shutdown_binary();
      $web_driver = undef;
   }
};

$web_driver = Selenium::Chrome->new(
   binary => "$RealBin/chromedriver.exe",
);

$web_driver->get('https://www.stackoverflow.com/');

【讨论】:

  • 非常感谢。事实证明,这个答案非常有帮助....我不得不从我的(Mac,而不是 Windows)中删除 .exe,但除此之外,这很完美。一些明显的替代品甚至让 Firefox/gecko 的东西也能正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-13
  • 2014-11-03
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多