向类中添加动态函数;蟒蛇

时间:2016-03-02 20:14:54

标签: python python-3.x closures metaprogramming dynamic-programming

"""pip install selenium"""

import os, time, subprocess, random

from functools import wraps

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select


class WebdriverChauffuer(object):

    def __init__(self, username=None, password=None, start_url=None):
        self.username = username
        self.password = password
        self.start_url = start_url

    def quit(self):
        self.driver.quit()

    def restart_driver(self):
        self.driver.quit()
        self.start_driver()

    def get(self, url):
        self.driver.get(url)

    def maximize_window(self):
        self.driver.maximize_window()



class FirefoxDriver(WebdriverChauffuer):

    def __init__(self, username=None, password=None, start_url=None, driver=None):
        super(FirefoxDriver, self).__init__(username=username, password=password, start_url=start_url)
        self.start_driver()

    def start_driver(self):
        self.driver = webdriver.Firefox()

目标是

class WebdriverChauffuer(object):

    def __init__(self, username=None, password=None, start_url=None):
        self.username = username
        self.password = password
        self.start_url = start_url

    def quit(self):
        self.driver.quit()

    def restart_driver(self):
        self.driver.quit()
        self.start_driver()

    def generate_methods(self):
        funcs = ['get', 'maximize_window']
        # makes get and maximize_window

然后你可以做

In [14]: d = FirefoxDriver()

In [15]: d.get('google.com')

In [16]: d.maximize_window()

这很有希望,但没有成功Python dynamic function creation with custom names

class Driver(object):
    pass


class FuncTester(object):

    def __init__(self):
        self.driver = Driver()
        self.generate_instance_methods()

    def make_method(self, name):
        def _method(self):
            getattr(self.driver, name)(*args, **kwargs)
        return _method

    def generate_instance_methods(self):
        FUNCTIONS = ['get', 'maximize_window']
        for name in FUNCTIONS:
            _method = self.make_method(name)
            setattr(self, name, _method)



In [11]: f = FuncTester()

In [12]: f.driver
f.driver

In [12]: f.driver.
  File "<ipython-input-12-9e167ce57f62>", line 1
    f.driver.
             ^
SyntaxError: invalid syntax


In [13]: f.driver.get
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-7e99453babae> in <module>()
----> 1 f.driver.get

AttributeError: 'Driver' object has no attribute 'get'

1 个答案:

答案 0 :(得分:1)

为什么不直接将Selenium Driver对象传递给你的FuncTester:

class FuncTester(webdriver)

我不是100%肯定会工作,而不是安装了selenium的计算机,但我认为这样可行。只需将FuncTester传递给webdriver对象。

然后当你这样做:

f = new FuncTester()

尝试:

f.get()

但是我会小心不要像你离开时那样覆盖你自己的Selenium Webdriver函数,

FUNCTIONS = ['get', 'maximize_window']

在您的代码中。

然后对于这样的事情:

def restart_driver(self):
    self.driver.quit()
    self.start_driver()

只是这样做:

def restart_driver(self):
    self.quit() //This got changed!
    self.start_driver()