Perl Selenium::Remote::Driver 测试检查按钮元素是否形成?

Perl Selenium::Remote::Driver test to check button element is formed?

我需要测试在页面底部形成按钮的网站 用户滚动页面两次。

我写了一个小脚本来测试是否形成了所需的元素。 即使在页面中形成了必需的元素,测试的条件总是 return false。

 use Selenium::Remote::Driver;
 use Scalar::Util qw/blessed reftype/;

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

 while ( 1 ) {

     $query = $driver->find_element_by_xpath(q{//button[@class='button']});

     #to test the if the element is present
     if ( blessed($query) && $query->isa('Selenium::Remote::Driver') ) {

           $query->click;
           last;
     }
     else {

         #always goes into else loop
         #to go to the end of the webpage 
         my $script = q{window.scrollTo(0,document.body.scrollHeight);};
         my $elem = $driver->execute_script($script);
     }
 }

有什么方法可以测试脚本中是否已经形成按钮元素?

所有这些功能return 0 当找不到元素时。

Read the doc carefully.

find_element_by_xpath

These functions all take a single STRING argument: the locator
search target of the element you want. If the element is found, we
will receive a WebElement. Otherwise, we will return 0. Note that
invoking methods on 0 will of course kill your script.

使用以下查找元素

my $elem = $driver->find_element_by_xpath($locator);

如果这没有 return0 你已经找到你的元素,那么你可以 运行 下面:

检查元素是否显示

$elem->is_displayed();

检查元素是否隐藏

 $elem->is_hidden();

查看更多方法:https://metacpan.org/pod/Selenium::Remote::WebElement