简体   繁体   English

Python selenium WhatsApp 机器人

[英]Python selenium WhatsApp bot

I am trying to create a bot in WhatsApp but I keep running into我正在尝试在 WhatsApp 中创建一个机器人,但我一直遇到
NoSuchElementException
My code is below:我的代码如下:

from selenium import webdriver
Import time

Firefox_browser = webdriver.Firefox(executable_path="/usr/bin/geckodriver")
Firefox_browser.get('https://web.whatsapp.com/')

time.sleep(15)

firefox_browser.switch_to_frame(firefox_browser.find_element_by_name("WhatsAppBot"))
firefox_browser.find_element_by_xpath('//span[@title="WhatsAppBot"]).click()

There is no element with name attribute equals WhatsAppBot on that page该页面上没有name属性等于WhatsAppBot的元素

I know this question is already a few months old, but I would like to try and answer anyway.我知道这个问题已经有几个月了,但我还是想尝试回答。 As the other answer has already pointed out, what you are trying to do in the last two lines does not make sense.正如另一个答案已经指出的那样,您在最后两行中尝试做的事情没有意义。 The find element by xpath/name function expects to be pointed at a certain element on Whatsapp.通过 xpath/name function 查找元素期望指向 Whatsapp 上的某个元素。 There is no element called "WhatsAppBot" on the WhatsApp webpage. WhatsApp 网页上没有名为“WhatsAppBot”的元素。

Unfortunately you did not give a lot of info to work with, however, I think what you want to achieve might be similar to this tutorial.不幸的是,您没有提供很多可使用的信息,但是,我认为您想要实现的目标可能与教程类似。 I used this myself when I tried to play around with sending automatic messages via Whatsapp.当我尝试通过 Whatsapp 发送自动消息时,我自己使用了这个。

The following code is what I used for my own small bot and largely follows the before mentioned tutorial:以下代码是我用于我自己的小型机器人的代码,主要遵循前面提到的教程:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


# SPECIFY PATH FOR CHROME DRIVER HERE
driverPath = 'D:chromedriver_win32\\chromedriver.exe'
# SPECIFY PATH FOR USER DIRECTORY (just change "TEST" to your WIN10 username)
userPath = "--user-data-dir=C:\\Users\\TEST\\AppData\\Local\\Google\\Chrome\\User Data"
# SPECIFY MESSAGES HERE
messages = ["Testmessage 1", "Testmessage 2"]


# bypass QR code login after first login
options = Options()
options.add_argument(userPath)
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option("excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(driverPath, options=options)

# open specified whatsapp chat
driver.maximize_window()
driver.get('https://web.whatsapp.com')
time.sleep(10)
driver.find_element_by_xpath("//*[@title='INSERT NAME OF CHAT HERE']").click()

# Send message
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div/div/div[2]/div[1]/div/div[2]').send_keys(messages[0])
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div/div/div[2]/div[2]/button').click()
driver.close()

Note that this code uses the chrome driver instead.请注意,此代码改为使用 chrome 驱动程序。

In the future the whatsapp layout might change, meaning the given xpaths could produce errors similar to yours because the element arrangement has changed.将来,whatsapp 布局可能会发生变化,这意味着给定的 xpath 可能会产生与您类似的错误,因为元素排列发生了变化。 In that case you will need to find the new xpath of the element you want to perform tasks on (ie click on it) and update it in your code.在这种情况下,您需要找到您想要执行任务的元素的新 xpath(即单击它)并在代码中更新它。 You can easily do that by pressing F12 while you are on Whatsapp Web, then hit Ctrl + Shift + C and click on the element.您可以通过在 Whatsapp Web 上按 F12 轻松做到这一点,然后按 Ctrl + Shift + C 并单击该元素。 The element is then highlighted on the right of the screen and if you right click on it you will see the option "copy -> copy xpath".然后该元素在屏幕右侧突出显示,如果您右键单击它,您将看到选项“复制 -> 复制 xpath”。 That's the path you will need to paste into the find_element_by_xpath function for it to work.这是您需要粘贴到 find_element_by_xpath function 才能使其工作的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM