2017-04-07 21 views
0

我想用selenium和python刮一個網站。不過,我需要點擊一個按鈕來顯示所需的信息。當按鈕點擊並且我接受時,網站會減少我的帳戶點數。我只是想用硒自動化這個過程並且刮掉這個網站。我對蟒蛇和硒都很陌生。任何人都可以幫助我如何點擊這樣的按鈕或執行腳本?Javascript void(0)點擊或執行使用硒python

我當前的代碼是:

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; 
        }); 
       } 
      }); 
     }); 
+0

感謝羅裏,我是新來的Stackoverflow:D。 –

+0

您的代碼是否行得通? xpath看起來很好,而不是find_elements,使用find_element_by_xpath –

+0

它不會返回錯誤,但按鈕沒有被點擊。我也重試了你的代碼,它返回了這個錯誤:find_element_by_xpath return self.find_element(by = By.XPATH,value = xpath) –

回答

0

我已經找到了解決這一問題。實際問題不在.click()函數中。它絕對可以點擊這種按鈕。問題在於標籤管理。

因爲href在新標籤中打開,我不知道我必須將焦點切換到新標籤。