NameError:未定义名称“驱动程序”

2024-06-16 15:06:15 发布

您现在位置:Python中文网/ 问答频道 /正文

嗯,我是第一次学习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窗口打开但很快关闭

提前感谢您的帮助


Tags: 代码fromimportselfdefdriverseleniumchrome
1条回答
网友
1楼 · 发布于 2024-06-16 15:06:15

您需要在init方法中使用self.driver = ...

总体而言:

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

相关问题 更多 >