[英]Perl Mechanize Firefox click button (not in HTML form)
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. 加载季度数据后,我有兴趣捕获表格内的内容
<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)">
用于基本分析。
Any help would be greatly appreciated. 任何帮助将不胜感激。
Thanks! 谢谢!
You cannot use click_button
to click something that's not in a form. 您不能使用
click_button
单击不在表单中的内容。 Instead, you need to use click
. 相反,您需要使用
click
。 To get the right button, we can use an xpath expression. 要获得正确的按钮,我们可以使用xpath表达式。
$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. 它将为您提供一个按钮,然后
click
将单击它。
(Please note that I have only tested the xpath expression, not the actual Perl code). (请注意,我只测试了xpath表达式,而不是实际的Perl代码)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.