简体   繁体   English

Javascript void(0)单击或使用Selenium Python执行

[英]Javascript void(0) click or execute using selenium python

I am trying to scrape a website using selenium and python. 我正在尝试使用Selenium和python抓取一个网站。 However, I need to click on a button to show the required information. 但是,我需要单击一个按钮以显示所需的信息。 The website reduce points out of my account when ever the button click and I accept that. 只要单击按钮,网站就会减少我的帐户中的积分,我接受这一点。 I just want to automate the process with selenium and scrape the site. 我只想使用硒来自动化该过程并刮取网站。 I am very new to both python and selenium. 我对python和selenium都很陌生。 Can anyone help me on how to click such a button or executing the script? 有人可以帮助我如何单击这样的按钮或执行脚本吗?

My current code is: 我当前的代码是:

driver.find_elements_by_xpath("//*[@id='show_info_button']")

It does not return any errors, the button just wasn't clicked. 它不返回任何错误,只是未单击按钮。 I have also tried driver.execute_script. 我也尝试过driver.execute_script。 But I am not sure what is the function name I am trying to execute so I have posted the html of the button below. 但是我不确定我要执行的函数名称是什么,因此我在下面发布了按钮的html。

Here is the html of the button: 这是按钮的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;
                    });
                }
            });
        });

I have found out the solution to this problem. 我已经找到解决此问题的方法。 The actual problem is not with the .click() function. 实际的问题与.click()函数无关。 It can definitely click on this sort of button. 它绝对可以单击此类按钮。 The problem lies within tab management. 问题出在标签管理中。

Because the href is opened in a new tab, I did not know that i have to switch the focus to the new tab. 因为href是在新标签页中打开的,所以我不知道必须将焦点切换到新标签页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM