Driver name not set

Asked

Viewed 45 times

-2

I’m doing this bot to access instagram, but is presenting an error that I can not identify. I’m a beginner and I’m having trouble finding the error. If anyone can help me, I appreciate!!

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import random

class InstagramBot:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.driver = webdriver.Firefox(executable_path=r"C:\Users\bruno\Desktop\geckodriver-v0.29.1-win64\geckodriver.exe")
           
    def login(self):
        driver = self.driver
        driver.get("https://www.instagram.com/accounts/login/?hl=pt-br")
   
    campo_usuario = driver.find_element_by_xpath("//input[@name='username']")
    campo_usuario.click()
    campo_usuario.clear()
    campo_usuario.send_keys(self.username)
    campo_senha = driver.find_element_by_xpath("//input[@name='password']")
    campo_senha.click()
    campo_senha.clear()
    campo_senha.send_keys(self.password)
    campo_senha.send_keys(Keys.RETURN)
 
   
brunoBot = InstagramBot("user","senha1234")  
brunoBot.login()

Error:

File "c:\Users\bruno\Desktop\geckodriver-v0.29.1-win64\igBot.py", line 16, in InstagramBot
campo_usuario = driver.find_element_by_xpath("//input[@name='username']")
NameError: name 'driver' is not defined
PS C:\Users\bruno\Desktop\geckodriver-v0.29.1-win64>```

Obrigado! :) 
  • 1

    I believe the lines from campo_usuario are indented wrong. Include 4 spaces for each of them.

1 answer

1

Some points need to be fixed in your code for it to work properly, come on?

login(self):
    driver = self.driver
    driver.get("https://www.instagram.com/accounts/login/?hl=pt-br")

campo_usuario = driver.find_element_by_xpath("//input[@name='username']")
campo_usuario.click()
campo_usuario.clear()
campo_usuario.send_keys(self.username)
campo_senha = driver.find_element_by_xpath("//input[@name='password']")
campo_senha.click()
campo_senha.clear()
campo_senha.send_keys(self.password)
campo_senha.send_keys(Keys.RETURN)

In the login function, first you must correctly identify your code to stay inside the function. Identation is something fundamental in the python language for delimiting the blocks of code, the misuse can lead to the malfunction of your system. Then it would look like this:

def login(self):
    driver = self.driver
    driver.get("https://www.instagram.com/accounts/login/?hl=pt-br")
    campo_usuario = driver.find_element_by_xpath("//input[@name='username']")
    campo_usuario.click()
    campo_usuario.clear()
    campo_usuario.send_keys(self.username)
    campo_senha = driver.find_element_by_xpath("//input[@name='password']")
    campo_senha.click()
    campo_senha.clear()
    campo_senha.send_keys(self.password)
    campo_senha.send_keys(Keys.RETURN)

A tip. still in the "login" function you would not need to create the variable "driver" with the value "self.driver", because right above in the constructor "__ init__" you have already created it as "self.driver". What does that mean? The "self" serves for you to refer to the object(instance) itself both when you are making use of methods and when using attributes belonging to this object. So this way you can access your "self.driver" anywhere within your class and the structure would look like this:

def login(self):
    self.driver.get("https://www.instagram.com/accounts/login/?hl=pt-br")
    campo_usuario = self.driver.find_element_by_xpath("//input[@name='username']")
    campo_usuario.click()
    campo_usuario.clear()
    campo_usuario.send_keys(self.username)
    campo_senha = self.driver.find_element_by_xpath("//input[@name='password']")
    campo_senha.click()
    campo_senha.clear()
    campo_senha.send_keys(self.password)
    campo_senha.send_keys(Keys.RETURN)
  • 1

    Whoa, thanks for the tip!!

  • Got it buddy, ball show!! Thanks so much for your help!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.