简体   繁体   中英

NameError: name 'driver' is not defined

Well, I'm studying Python for the first time. I need help with this code below:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class CursoAutomacao:
    def __init__(self):
        chrome_options = Options()
        chrome_options.add_argument('--lang-pt-BR')
        driver = webdriver.Chrome(executable_path=r'C:\Users\Daniel pc\Desktop\Tutorial chromeDriver\chromedriver.exe')

    def Iniciar(self):
        self.driver.get('https://www.mercadolivre.com.br')

curso = CursoAutomacao()
curso.Iniciar()

When I run this code this error appears: NameError: **name 'driver' is not defined**. What could be happening? My window of Chrome opens but close very quick.

Thanks in advance for your help!

You need to use self.driver =... in your init method.

In totality:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

class CursoAutomacao:
    def init(self):
        chrome_options = Options()
        chrome_options.add_argument('--lang-pt-BR')
        self.driver = webdriver.Chrome(executable_path=r'C:\Users\Daniel\pc\Desktop\Tutorial\chromeDriver\chromedriver.exe')

    def Iniciar(self):
        self.driver.get('https://www.mercadolivre.com.br')

curso = CursoAutomacao()
curso.Iniciar()

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