简体   繁体   中英

Javascript void(0) click or execute using selenium python

I am trying to scrape a website using selenium and 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. 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. 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.

Here is the html of the button:

<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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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