Lwyz Noname
Lwyz Noname

Reputation: 11

I'm having some trouble with a python Bot, like the driver is not being recognized

Please can someone tell me why I'm getting this error with this code? I am trying to run a whatsapp bot. However, when I run my code, I get the error below.

error:

Traceback (most recent call last):
  File "C:\Users\lwyzb\OneDrive\Documentos\Bot whatsapp\wppBot.py", line 32, in <module>
    bot.MsgSend()
  File "C:\Users\lwyzb\OneDrive\Documentos\Bot whatsapp\wppBot.py", line 16, in MsgSend
    self.driver.get('https://web.whatsapp.com/')
AttributeError: 'WhatsappBot' object has no attribute 'driver'

code:

from selenium import webdriver
import time

class WhatsappBot:
    def _init_(self):
        self.msg = "Bom dia famiiiilia"
        self.groups = ["Churras ??/?? šŸ–šŸ”„šŸ¤—", "PARƇAS (Nova formaĆ§Ć£o)", "ParƧas incorporated"]
        options = webdriver.ChromeOptions()
        options.add_argument('lang=pt-br')
        self.driver = webdriver.Chrome(executable_path=r'./chromedriver.exe')

    def MsgSend(self):
          # <span dir="auto" title="PARƇAS (Nova formaĆ§Ć£o)" class="_3ko75 _5h6Y_ _3Whw5">PARƇAS (Nova formaĆ§Ć£o)</span>
          # <div tabindex="-1" class="_3uMse">
          # <span data-testid="send" data-icon="send" class="">
          self.driver.get('https://web.whatsapp.com/')
          time.sleep(30)
          for group in self.groups:
              group = self.driver.find_element_by_xpath(f"//span[@title='{group}']")
              time.sleep(3)
              group.click()
              chatBox = self.driver.find_element_by_class_name('_3uMse')
              time.sleep(3)
              chatBox.click()
              chatBox.send_keys(self.msg)
              send = self.driver.find_element_by_xpath("//span[@data-icon='send']")
              time.sleep(3)
              send.click()
              time.sleep(5)

bot = WhatsappBot()
bot.MsgSend()         

Upvotes: 1

Views: 309

Answers (2)

wasif
wasif

Reputation: 15498

Everyting is OK but LOL you are missing the two underscores of __init__, so class does not get properly initialised:

from selenium import webdriver
import time

class WhatsappBot:
    def __init__(self):
        self.msg = "Bom dia famiiiilia"
        self.groups = ["Churras ??/?? šŸ–šŸ”„šŸ¤—", "PARƇAS (Nova formaĆ§Ć£o)", "ParƧas incorporated"]
        options = webdriver.ChromeOptions()
        options.add_argument('lang=pt-br')
        self.driver = webdriver.Chrome(executable_path=r'./chromedriver.exe')

    def MsgSend(self):
          # <span dir="auto" title="PARƇAS (Nova formaĆ§Ć£o)" class="_3ko75 _5h6Y_ _3Whw5">PARƇAS (Nova formaĆ§Ć£o)</span>
          # <div tabindex="-1" class="_3uMse">
          # <span data-testid="send" data-icon="send" class="">
          self.driver.get('https://web.whatsapp.com/')
          time.sleep(30)
          for group in self.groups:
              group = self.driver.find_element_by_xpath(f"//span[@title='{group}']")
              time.sleep(3)
              group.click()
              chatBox = self.driver.find_element_by_class_name('_3uMse')
              time.sleep(3)
              chatBox.click()
              chatBox.send_keys(self.msg)
              send = self.driver.find_element_by_xpath("//span[@data-icon='send']")
              time.sleep(3)
              send.click()
              time.sleep(5)

bot = WhatsappBot()
bot.MsgSend()         

Upvotes: 2

Ayush Garg
Ayush Garg

Reputation: 2527

You're init function is named incorrectly - it should be __init__, not _init_:

from selenium import webdriver
import time

class WhatsappBot:
    def __init__(self):
        self.msg = "Bom dia famiiiilia"
        self.groups = ["Churras ??/?? šŸ–šŸ”„šŸ¤—", "PARƇAS (Nova formaĆ§Ć£o)", "ParƧas incorporated"]
        options = webdriver.ChromeOptions()
        options.add_argument('lang=pt-br')
        self.driver = webdriver.Chrome(executable_path=r'./chromedriver.exe')

    def MsgSend(self):
          # <span dir="auto" title="PARƇAS (Nova formaĆ§Ć£o)" class="_3ko75 _5h6Y_ _3Whw5">PARƇAS (Nova formaĆ§Ć£o)</span>
          # <div tabindex="-1" class="_3uMse">
          # <span data-testid="send" data-icon="send" class="">
          self.driver.get('https://web.whatsapp.com/')
          time.sleep(30)
          for group in self.groups:
              group = self.driver.find_element_by_xpath(f"//span[@title='{group}']")
              time.sleep(3)
              group.click()
              chatBox = self.driver.find_element_by_class_name('_3uMse')
              time.sleep(3)
              chatBox.click()
              chatBox.send_keys(self.msg)
              send = self.driver.find_element_by_xpath("//span[@data-icon='send']")
              time.sleep(3)
              send.click()
              time.sleep(5)

bot = WhatsappBot()
bot.MsgSend()       

Python only looks for the __init__ function - since you accidentally forgot those two underscores, Python didn't realize that was supposed to be the initializer, and so it didn't run it.

Upvotes: 4

Related Questions