【问题标题】:I'm having some trouble with a python Bot, like the driver is not being recognized我在使用 python Bot 时遇到了一些问题,例如无法识别驱动程序
【发布时间】:2021-03-07 12:21:46
【问题描述】:

请有人告诉我为什么这段代码会出现这个错误?我正在尝试运行一个whatsapp bot。但是,当我运行我的代码时,我收到以下错误。

错误:

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'

代码:

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 selenium selenium-webdriver bots


    【解决方案1】:

    你的 init 函数命名不正确 - 它应该是 __init__,而不是 _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 只查找 __init__ 函数 - 由于您不小心忘记了这两个下划线,Python 没有意识到它应该是初始化程序,因此它没有运行它。

    【讨论】:

    • 非常感谢!现在一切都说得通了哈哈
    【解决方案2】:

    一切正常,但是大声笑你缺少__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()         
    

    【讨论】:

    • 非常感谢哈哈我回去改变了方法哈哈但是太好了!感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2021-11-18
    • 2012-05-13
    相关资源
    最近更新 更多