Please consider the following code:
I need help clicking the button Quarterly on the web page.
#!/usr/bin/perl
use strict;
use WWW::Mechanize::Firefox;
my $mech = WWW::Mechanize::Firefox->
new
(
create => 1,
activate => 1,
launch => 'c:\Program Files (x86)\Mozilla Firefox\firefox.exe',
);
my $url = 'http://finance.yahoo.com/quote/AAPL/financials?p=AAPL';
$mech->get($url);
for (1..15)
{
last if $mech->xpath('//[@class="Fl(end)"]', all => 1);
sleep(1);
}
$mech->click_button(value => 'Quarterly');
The button is located:
<div class="Fl(end)" data-reactid="319">
<button class="P(0px) M(0px) C($actionBlue) Bd(0px) O(n)">
<div class="Fz(s) Fw(500) D(ib) Pend(15px) H(18px) C($finDarkLink):h Mend(15px) BdEnd Bdc($subTabNavGray) C($actionBlue)">
<span>Annual</span>
</div>
</button>
<div class="Fz(s) Fw(500) D(ib) Pend(15px) H(18px) C($finDarkLink):h Mend(15px) C($finDarkLink)">
<span>Quarterly</span>
</div>
</div>
The page initially loads Annual data, but I'm interested in the Quarterly data.
After the Quarterly data is loaded, I'm interested in capturing the content inside the table <div class="Mt(10px)"><table class="Lh(1.7) W(100%) M(0)"><tbody><tr class="Bdbw(1px) Bdbc($lightGray) Bdbs(s) H(36px)"><td class="Fw(b) Fz(15px)">
for fundamental analysis.
Any help would be greatly appreciated.
Thanks!
You cannot use click_button
to click something that's not in a form. Instead, you need to use click
. To get the right button, we can use an xpath expression.
$mech->click({ xpath => '//button[.//span[text()="Quarterly"]]' });
The expression looks complicated, but is not so bad.
//button[.//span[text()="Quarterly"]]
//button # the button element anywhere in the page
[ ] # that contains
. # relative to it's position
//span # a span element
[ ] # that contains
text()= # a text node
"Quarterly" # with the exact string "Quarterly"
It will give you exactly that one button, and click
will click it.
(Please note that I have only tested the xpath expression, not the actual Perl code).