new; $driver->get-6ren">
gpt4 book ai didi

selenium - 如何使用 Selenium::Remote::Driver 抑制 "warning"?

转载 作者:行者123 更新时间:2023-12-03 20:25:13 24 4
gpt4 key购买 nike

no warnings;
use Selenium::Remote::Driver;

my $driver = Selenium::Remote::Driver->new;
$driver->get('https://www.crawler-test.com/');
$driver->find_element_by_xpath('//a[.="text not found"]');
我怎样才能得到上面的代码 不是 打印此警告:

Error while executing command: no such element: Unable to locateelement: //a[.="text not found"]


根据 docs ,如果没有找到元素,该函数会发出“警告”,但有 no warnings;在脚本中不会抑制它。
我怎样才能抑制这个“警告”?

最佳答案

使用 find_element而不是 find_element_by_xpath .前者抛出异常而不是发出警告。您可以使用以下包装器捕获这些异常:

sub nf_find_element {
my $node;
if (!eval {
$node = $web_driver->find_element(@_);
return 1; # No exception.
}) {
return undef if $@ =~ /Unable to locate element|An element could not be located on the page using the given search parameters/;
die($@);
}

return $node;
}


sub nf_find_elements {
my $nodes;
if (!eval {
$nodes = $web_driver->find_elements(@_);
return 1; # No exception.
}) {
return undef if $@ =~ /Unable to locate element|An element could not be located on the page using the given search parameters/;
die($@);
}

return wantarray ? @$nodes : $nodes;
}


sub nf_find_child_element {
my $node;
if (!eval {
$node = $web_driver->find_child_element(@_);
return 1; # No exception.
}) {
return undef if $@ =~ /Unable to locate element|An element could not be located on the page using the given search parameters/;
die($@);
}

return $node;
}


sub nf_find_child_elements {
my $nodes;
if (!eval {
$nodes = $web_driver->find_child_elements(@_);
return 1; # No exception.
}) {
return undef if $@ =~ /Unable to locate element|An element could not be located on the page using the given search parameters/;
die($@);
}

return wantarray ? @$nodes : $nodes;
}
nf代表“非致命”。
为 Selenium::Chrome 编写,但也适用于 Selenium::Remote::Driver。

关于selenium - 如何使用 Selenium::Remote::Driver 抑制 "warning"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62929471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com