NameError: имя 'драйвер' не определено - PullRequest
0 голосов
/ 11 июля 2020

Ну вот, Python учусь впервые. Мне нужна помощь с этим кодом ниже:

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()

Когда я запускаю этот код, появляется следующая ошибка: NameError: **name 'driver' is not defined**. Что может происходить? Мое окно Chrome открывается, но закрывается очень быстро.

Заранее благодарим за помощь!

1 Ответ

0 голосов
/ 11 июля 2020

Вам необходимо использовать self.driver = ... в вашем методе init.

Всего:

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()
...