Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Selenium with PhantomJS not able to click links correctly

by jaypal (Beadle)
on Jul 19, 2014 at 06:15 UTC ( [id://1094283]=perlquestion: print w/replies, xml ) Need Help??

jaypal has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am trying to do some automation where my perl script will open a webpage and click on some link, push a button and then click on another link and push a button.

Layout: All services are placed on the left side of the page. Each service has its own set of buttons. The services links is visible on every page. When the service is clicked, the right portion of the page changes to reflect stuff of the service clicked

Here is my perl script:

#!/usr/bin/perl use strict; use warnings; use Selenium::Remote::Driver; my $url = 'http://my_internal_url.com'; my $driver = Selenium::Remote::Driver->new( 'browser_name' => 'phantomjs', 'port' => '8089' ); $driver->get( $url ); $driver->find_element("//a[contains(.,'serviceone')]")->click(1); $driver->find_element("//input[\@value='Stop Service']")->click(1); $driver->find_element("//a[contains(.,'servicetwo')]")->click(1); $driver->find_element("//input[\@value='Get Status']")->click(1);

I can see from the logs that the script does work. It worked fine when I was testing it by having the driver click on just one service. Since that was not going to be the only purpose of this script (it will do several other things for a particular service, then select another service, do stuff for it and then another service...etc) I started testing with two services.

This led to the issue I am having. During some runs both buttons gets pushed for serviceone and during some runs both buttons gets pushed for servicetwo. Sometimes just one button gets pushed for any one service.

I can't seem to figure out why is it happening. I have added sleep, set_implicit_wait_timeout, pause but the issue prevails.

The webpage doesn't have url but javascript onclick events.

Looking forward to your wisdom.

Regards
Jaypal

Replies are listed 'Best First'.
Re: Selenium with PhantomJS not able to click links correctly
by Loops (Curate) on Jul 19, 2014 at 10:23 UTC
    Hi there,

    You can start with some basic debugging, instead of just using ->click(), first test that each find_element succeeds or not. Likely that will reveal the problem.

      Thanks Loops. The debugging didn't help as they correctly identified each service and it's corresponding buttons. However, I added a longer pause time before each trigger. That does seem to work, however, I feel the benefit of using phantomJS to speedup testing is getting overshadowed by these pauses.

      Thank you for your response and help though.

        That does seem to work, however, I feel the benefit of using phantomJS to speedup testing is getting overshadowed by these pauses.

        That is funny :) The reason to use phantomJS is so you get free browser with javascript support ... you know the difficult stuff ... there is nothing you can do if the javascript does network communication or other stuff -- it will take as long as it will take, at least you didn't have to reverse engineer the javascript

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1094283]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-10 05:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found