2011-08-22 260 views
1

我是新來的硒蟒蛇。現在我試圖運行已經從硒轉換的Python代碼Ide。當我跑了代碼我得到了這個錯誤。我使用Eclipse來運行代碼IndexError:列表索引超出範圍

ERROR: test_isnin2 (__main__.Isnin2) 

====================================================================== 
ERROR: test_isnin2 (__main__.Isnin2) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "C:\EclipseWorkspaces\csse120\python\src\Isnin2.py", line 20, in test_isnin2 
    driver.get("https://stackoverflow.com/search?q=google&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a") 
    File "C:\Program Files\Python27\lib\site-packages\selenium-2.4.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 154, in get 
    self.execute(Command.GET, {'url': url}) 
    File "C:\Program Files\Python27\lib\site-packages\selenium-2.4.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 144, in execute 
    self.error_handler.check_response(response) 
    File "C:\Program Files\Python27\lib\site-packages\selenium-2.4.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 111, in check_response 
    zeroeth = value['stackTrace'][0] 
IndexError: list index out of range 

---------------------------------------------------------------------- 
Ran 1 test in 35.406s 

FAILED (errors=1) 

我的編碼如下:

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.common.exceptions import NoSuchElementException 
import unittest, time, re 

class Isnin2(unittest.TestCase): 
    def setUp(self): 
     self.driver = webdriver.Firefox() 
     self.driver.implicitly_wait(30) 
     self.base_url = "http://www.google.com.my/" 
     self.verificationErrors = [] 

    def test_isnin2(self): 
     driver = self.driver 
     driver.get("https://stackoverflow.com/search?q=google&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a") 
     driver.find_element_by_css_selector("em").click() 
     driver.find_element_by_id("lst-ib").click() 
     driver.find_element_by_id("lst-ib").clear() 
     driver.find_element_by_id("lst-ib").send_keys("selenium python") 
     driver.find_element_by_link_text("Setting Up Selenium with Python").click() 
     driver.find_element_by_link_text("selenium.py").click() 

    def is_element_present(self, how, what): 
     try: self.driver.find_element(by=how, value=what) 
     except NoSuchElementException, e: return False 
     return True 

    def tearDown(self): 
     self.driver.quit() 
     self.assertEqual([], self.verificationErrors) 

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

我真的很希望有人能幫助我,因爲我是新來硒蟒蛇。

回答

1

看起來好像你不提供你的base_urldriver。像這樣的東西

driver.get("https://stackoverflow.com/search?q=google&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a") 

driver.get(self.base_url + "https://stackoverflow.com/search?q=google&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a") 

編輯:也

:也許你應該取代這一行你想測試與is_element_present方法的東西嗎?使用Python單元測試框架,方法名稱必須以test開頭。所以如果你想把它作爲測試重命名爲test_is_element_present

此外:如果您計劃對響應進行多項測試,則應將driver.get()調用放在setUp方法中,因爲測試順序不必與您編寫測試的順序相同。另一方面,保證在調用任何測試之前運行另一個測試程序setUp

1

我會評論康斯坦丁尼斯的答案,如果可以的話,但我沒有代表。 。 。

我用這個問題作爲藉口來研究IDE - 看起來像上面的代碼幾乎是直接導出到Python Webdriver,在我的測試用例中,它導致相同的格式錯誤的.get(base_url沒有提供)。

Constantinius提到的其餘問題都是IDE有限能力的結果。 is_element_present方法看起來像是IDE中正在進行的工作的一部分,我猜它是實際檢查的佔位符。

相關問題