【问题标题】:how to avoid attribute error from classes?如何避免类的属性错误?
【发布时间】:2019-07-04 03:32:35
【问题描述】:

我做了一个类作为selenium driver 的基本设置。我向它添加了方法。我想从上下文管理器访问类的方法我试图在context manager 中创建类的对象并访问该方法但我失败了。我该怎么做?

我什至不知道这段代码有什么问题。请帮帮我。

class SeleniumDriver:
     '''basic setup for chromedriver(selenium)'''

     def __init__(self, 
             driversource='C:\\Users\Ewis\Downloads\chromedriver.exe',
             url = ('https://realpython.com/')
             ):
        self.driversource = driversource
        self.url = url # this tuple

    def __enter__(self):
        self.driver = webdriver.Chrome(executable_path=self.driversource)
        for urls in self.url:
            self.driver.get(urls)
        return self.driver, Keys

    def __exit__(self, exc_type, exc_val, exc_trace):
        self.driver.quit()

# and this code(another project)

from seleniumdriver import SeleniumDriver

with SeleniumDriver() as packed:

    seldriver1 = SeleniumDriver()
    driver = packed[0]
    urls = ('https://realpython.com/','https://stackoverflow.com/')
    resp = seldriver1.geturl(urls)
    for url in resp:
        title, url = resp.titleurl()
        print(title, '\n', url,'\n')

错误:

Traceback(最近一次调用最后一次): 文件“C:\python\progs\web\selenium\navigation_commands\navigation_commands.py”,第 9 行,在 对于相应的网址: 文件“c:\python\progs\my_modules\seleniumdriver\seleniumdriver.py”,第 22 行,在 geturl 产量(self.driver).get(url) AttributeError:“SeleniumDriver”对象没有属性“驱动程序”

【问题讨论】:

    标签: python selenium class selenium-webdriver


    【解决方案1】:

    我不确定这是否足以解决所有问题,但是:

    你必须使用packed[0],它是用with创建的,它执行__enter__,它创建self.driver

    resp = packed[0].geturl(urls)
    

    但是你使用seldriver1,它是使用seldriver1 = SeleniumDriver()创建的,它不执行__enter__,所以它不会创建self.driverseldriver1.driver),你会得到错误:

    'SeleniumDriver' object has no attribute 'driver'
    

    from seleniumdriver import SeleniumDriver
    
    with SeleniumDriver() as packed:
    
        urls = ('https://realpython.com/','https://stackoverflow.com/')
        resp = packed[0].geturl(urls)
    
        for url in resp:
            title, url = resp.titleurl()
            print(title, '\n', url,'\n')
    

    您的代码对我来说似乎很奇怪,它可能需要进行更多更改才能正常工作。


    编辑:这段代码对我有用。

    因为我在 Linux 上使用 Firefox,而且我不必设置 driversource,所以我添加了 None 以在没有 driversource 的情况下运行

    我从__enter__ 中删除了get(url),因为它没用。您无法在 __enter__ 中获取两个页面并稍后在 for 循环中使用它们,因为 Selenium 在您打开第二页时不会保留有关第一页的信息。

    from selenium import webdriver
    
    
    class SeleniumDriver:
        '''basic setup for chromedriver(selenium)'''
        def __init__(self, driversource='C:\\Users\Ewis\Downloads\chromedriver.exe'):
            self.driversource = driversource
    
        def __enter__(self):
            if self.driversource:
                #self.driver = webdriver.Chrome(executable_path=self.driversource)
                self.driver = webdriver.Firefox(executable_path=self.driversource)
            else:
                #self.driver = webdriver.Chrome()
                self.driver = webdriver.Firefox()
            return self.driver
    
        def __exit__(self, exc_type, exc_val, exc_trace):
            self.driver.quit()
    
    
    with SeleniumDriver(None) as driver:
        urls = ('https://realpython.com/', 'https://stackoverflow.com/')
        for url in urls:
            driver.get(url)
            title = driver.title
            print(title, '\n', url,'\n')
    

    【讨论】:

      猜你喜欢
      • 2019-11-23
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      相关资源
      最近更新 更多