简体   繁体   中英

Python Selenium “name 'driver' is not defined”

I'm trying to log into an account of mine using Selenium on with python. When I write this code without object it works with no problems, but when I start to implement a class I get the error:

name 'driver' not defined

It' weird because before I get the error 'driver' is already called 1 time.

The code looks like this:

class my_bot:
def __init__(self):

    self.driver = webdriver.Safari()
    self.driver.get('https://website.com')

def login(self, email, password):

    self.email = email
    self.password = password

    wait = WebDriverWait(self.driver, 10)

    fb_btn = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="modal-manager"]/div/div/div/div/div[3]/span/div[2]/button')))
    self.driver.execute_script("arguments[0].click()", fb_btn)
    sleep(3)

    #switch the window

    base_window = self.driver.window_handles[0]
    self.driver.switch_to_window(driver.window_handles[1])

I get the error on the last line even though the call of 'driver' already happened before.

Has anyone an idea why this isnt working?

You can use below code while switching to new window. it will help you to get the window handle after a new window has opened

self.driver.switch_to_window(self.driver.window_handles[1])

add self.

self.driver.switch_to_window(driver.window_handles[1]) -> self.driver.switch_to_window(self.driver.window_handles[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM