简体   繁体   English

AttributeError: 'TestTomsLogin' object 没有属性 'driver'

[英]AttributeError: 'TestTomsLogin' object has no attribute 'driver'

conftest.py file conftest.py 文件

    import pytest
    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    from webdriver_manager.chrome import ChromeDriverManager
    
    
    @pytest.fixture(scope="class")
    def setup(request):
        driver = webdriver.Chrome(ChromeDriverManager().install())
        driver.get("https://itera-qa.azurewebsites.net/Login")
        driver.maximize_window()
        wait = WebDriverWait(driver, 10)
        request.cls.driver = driver
        request.cls.driver = wait
        yield
        driver.close()

loginpage.py file登录页面.py 文件

    from selenium.webdriver.common.by import By
    
    
    class TomsLogin:
        def __init__(self, driver, wait):
            self.driver = driver
            self.wait = wait
    
        def toms_login(self, usern, pwd):
            self.driver.find_element(By.ID, "Username").send_keys(usern)
            self.driver.find_element(By.ID, "Password").send_keys(pwd)

test_toms_login.py file -- Getting error while calling this file test_toms_login.py 文件——调用此文件时出错

    import pytest
    from base.loginpage import TomsLogin
    
    @pytest.mark.usefixtures("setup")
    class TestTomsLogin():
        def test_login(self):
            self.tl = TomsLogin(self.driver, self.wait)
            self.tl.toms_login("User1", "pwd1")
    
    ttl = TestTomsLogin()
    ttl.test_login()

Error Details:错误详情:

collecting ... 
test_toms_login.py:None (test_toms_login.py)
test_toms_login.py:11: in <module>
    ttl.test_login()
test_toms_login.py:7: in test_login
    self.tl = TomsLogin(self.driver, self.wait)
E   AttributeError: 'TestTomsLogin' object has no attribute 'driver'

I would change the structure of you code to something like this:我会将您的代码结构更改为以下内容:
And make sure driver is actually installing (you can use hard coded path once to see the problem)并确保驱动程序正在实际安装(您可以使用硬编码路径一次来查看问题)

@pytest.fixture(scope="class")
def driver(request):
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get("https://itera-qa.azurewebsites.net/Login")
    driver.maximize_window()
    yield driver
    driver.quit()

@pytest.fixture(scope="class")
def setup(request, driver):
    # put your wait here too
    request.cls.driver = driver
ttl = TestTomsLogin()
ttl.test_login()

The Issue got resolved by removing the above two lines.通过删除上述两行,问题得到解决。 It should not be used when using pytest methods.使用 pytest 方法时不应使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:“驱动程序”对象没有属性“ id” - AttributeError: 'Driver' object has no attribute 'id' AttributeError:“WebElement”object 没有属性“驱动程序” - AttributeError: 'WebElement' object has no attribute 'driver' AttributeError:&#39;LoginPage&#39;对象没有属性&#39;driver&#39; - AttributeError: 'LoginPage' object has no attribute 'driver' AttributeError:&#39;TestLogin&#39;对象没有属性&#39;driver&#39; - AttributeError: 'TestLogin' object has no attribute 'driver' AttributeError: 'WebDriver' object 没有属性 'driver' - AttributeError: 'WebDriver' object has no attribute 'driver' 安装 Chrome 驱动程序,AttributeError: 'Service' object has no attribute 'process' - install Chrome driver, AttributeError: 'Service' object has no attribute 'process' AttributeError: 'list' object 没有属性 'find_element' - Selenium 驱动程序 - AttributeError: 'list' object has no attribute 'find_element' - Selenium driver AttributeError:“ Dlink”对象没有属性“ browser_driver” - AttributeError: 'Dlink' object has no attribute 'browser_driver' 抛出错误:AttributeError:&#39;KenLogin&#39;对象没有属性&#39;driver&#39; - Throwing error : AttributeError: 'KenLogin' object has no attribute 'driver' AttributeError:对象没有属性 - AttributeError: object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM