【问题标题】:Javascript void(0) click or execute using selenium pythonJavascript void(0) 点击或使用 selenium python 执行
【发布时间】: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


【解决方案1】:

我已经找到了解决这个问题的方法。实际问题不在于 .click() 函数。它绝对可以点击这种按钮。问题在于标签管理。

因为 href 是在新标签页中打开的,所以我不知道我必须将焦点切换到新标签页。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 2014-09-05
    • 2019-03-13
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多