python - 索引错误: list index out of range

标签 python eclipse selenium

我是 Selenium python 的新手。现在我正在尝试运行已经从 selenium Ide 转换的 python 代码。当我运行代码时出现此错误。我正在使用 eclipse 来运行代码

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("/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("/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()

我真的希望有人能帮助我,因为我是 selenium python 的新手。

最佳答案

您似乎没有向驱动程序提供您的base_url。也许你应该替换这一行:

driver.get("/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 + "/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 保证在调用任何测试之前运行。

关于python - 索引错误: list index out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7144571/

相关文章:

eclipse - 在Eclipse中运行gradle Project

c# - Selenium 测试失败时捕获错误消息

java - 我想在 Selenium 中按类抽象

python - MySQLdb 不能使用游标类

python - 从 Pandas 中的现有 df 创建新的 df - python

c - undefined reference ... 使用 gtk 图形

eclipse - 如何使用命令行将多个项目导入Eclipse?

java - WebDriver - 捕获的屏幕截图似乎具有红色或橙色色调

python - 为什么我再次执行时confusion_matrix不一样?

Debian Lenny 上的 Python 2.6。可执行文件应该去哪里?