Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
全局名称未得到定义-python_Python_Selenium - Fatal编程技术网

全局名称未得到定义-python

全局名称未得到定义-python,python,selenium,Python,Selenium,我正在尝试运行以下命令: import unittest import wd.parallel from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import WebDriverException import copy class Selenium2OnSauce(unittest.TestCase):

我正在尝试运行以下命令:

import unittest
import wd.parallel
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import WebDriverException
import copy
class Selenium2OnSauce(unittest.TestCase):
    def setUp(self):
        desired_capabilities = []

        browser = copy.copy(webdriver.DesiredCapabilities.FIREFOX)
        browser['version'] = '10'
        browser['platform'] = 'XP'
        browser['name'] = 'Python at Sauce'
        browser['tags'] = "Parallel"
        desired_capabilities += [browser]

        self.drivers = wd.parallel.Remote(
            desired_capabilities=desired_capabilities,
            command_executor="http://user-string:key-string@ondemand.saucelabs.com:80/wd/hub"
        )

    def ajax_complete(driver):
        try:
            return 0 == driver.execute_script("return JQuery.active")
        except WebDriverException:
            pass    

    @wd.parallel.multiply
    def test_sauce(self):
        self.driver.get('http://url.com')
        username = self.driver.find_element_by_id('id_username')
        print "Found Username"
        username.send_keys('i1@example.com')
        print "Username Entered"

        password = self.driver.find_element_by_id('id_password')
        print "Found Password"
        password.send_keys('testuser')        
        print "Password Entered"

        password.submit()
        print "Logged in"

        self.driver.find_elements_by_link_text('Notification')[0].click()
        print "Going to Notification"

        WebDriverWait(self.driver, 10).until(
            ajax_complete, "Timeout waiting for page to load"
        )

        print "ajax loaded string" in self.drive.page_source

    @wd.parallel.multiply
    def tearDown(self):
        print("Link to your job: https://saucelabs.com/jobs/%s" % self.driver.session_id)
        self.driver.quit()
        print "Quit"

if __name__ == '__main__':
    unittest.main()
当我这样做时,我得到了:

Found Username
Entered Username
Found Password
Entered Password
Logged in
Going to Cal
Process Process-1:
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "build/bdist.macosx-10.8-intel/egg/wd/parallel.py", line 88, in thread_func
    f(SubTest(driver))
  File "functionalTest.py", line 57, in test_sauce
    ajax_complete, "Timeout waiting for page to load"
NameError: global name 'ajax_complete' is not defined
Link to your job: https://saucelabs.com/jobs/06022dc1f54548d98a75f59bd211848d
Quit
我想我在访问var调用
ajax\u complete
时遇到了问题,但我不知道为什么

最后,我想做的是等待所有ajax完成,然后再运行下一个测试


注意:我是python新手,很抱歉,这个问题真的很傻。

python中的方法可以通过
self
访问:

WebDriverWait(self.driver, 10).until(
    self.ajax_complete, "Timeout waiting for page to load"
)

请注意,
ajax\u complete
方法本身也需要将
self
作为第一个参数。

Python中的方法通过
self
访问:

WebDriverWait(self.driver, 10).until(
    self.ajax_complete, "Timeout waiting for page to load"
)

请注意,
ajax\u complete
方法本身也需要将
self
作为第一个参数。

或者,
ajax\u complete
可以定义为类外的函数或静态方法。如下所示:
def ajax\u complete(self):try:return 0==self.driver.execute\u脚本(“return JQuery.active”)除了WebDriverException:pass
之外,
ajax\u complete
也可以定义为类外的函数或静态方法。如下所示:
def ajax\u complete(self):try:return 0==self.driver.execute_脚本(“return JQuery.active”),除了WebDriverException:pass