made_in_india
made_in_india

Reputation: 2279

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

I need to test web site where button is formed at the bottom of the page after user scroll the page for two times.

I have written a small script to test if the required element is formed. the condition tested always return false even though required element is formed in the page.

 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);
     }
 }

Is there any way to test if the button element has been formed in the script?

Upvotes: 0

Views: 644

Answers (2)

Chankey Pathak
Chankey Pathak

Reputation: 21676

Use the below to find the element

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

If this doesn't return 0 you've found your element, then you could run below:

To check whether the element is displayed

$elem->is_displayed();

To check whether the element is hidden

 $elem->is_hidden();

Check out more methods at: https://metacpan.org/pod/Selenium::Remote::WebElement

Upvotes: 0

made_in_india
made_in_india

Reputation: 2279

All these function return 0 when element is not found.

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.

Upvotes: 0

Related Questions