2011-08-22 177 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中正在进行的工作的一部分,我猜它是实际检查的占位符。

相关问题