2016-09-30 78 views
2

我想用Python來處理警報。我wuold喜歡做的是:如何使用Python處理警報?

  • 打開一個URL
  • 提交一個表單或如果發生在新頁面的警報

Javascript使這個點擊一些鏈接

  • 檢查使用PhantomJS,但我會甚至使用Python。

    下面是javascript代碼:

    文件test.js:

    var webPage = require('webpage'); 
    var page = webPage.create(); 
    
    var url = 'http://localhost:8001/index.html' 
    
    page.onConsoleMessage = function (msg) { 
        console.log(msg); 
    }  
    page.open(url, function (status) {     
        page.evaluate(function() { 
         document.getElementById('myButton').click()  
        });   
        page.onConsoleMessage = function (msg) { 
         console.log(msg); 
        }  
        page.onAlert = function (msg) { 
         console.log('ALERT: ' + msg); 
        };  
        setTimeout(function() { 
         page.evaluate(function() { 
          console.log(document.documentElement.innerHTML) 
         }); 
         phantom.exit(); 
        }, 1000); 
    }); 
    

    index.html文件

    <!DOCTYPE html> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
        <title></title> 
        <meta charset="utf-8" /> 
    </head> 
    <body> 
        <form> 
         <input id="username" name="username" /> 
         <button id="myButton" type="button" value="Page2">Go to Page2</button> 
        </form> 
    </body> 
    </html> 
    
    <script> 
        document.getElementById("myButton").onclick = function() { 
         location.href = "page2.html"; 
        }; 
    </script> 
    

    文件page2.html

    <!DOCTYPE html> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
        <title></title> 
        <meta charset="utf-8" /> 
    </head> 
    <body onload="alert('hello')"> 
    </body> 
    </html> 
    

    該作品;它檢測到page2.html上的警報。 現在,我做了這個python腳本:

    test.py

    import requests 
    from test import BasicTest 
    from selenium import webdriver 
    from bs4 import BeautifulSoup 
    
    url = 'http://localhost:8001/index.html'  
    
    def main(): 
        #browser = webdriver.Firefox() 
        browser = webdriver.PhantomJS() 
        browser.get(url) 
        html_source = browser.page_source 
        #browser.quit()  
        soup = BeautifulSoup(html_source, "html.parser") 
        soup.prettify()  
        request = requests.get('http://localhost:8001/page2.html') 
        print request.text  
        #Handle Alert  
    if __name__ == "__main__": 
        main(); 
    

    現在,我怎麼能檢查是否提醒。page2.html發生與Python?首先,我打開頁面index.html,然後打開page2.html。 我在一開始,所以任何建議將不勝感激。

    p.s. 我也測試過webdriver.Firefox(),但速度非常慢。 我也讀過這個問題:Check if any alert exists using selenium with python

    但它不起作用(下面是相同的前面的腳本加解答建議的解決方案)。

    .....  
    from selenium.webdriver.support.ui import WebDriverWait 
    from selenium.webdriver.support import expected_conditions as EC 
    from selenium.common.exceptions import TimeoutException 
    
    .... 
    
    def main(): 
        ..... 
        #Handle Alert 
        try: 
         WebDriverWait(browser, 3).until(EC.alert_is_present(), 
                 'Timed out waiting for PA creation ' + 
                 'confirmation popup to appear.') 
    
         alert = browser.switch_to.alert() 
         alert.accept() 
         print "alert accepted" 
        except TimeoutException: 
         print "no alert" 
    
    if __name__ == "__main__": 
        main(); 
    

    我得到的錯誤:

    「selenium.common.exceptions.WebDriverException:消息:無效 命令方法。」

  • 回答

    1

    PhantomJS使用GhostDriver實現電線的webdriver協議,這是它如何在Selenium中用作無頭瀏覽器。

    不幸的是,GhostDriver目前不支持警報。雖然它看起來像他們想的幫助來實現功能:

    https://github.com/detro/ghostdriver/issues/20

    你可能切換到PhantomJS的JavaScript版本或使用中的硒Firefox的驅動程序。

    from selenium import webdriver 
    from selenium.common.exceptions import NoAlertPresentException 
    
    if __name__ == '__main__': 
        # Switch to this driver and switch_to_alert will fail. 
        # driver = webdriver.PhantomJS('<Path to Phantom>') 
        driver = webdriver.Firefox() 
        driver.set_window_size(1400, 1000) 
        driver.get('http://localhost:8001/page2.html') 
    
        try: 
         driver.switch_to.alert.accept() 
         print('Alarm! ALARM!') 
        except NoAlertPresentException: 
         print('*crickets*') 
    
    +0

    嗨!感謝你的回答。我試圖運行您的解決方案,但它不起作用。它打開一個空白頁面,沒有任何反應。我錯過了什麼嗎? – d3llafr33

    +0

    腳本使用Python 3.5解釋器運行,那是你正在使用的?我建議複製並粘貼'__main__'下的代碼:以及import語句到你自己的腳本中。如果正確運行,它應該打開一個Firefox窗口到您的URL並將兩條消息之一打印到控制檯。 – jinksPadlock

    +0

    我正在使用python 2.7。我切換到python 3.5,但結果是一樣的。我也遵循你的建議。我使用硒v.2.53.6 – d3llafr33

    相關問題