Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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如何将selenium驱动程序传递到函数中_Python_Python 3.x_Selenium_Headless Browser - Fatal编程技术网

Python如何将selenium驱动程序传递到函数中

Python如何将selenium驱动程序传递到函数中,python,python-3.x,selenium,headless-browser,Python,Python 3.x,Selenium,Headless Browser,我在运行代码时不断遇到此错误: **name 'driver' is not defined** 有人能告诉我为什么吗?我怎样才能让它像这样运行。因此,如果任何较小的测试失败,问题所在就非常清楚了 generateRandomBroswerInfo() loginSite() getSomeInfo() quitBroswer() 我正在Python3.6上使用selenium 我的代码: from selenium import webdriver from selenium.webdri

我在运行代码时不断遇到此错误:

**name 'driver' is not defined**
有人能告诉我为什么吗?我怎样才能让它像这样运行。因此,如果任何较小的测试失败,问题所在就非常清楚了

generateRandomBroswerInfo()
loginSite()
getSomeInfo()
quitBroswer()
我正在Python3.6上使用selenium

我的代码:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def genrateBroswer():
    dcap = dict(DesiredCapabilities.PHANTOMJS)
    dcap["phantomjs.page.settings.userAgent"] = ('Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3')
    driver = webdriver.PhantomJS(desired_capabilities=dcap)
    driver.set_window_size(300, 600)

def letssee():
    driver.get('http://www.whatsmyip.org/')
    driver.save_screenshot('this.png')


genrateBroswer()
letssee()
#ETC

您正在
genrateBroswer
中本地定义
driver
,一旦您离开该函数,它就消失了

letssee
函数中,您试图访问
driver
,此时未定义该驱动程序,因此您会收到一个错误:

NameError: name 'driver' is not defined
有关详细信息,请阅读


另外,请查看。

您正在
genrateBroswer
中本地定义
driver
,一旦您离开该函数,它就消失了

letssee
函数中,您试图访问
driver
,此时未定义该驱动程序,因此您会收到一个错误:

NameError: name 'driver' is not defined
有关详细信息,请阅读


另外,请查看。

您可以将类与所有必要的方法一起使用,请参阅。例如:

class Webdriver:

    def __init__(self):
        self.genrateBroswer()
        self.letssee()


    def genrateBroswer(self):
        dcap = dict(DesiredCapabilities.PHANTOMJS)
        dcap["phantomjs.page.settings.userAgent"] = ('Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3')
        self.driver = webdriver.PhantomJS(desired_capabilities=dcap)
        self.driver.set_window_size(300, 600)

    def letssee(self):
        self.driver.get('http://www.whatsmyip.org/')
        self.driver.save_screenshot('this.png')


# Creating an instance of the webdriver
myWebsite = Webdriver()

myWebsite.driver

简而言之,您需要跨函数使用的所有内容,您需要通过使用关键字
self
来存储类的属性,并且需要将
self
传递给类中的所有函数。

您可以使用具有所有必要方法的类,请参阅。例如:

class Webdriver:

    def __init__(self):
        self.genrateBroswer()
        self.letssee()


    def genrateBroswer(self):
        dcap = dict(DesiredCapabilities.PHANTOMJS)
        dcap["phantomjs.page.settings.userAgent"] = ('Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3')
        self.driver = webdriver.PhantomJS(desired_capabilities=dcap)
        self.driver.set_window_size(300, 600)

    def letssee(self):
        self.driver.get('http://www.whatsmyip.org/')
        self.driver.save_screenshot('this.png')


# Creating an instance of the webdriver
myWebsite = Webdriver()

myWebsite.driver

简言之,您需要跨函数使用的所有内容,您需要通过使用关键字
self
来存储类的属性,并且需要将
self
传递给类中的所有函数。

为我工作

    class Class(object):

    options = webdriver.ChromeOptions()
    options.add_argument('--lang=EN')
    driver = webdriver.Chrome(executable_path='assets/chromedriver', chrome_options=options)

    iframe = ''

    def LogIn(self, telnum, password):
        self.driver.set_window_size(800, 600)
        self.driver.get("url")
        self.iframe = self.driver.find_element_by_xpath('//[@id="app"]/div/section/div[2]/div/div/iframe')
        self.driver.switch_to.frame(self.iframe)

为我工作

    class Class(object):

    options = webdriver.ChromeOptions()
    options.add_argument('--lang=EN')
    driver = webdriver.Chrome(executable_path='assets/chromedriver', chrome_options=options)

    iframe = ''

    def LogIn(self, telnum, password):
        self.driver.set_window_size(800, 600)
        self.driver.get("url")
        self.iframe = self.driver.find_element_by_xpath('//[@id="app"]/div/section/div[2]/div/div/iframe')
        self.driver.switch_to.frame(self.iframe)

我可以从
genrateBroswer
中取出
driver
并将其加载到第二个函数中吗?&例如,如何在
genrateBroswer
函数的末尾添加一个return语句:
returndriver
,并将其命名为
driver=genrateBroswer()
@FutureHendrixs。我建议您自己解决,因为这是一个非常基本的问题。无意冒犯,这会让你省去很多头痛和咒骂:-)我可以从
genrateBroswer
中取出
driver
并将其加载到第二个函数中吗?&例如,如何在
genrateBroswer
函数的末尾添加一个return语句:
returndriver
,并将其命名为
driver=genrateBroswer()
@FutureHendrixs。我建议您自己解决,因为这是一个非常基本的问题。没有冒犯的意思,只是它会让你省去很多头痛和咒骂:-)我标记了你的答案,因为它有效。但这并不是我解决问题的答案,而是我将我的变量驱动程序转换成了变量globe。@FutureHendrixs如果你想远离类,我建议使用bgse的方法,将
驱动程序
传递给函数,并返回更新后的
驱动程序
。您能说明如何使用全局变量解决问题吗@未来亨德里克斯?我正在尝试@rinkert方法,但仍然得到
未定义的驱动程序
我标记了您的答案,因为它有效。但这并不是我解决问题的答案,而是我将我的变量驱动程序转换成了变量globe。@FutureHendrixs如果你想远离类,我建议使用bgse的方法,将
驱动程序
传递给函数,并返回更新后的
驱动程序
。您能说明如何使用全局变量解决问题吗@未来亨德里克斯?我正在尝试@rinkert方法,但仍然得到未定义的
驱动程序