【问题标题】:Error in Selenium Python ScriptSelenium Python 脚本中的错误
【发布时间】:2011-01-11 07:46:42
【问题描述】:

我正在尝试掌握 Python 和 Selenium RC 的窍门,但在解析以下示例 Selenium Python 脚本时遇到了一些困难。除了一个之外,我已经解决了以下所有代码的错误:

from selenium import selenium
import unittest

class SignUpTask(unittest.TestCase):
    """ The following needs to have the issues corrected to make 
        it run. When the run is completed the answer for question 
        2 will be shown"""

def setUp(self):
    self.selenium = selenium("localhost", 4444, "*firefox",
            "http://www.google.com/")
    self.selenium.start()


def test_that_will_print_out_a_url_as_answer_for_task(sel):
    self.selenium.open("/")
    self.selenium.click("link=Web QA")
    self.selenium.wait_for_page_to_load("30000")
    self.selenium.click("link=Get Involved")
    self.selenium.wait_for_page_to_load("30000")
    url = self.selenium.get_attribute("//ol/li[5]/a@href")
    print """The Url below needs to be entered as the answer 
             for Question 2) in the signup task"""
    print "URL is: %s" % url

def tearDown(self):
    self.selenium.stop()

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

通过 Selenium RC 运行上述脚本后,出现以下错误:


错误:test_that_will_print_out_a_url_as_answer_for_task (ma​​in.SignUpTask) 回溯(最近一次通话最后): 文件“/Users/eanderson/Desktop/TestFiles/Selenium1.py”,第 16 行,在 test_that_will_print_out_a_url_as_answer_for_task self.selenium.open("/") NameError:未定义全局名称“self”

在 24.577 秒内运行 1 次测试

失败(错误=1)


有没有人明白我为什么会得到这个

NameError:未定义全局名称“self”

第 16 行的错误,可以帮助我缓解此错误,以便我的脚本可以无错误地解析吗?

【问题讨论】:

    标签: python error-handling selenium selenium-rc


    【解决方案1】:

    def test_that_will_print_out_a_url_as_answer_for_task(sel): 那应该是self

    【讨论】:

      【解决方案2】:

      调试此类问题的第一步是假设错误消息是真实的。从字面上看。它说“自我没有定义”。所以问问自己,为什么没有定义?。确实只有几个原因:要么你真的没有定义它,要么你认为你定义了它但没有(这意味着一个错字,或者你定义它是错误的范围)

      那么,“自我”在哪里定义?在 python 的情况下,它作为参数传入。因此,请查看该函数的参数列表。当您这样做时,您会注意到您将self 拼写为sel

      【讨论】:

      • 我来这里是为了寻找完全不同的东西。但是当我刚读到这篇文章时,我解决了我的问题。你太棒了!! :D
      【解决方案3】:

      会不会是“sel”少了一个 f?

      def test_that_will_print_out_a_url_as_answer_for_task(sel):

      【讨论】:

        猜你喜欢
        • 2020-10-20
        • 2015-08-28
        • 2017-12-22
        • 1970-01-01
        • 2017-10-12
        • 2016-07-23
        • 2020-09-22
        • 2017-07-04
        • 1970-01-01
        相关资源
        最近更新 更多