Whatsapp Automation使用Selenium和Python时出错



我试图用硒给whatsapp中的朋友发一条消息。我下载了windows最新版本的chromedriver,当我运行代码时,whatsapp网站被打开,但消息没有发送。这是我的代码和我得到的错误:-

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
# Replace below path with the absolute path
# to chromedriver in your computer
*driver = webdriver.Chrome(executable_path=r'D:Whatsapp Automationchromedriver.exe')
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 600)
# Replace 'Friend's Name' with the name of your friend
# or the name of a group
target = "Friend's name"
# Replace the below string with your own message
string = "Hii"
x_arg = '//span[contains(@title,' + target + ')]'
group_title = wait.until(EC.presence_of_element_located((
By.XPATH, x_arg)))
group_title.click()
inp_xpath = '//div[@class="input"][@dir="auto"][@data-tab="1"]'
input_box = wait.until(EC.presence_of_element_located((
By.XPATH, inp_xpath)))
for i in range(100):
input_box.send_keys(string + Keys.ENTER)
time.sleep(1)

这是我得到的调试文件

[1228/125305.696:ERROR:process_reader_win.cc(123)] NtOpenThread: {Access Denied} A process has requested access to an object, but has not been granted those access rights. (0xc0000022)
[1228/125305.696:ERROR:exception_snapshot_win.cc(99)] thread ID 8952 not found in process
[1228/125305.742:ERROR:process_reader_win.cc(151)] SuspendThread: Access is denied. (0x5)
[1228/125305.758:ERROR:process_reader_win.cc(151)] SuspendThread: Access is denied. (0x5)
[1228/125305.758:ERROR:process_reader_win.cc(151)] SuspendThread: Access is denied. (0x5)
[1228/125305.758:ERROR:process_reader_win.cc(151)] SuspendThread: Access is denied. (0x5)
[1228/125305.758:ERROR:process_reader_win.cc(151)] SuspendThread: Access is denied. (0x5)
[1228/125305.758:ERROR:process_reader_win.cc(151)] SuspendThread: Access is denied. (0x5)
[1228/125305.758:ERROR:process_reader_win.cc(123)] NtOpenThread: {Access Denied} A process has requested access to an object, but has not been granted those access rights. (0xc0000022)
[1228/125305.758:ERROR:exception_snapshot_win.cc(99)] thread ID 10128 not found in process

我从下载了chromedriverhttps://chromedriver.storage.googleapis.com/index.html?path=87.0.4280.88/

我已经用硒做到了这一点。我制作了一个whatsapp机器人,当它收到任何人的新消息时,它会自动发送消息。

请检查以下代码,并尝试将其用于向某人发送消息。


from selenium import webdriver
import time
try:
driver = webdriver.Chrome(executable_path=r'D:Whatsapp Automationchromedriver.exe')
except:
driver = webdriver.Chrome(ChromeDriverManager().install())       # This will install the updated chromedriver automatically.
driver.get("https://web.whatsapp.com")
time.sleep(25) # For scan the qr code from the mobile phone to connect.
# Plese make sure that you have done the qr code scan successful.
confirm = int(input("Press 1 to proceed if sucessfully login or press 0 for retry : "))
if confirm == 1:
print("Continuing...")
elif confirm == 0:
driver.close()
exit()
else:
print("Sorry Please Try again")
driver.close()
exit()
'''
while True:
# Here you can handle the received message and can send message to any selected person. Or you can follow my way to reply each person on their new message.
send_message(driver)
'''
def send_message(chrome_browser):
print("Starting Sending_msg")
user_name_list = ['My Bot +1']  # Add the all users name in the list to whom you want to sent message.
for user_name in user_name_list:
try:
# Select for the title having user name
user = chrome_browser.find_element_by_xpath('//span[@title="{}"]'.format(user_name))
user.click()
time.sleep(1)
except NoSuchElementException as se:
print("Error : {}nCause of Error : {}n Line number : {}".format(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2].tb_lineno))
print(se)
new_chat(user_name, chrome_browser)
# Typing message into message box
time.sleep(1)

message_input_box = chrome_browser.find_element_by_xpath('//div[@class="p3_M1 _1YbbN"]')
message_box.send_keys('Hey, I am your whatsapp bot :)')

time.sleep(1)
# Click on send button
try:
message_box = chrome_browser.find_element_by_xpath('//button[@class="_4sWnG"]')
message_box.click()
except:
print("Error : {}nCause of Error : {}n Line number : {}".format(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2].tb_lineno))
time.sleep(1)
message_box = chrome_browser.find_element_by_xpath('//button[@class="_4sWnG"]')
message_box.click()
time.sleep(2)

send_message(driver)

节点:我不确定whatsapp网络是否再次更改了其网络元素类名。如果发现任何与找不到这样的元素有关的错误,请检查whatsapp web元素类名,并在代码中更新它。

  • 如果要复制,请确保代码块中的缩进相等。

  • 可以在下面的链接中阅读我的另一个答案,了解更多关于使用python的WhatsApp网络的信息。

  • 使用Python 发送的WhatsApp消息中的换行符

  • 我正在使用python开发WhatsApp机器人。

  • 有关贡献,您可以联系:anurag.cse016@gmail.com

  • 请给我一颗星https://github.com/4NUR46如果这个答案对你有帮助。

相关内容

  • 没有找到相关文章

最新更新