【发布时间】:2017-04-07 13:31:03
【问题描述】:
我正在尝试使用 selenium 和 python 抓取网站。但是,我需要单击一个按钮来显示所需的信息。当单击按钮时,网站会从我的帐户中减少点数,我接受。我只想用 selenium 自动化这个过程并抓取网站。我对 python 和 selenium 都很陌生。谁能帮助我如何单击这样的按钮或执行脚本?
我当前的代码是:
driver.find_elements_by_xpath("//*[@id='show_info_button']")
它不会返回任何错误,只是没有点击按钮。我也尝试过 driver.execute_script。但我不确定我要执行的函数名称是什么,所以我在下面发布了按钮的 html。
这是按钮的html:
<a href="javascript:void(0);" id="show_info_button" class="btn btn-big btn-danger text-center">Xem thông tin ứng viên</a>
<script type="text/javascript">
$(document).ready(function() {
$process = false;
$('#show_info_button').click(function() {
if (!$process) {
$process = true;
$.ajax({
url: 'https://www.timviecnhanh.com/ho-so-show-info-point/ajax',
type: 'GET',
dataType : 'json',
data: {
employer_id: '2677857',
resume_id: '4065839',
package: '100',
remain_point: '611',
remain_point_gift: '0',
resume_point: '2'
},
async: false,
success: function(response) {
if (typeof response != 'undefined') {
$('#remain_point').text(parseInt($("#remain_point").text(), 10) - parseInt($("#resume_point").text(), 10));
$('#show_info_point').html(response.info);
$('#show_info_point_hide').hide();
if (typeof response.dinhkem != 'undefined') {
$('#preview-attach').html(response.dinhkem);
$('#preview-attach-resume-meta').hide();
$dinhkem_base64 = response.dinhkem_base64;
}
$('#show_info_button').hide();
$('#send_mail').append("<a href='https://www.timviecnhanh.com/nha-tuyen-dung/lien-he-nguoi-tim-viec?id=4065839&job_id=&last_cache_name=' class='btn btn-primary btn-send-mail'>Gửi mail</a>");
$('#send_mail_contact').append("<a href='https://www.timviecnhanh.com/nha-tuyen-dung/lien-he-nguoi-tim-viec?id=4065839&job_id=&last_cache_name=' class='btn btn-primary btn-send-mail'>Gửi mail liên hệ</a>");
$('#btn_view_attach').hide();
} else {
alert('Lỗi, refresh lại trình duyệt.');
}
}
}).done(function() {
// $process = false;
});
}
});
});
</script>
$(document).ready(function() {
$process = false;
$('#show_info_button').click(function() {
if (!$process) {
$process = true;
$.ajax({
url: 'https://www.timviecnhanh.com/ho-so-show-info-point/ajax',
type: 'GET',
dataType : 'json',
data: {
employer_id: '2677857',
resume_id: '4065839',
package: '100',
remain_point: '611',
remain_point_gift: '0',
resume_point: '2'
},
async: false,
success: function(response) {
if (typeof response != 'undefined') {
$('#remain_point').text(parseInt($("#remain_point").text(), 10) - parseInt($("#resume_point").text(), 10));
$('#show_info_point').html(response.info);
$('#show_info_point_hide').hide();
if (typeof response.dinhkem != 'undefined') {
$('#preview-attach').html(response.dinhkem);
$('#preview-attach-resume-meta').hide();
$dinhkem_base64 = response.dinhkem_base64;
}
$('#show_info_button').hide();
$('#send_mail').append("<a href='https://www.timviecnhanh.com/nha-tuyen-dung/lien-he-nguoi-tim-viec?id=4065839&job_id=&last_cache_name=' class='btn btn-primary btn-send-mail'>Gửi mail</a>");
$('#send_mail_contact').append("<a href='https://www.timviecnhanh.com/nha-tuyen-dung/lien-he-nguoi-tim-viec?id=4065839&job_id=&last_cache_name=' class='btn btn-primary btn-send-mail'>Gửi mail liên hệ</a>");
$('#btn_view_attach').hide();
} else {
alert('Lỗi, refresh lại trình duyệt.');
}
}
}).done(function() {
// $process = false;
});
}
});
});
【问题讨论】:
-
感谢 Rory,我是 Stackoverflow 的新手:D。
-
您的代码有效吗? xpath 看起来不错,而不是 find_elements,使用 find_element_by_xpath
-
它不会返回错误,但只是没有点击按钮。我也重试了你的代码,它返回了这个错误:find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath)
标签: javascript jquery python selenium