我几个小时都在苦苦挣扎。我试图在选择列表中执行单击,并且似乎涉及onchange事件。我根本不懂Javascript。
基本上,我登录以下网站:www.guidestar.org
要使用高级搜索,您需要注册。所以我用丢弃的电子邮件帐户/密码做到了这一点。我到目前为止自动登录,然后转到高级搜索页面并选择我的值并获得我的搜索结果。
在此结果页面中,我点击其中一个"优化按类别","按收入"选择选项。我可以机械化并获得第一个值,例如:$ 100k - $ 500k(2),但选择可能不是我想要的,我需要以某种方式点击该值。如果我手动(没有脚本)点击该值,它会正确推进页面。
这是我到目前为止的代码,并注释了各种尝试。
非常感谢任何帮助。
use strict;
use warnings;
use Data::Dumper;
use WWW::Mechanize::Firefox;
use HTML::TreeBuilder 5 -weak; # Ensure weak references in use
binmode(STDOUT, ":utf8");
my $log = 'c:\perl_script_logs\script_log';
my $log_errors = 'c:\perl_script_logs\script_errors_log';
open STDOUT, '>', $log or die "Can't write to $!";
open STDERR, '>', $log_errors or die "Can't write to $!";
# login
my $login_page = 'https://www.guidestar.org/Login.aspx';
my $user = 'email@somewhere.com';
my $pw = '********';
my $mech = WWW::Mechanize::Firefox->new();
$mech->allow( javascript => 1 );
$mech->get($login_page);
$mech->field('ctl00$phMainBody$LoginMainsite$UserName', $user);
$mech->field('ctl00$phMainBody$LoginMainsite$Password', $pw);
$mech->click_button( value => 'Sign In', );
if ( $mech->success() ) {
print "Successful login\n";
} else {
print "Not a successful login\n";
}
# go to adv search page
my $adv_search = 'http://www.guidestar.org/AdvancedSearch.aspx';
$mech->get($adv_search);
$mech->select('ctl00$phMainBody$orgSearchConfiguration_state$gslbList', 'CA' );
$mech->select('ctl00$phMainBody$orgSearchConfiguration_categorydesc$gslbList', ['3', '4x19', '5', '6', '8x44']);
$mech->field('ctl00$phMainBody$orgSearchConfiguration_incometotal$tbMin', '500k');
$mech->click({ xpath => '//*[@id="ctl00_phMainBody_ctl08"]' });
# selecting fine, but need to click it somehow or click after select, or execute some JS here... or something.
#$mech->select('ctl00$phMainBody$incomenavigator$listNavValues', '[100001;500001>' );
# tried:
#$mech->click({ xpath => '//*[@id="ctl00_phMainBody_incomenavigator_listNavValues"]' });
#$mech->click({ xpath => '//*[@id="ctl00_phMainBody_incomenavigator_listNavValues"]/option[1]' });
#$mech->click({ xpath => '//*[@id="ctl00_phMainBody_incomenavigator_listNavValues"]/option[1]', synchonize => 0});
#my $click_here = $mech->xpath('//*[@id="ctl00_phMainBody_incomenavigator_listNavValues"]/option[1]', single => 1);
#$mech->click($click_here);
# maybe selector?
#my $select = $mech->selector('#ctl00_phMainBody_incomenavigator_listNavValues', single => 1);
#$select->__click();
以下是可能有所帮助的HTML:
</p><p id="ctl00_phMainBody_incomenavigator_divNavigator">
<label>
By
Income
</label>
<br />
<select size="6" name="ctl00$phMainBody$incomenavigator$listNavValues" onchange="javascript:setTimeout('__doPostBack(\'ctl00$phMainBody$incomenavigator$listNavValues\',\'\')', 0)" id="ctl00_phMainBody_incomenavigator_listNavValues" OnClick="MPNav(this)">
<option value="[100001;500001>" nameonly="$100K - $500K">$100K - $500K (2)</option>
<option value="[500001;1e+006>" nameonly="$500K - $1M">$500K - $1M (2140)</option>
<option value="[1e+006;2.5e+006>" nameonly="$1M - $2.5M">$1M - $2.5M (1946)</option>
<option value="[2.5e+006;5e+006>" nameonly="$2.5M - $5M">$2.5M - $5M (908)</option>
<option value="[5e+006;1e+007>" nameonly="$5M - $10M">$5M - $10M (591)</option>
<option value="[1e+007;>" nameonly="Greater than $10M">Greater than $10M (623)</option>
</select>
<input name="ctl00$phMainBody$incomenavigator$txtNavName" type="hidden" id="ctl00_phMainBody_incomenavigator_txtNavName" value="incomenavigator" />
<input name="ctl00$phMainBody$incomenavigator$txtNavModifier" type="hidden" id="ctl00_phMainBody_incomenavigator_txtNavModifier" value="incometotal" />
<input name="ctl00$phMainBody$incomenavigator$txtFieldName" type="hidden" id="ctl00_phMainBody_incomenavigator_txtFieldName" value="Income" />
<script type="text/javascript">
function MPNav(listbox) {
var navName = listbox.name;
var nameSplit = navName.split('$');
if (nameSplit.length >= 2) {
navName = nameSplit[nameSplit.length - 2];
}
var navSelection = listbox.options[listbox.selectedIndex].innerHTML;
var selSplit = navSelection.split(' (');
if (selSplit.length >= 1) {
navSelection = selSplit[0];
}
switch(navName)
{
case 'statenavigator':
mixpanel.track('Navigator', { 'State' : navSelection });
break;
case 'citynavigator':
mixpanel.track('Navigator', { 'CitySearched': navSelection });
break;
case 'incomenavigator':
mixpanel.track('Navigator', { 'Income': navSelection });
break;
case 'assetsnavigator':
mixpanel.track('Navigator', { 'Assets': navSelection });
break;
}
}
</script>
</p></fieldset>