2011-03-04 34 views
9

我一直在使用urllib2訪問網頁,但它不支持JavaScript,所以我看了一下Selenium,但即使閱讀過它的文檔,我也很困惑。Python中的硒

我下載了Firefox的Selenium IDE插件,我嘗試了一些簡單的東西。

from selenium import selenium 
import unittest, time, re 

class test(unittest.TestCase): 
    def setUp(self): 
     self.verificationErrors = [] 
     self.selenium = selenium("localhost", 4444, "*chrome", "http://www.wikipedia.org/") 
     self.selenium.start() 

    def test_test(self): 
     sel = self.selenium 
     sel.open("/") 
     sel.type("searchInput", "pacific ocean") 
     sel.click("go") 
     sel.wait_for_page_to_load("30000") 

    def tearDown(self): 
     self.selenium.stop() 
     self.assertEqual([], self.verificationErrors) 

if __name__ == "__main__": 
    unittest.main() 

我剛剛訪問wikipedia.org和類型太平洋在搜索領域,但是當我嘗試編譯它,它給了我很多的錯誤。

+1

這些錯誤是什麼? – MAK 2011-03-04 20:32:21

+0

我跑了你的腳本沒有問題。在運行腳本之前是否啓動了硒服務器? – unutbu 2011-03-04 20:42:35

+0

Selenium服務器?我只下載了Selenium Client Driver和Selenium IDE。 – sophiaw 2011-03-04 21:21:00

回答

6

如果運行在[Errno 111] Connection refused錯誤的腳本結果像這樣:

% test.py 
E 
====================================================================== 
ERROR: test_test (__main__.test) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/home/unutbu/pybin/test.py", line 11, in setUp 
    self.selenium.start() 
    File "/data1/unutbu/pybin/selenium.py", line 189, in start 
    result = self.get_string("getNewBrowserSession", [self.browserStartCommand, self.browserURL, self.extensionJs]) 
    File "/data1/unutbu/pybin/selenium.py", line 219, in get_string 
    result = self.do_command(verb, args) 
    File "/data1/unutbu/pybin/selenium.py", line 207, in do_command 
    conn.request("POST", "/selenium-server/driver/", body, headers) 
    File "/usr/lib/python2.6/httplib.py", line 898, in request 
    self._send_request(method, url, body, headers) 
    File "/usr/lib/python2.6/httplib.py", line 935, in _send_request 
    self.endheaders() 
    File "/usr/lib/python2.6/httplib.py", line 892, in endheaders 
    self._send_output() 
    File "/usr/lib/python2.6/httplib.py", line 764, in _send_output 
    self.send(msg) 
    File "/usr/lib/python2.6/httplib.py", line 723, in send 
    self.connect() 
    File "/usr/lib/python2.6/httplib.py", line 704, in connect 
    self.timeout) 
    File "/usr/lib/python2.6/socket.py", line 514, in create_connection 
    raise error, msg 
error: [Errno 111] Connection refused 

---------------------------------------------------------------------- 
Ran 1 test in 0.063s 

FAILED (errors=1) 

則解決方案是最有可能的,你需要得到硒服務器首先運行。

在下載SeleniumRC時,會發現一個名爲selenium-server.jar的文件(截至幾個月前,該文件位於SeleniumRC/selenium-server-1.0.3/selenium-server.jar)。

在Linux上,你可以用命令

java -jar /path/to/selenium-server.jar 2>/dev/null 1>&2 & 

你會發現關於如何設置服務器here更完整的說明在後臺運行硒服務器。

1

我建議你使用一個webdriver,你可以在這裏找到它:http://code.google.com/p/selenium/downloads/list。如果你想以編碼器的形式編寫測試(而不是使用鼠標),那麼這種情況會比你嘗試使用的RC版本更好,至少因爲它不會問你一個SeleniumRC Jar實例。您只需擁有瀏覽器的二進制文件或使用已安裝在系統上的那些文件,例如Firefox。

-1

我在我的項目中遇到了這個問題,發現問題出在幾個webdriver.get調用之間,它們之間的時間間隔非常短。我的修復不是延遲,只是刪除不需要的調用和錯誤消失。希望,它可以幫助別人。