我正在try 使用SeleniumTeleBot库在Telegram上自动发送消息.然而,我不断遇到以下错误:

AttributeError: 'TeleBot' object has no attribute 'send_message'. 

我该如何解决这个问题?这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from telebot import TeleBot

bot = TeleBot("your_token_here")

driver = webdriver.Chrome(executable_path="/path/to/chromedriver")
driver.get("https://web.telegram.org/")
driver.implicitly_wait(10)

login_button = driver.find_element_by_xpath("//button[text()='Log in']")
login_button.click()

phone_input = driver.find_element_by_name("phone_number")
phone_input.send_keys("your_phone_number")

next_button = driver.find_element_by_xpath("//button[text()='Next']")
next_button.click()

bot.send_message(chat_id="your_chat_id", text="Hello, World!")

driver.quit()

推荐答案

您不明白pyTelegramBotAPITeleBot类的使用. 似乎您正在将Selenium自动化与机器人发送消息的功能混合起来.您应该通过pyTelegramBotApi or发送消息,使用Selenium在Web界面上自动进行用户交互.您shouldn't同时使用两者来发送单个消息:

from telebot import TeleBot
bot = TeleBot("your_token")
def send_telegram_message(chat_id, message):
    try:
        bot.send_message(chat_id, message)
        print("Message sent.")
    except Exception as e:
        print(f"Failed to send message: {str(e)}")
send_telegram_message("your_chat_id", "Hello, World!")

Python相关问答推荐

在Python中,如何才能/应该使用decorator 来实现函数多态性?

在Transformer中使用LabelEncoding的ML模型管道

由于瓶颈,Python代码执行太慢-寻求性能优化

Pydantic:如何将对象列表表示为dict(将列表序列化为dict)

为什么我的Python代码在if-else声明中的行之前执行if-else声明中的行?

在Python中处理大量CSV文件中的数据

Django mysql图标不适用于小 case

如何删除索引过go 的lexsort深度可能会影响性能?' &>

抓取rotowire MLB球员新闻并使用Python形成表格

可变参数数量的重载类型(args或kwargs)

如何在Python中并行化以下搜索?

将tdqm与cx.Oracle查询集成

基于索引值的Pandas DataFrame条件填充

Pandas DataFrame中行之间的差异

创建可序列化数据模型的最佳方法

如何使Matplotlib标题以图形为中心,而图例框则以图形为中心

如何在Python中获取`Genericums`超级类型?

以逻辑方式获取自己的pyproject.toml依赖项

使用Openpyxl从Excel中的折线图更改图表样式

如何将数据帧中的timedelta转换为datetime