selenium 错误:属性错误:“function”对象没有属性“driver”[重复]

6yt4nkrj  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(191)
    • 此问题在此处已有答案**:

How to create a new instance from a class object in Python(6个答案)
3天前关闭。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from self import self

class Demo:
def new_login(self):
driver = webdriver.Chrome()
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login")
driver.maximize_window()

def new_tests(self):
self.driver.find_element(By.XPATH, "//input[@placeholder='Username']").send_key("Admin")

obj = Demo
obj.new_login(self)
obj.new_tests(self)

尝试运行此代码时,我遇到了一个错误:
Error: AttributeError: 'function' object has no attribute 'driver'
如何解决此问题?

qpgpyjmq

qpgpyjmq1#

您的代码应如下所示:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

class Demo:
    def __init__(self):
        self.driver = None

    def new_login(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login")
        self.driver.maximize_window()

    def new_tests():
        self.driver.find_element(By.XPATH, "//input[@placeholder='Username']").send_key("Admin")

obj = Demo()
obj.new_login()
obj.new_tests()

相关问题