简体   繁体   English

Python 和 Selenium 与 Whatsapp Web

[英]Python and Selenium with Whatsapp Web

I'm trying to make a bot that allows me to send any message for a lot of ppl.我正在尝试制作一个允许我为很多人发送任何消息的机器人。 through web.whatsapp.com i can do that, but my issue here with the class name:通过web.whatsapp.com我可以做到,但我的问题是 class 名称:

this is Type a message's class, you can see it clearly: _3FRCZ copyable-text selectable-text , so my issue here that i cannot select it, idk why, i tried to select deffrent classes but still same issue.这是键入消息的 class,您可以清楚地看到它: _3FRCZ copyable-text selectable-text ,所以我在这里的问题是我不能 select 它,idk 为什么,我尝试了 select deffrent 类,但仍然是同样的问题。 here is my code:这是我的代码:

from selenium import webdriver

driver = webdriver.Firefox()
content = "Hi There"
phone = "+96657831111"
driver = webdriver.Firefox()
driver.get(f"https://web.whatsapp.com/send?phone={phone}&text&source&data&app_absent")
input("Press Enter After Scanning..")
send = driver.find_element_by_class_name("_3FRCZ copyable-text selectable-text")
send.send_keys("Test")

Notes:笔记:

  • for me i dont have to set geckdriver.对我来说,我不必设置 geckdriver。
  • my python verion is 3.8我的 python 版本是3.8

EDIT: I used xpath to do it //div[@class='_3FRCZ copyable-text selectable-text'] and it worked, but with search box not messages box.编辑:我使用 xpath 来做//div[@class='_3FRCZ copyable-text selectable-text']并且它有效,但搜索框不是消息框。 i realized that search box and message box has same class.我意识到搜索框和消息框具有相同的 class。

I fixed it by using CSS Selector, You cannot use xpath or find element by class name, bc there is many elements using the same class name, so here is the best choice:我使用 CSS 选择器修复了它,您不能使用 xpath 或通过 class 名称查找元素,因为有很多元素使用相同的 class 名称,所以这里是最佳选择:

send = driver.find_element_by_css_selector("div[data-tab='6']")
send.send_keys(msg,Keys.ENTER)

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

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