[英]How do I find the message options button on whatsapp with selenium
I want to automate the action of deleting some messages, how can I find a down-context button to delete?我想自动化删除一些消息的操作,我怎样才能找到要删除的上下文按钮?
Here's the code.这是代码。 The error is that I can't find the down-context to delete the message btn_option = driver.find_element_by_xpath('//span[@data-icon="down-context"]')
错误是我找不到删除消息btn_option = driver.find_element_by_xpath('//span[@data-icon="down-context"]') 的下行上下文
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
Python Constants Python 常数
WPP_URL = "https://web.whatsapp.com/"
NEW_CHAT = '//*[@id="side"]/header/div[2]/div/span/div[2]/div/span'
SEARCH_CONTACT = '//*[@id="app"]/div[1]/div[1]/div[2]/div[1]/span/div[1]/span/div[1]/div[1]/div/label/div/div[2]'
FIRST_CONTACT= '//*[@id="app"]/div[1]/div[1]/div[2]/div[1]/span/div[1]/span/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/div[1]/div/span'
TYPE_MSG = '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]'
SEND_BUTTON = '//*[@id="main"]/footer/div[1]/div[3]/button/span'
OPTION_BTN = '//*[@id="main"]/div[3]/div/div/div[2]/div[10]/div/div/div'
DELETE_BTN = '//*[@id="app"]/div[1]/span[4]/div/ul/div/li[5]'
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(WPP_URL)
def send_message(contato,mensagem):
new_msg_button = driver.find_element_by_xpath(NEW_CHAT)
new_msg_button.click()
sleep(1)
search_field = driver.find_element_by_xpath(SEARCH_CONTACT)
search_field.click()
search_field.send_keys(contato)
sleep(1)
first_contact = driver.find_element_by_xpath(FIRST_CONTACT)
first_contact.click()
sleep(1.5)
type_field = driver.find_element_by_xpath(TYPE_MSG)
type_field.click()
type_field.send_keys(mensagem)
send_msg= driver.find_element_by_xpath(SEND_BUTTON)
send_msg.click()
sleep(1)
Delete message function删除消息 function
def delete_message(contato):
new_msg_button = driver.find_element_by_xpath(NEW_CHAT)
new_msg_button.click()
sleep(1)
search_field = driver.find_element_by_xpath(SEARCH_CONTACT)
search_field.click()
search_field.send_keys(contato)
sleep(1)
first_contact = driver.find_element_by_xpath(FIRST_CONTACT)
first_contact.click()
sleep(3)
**btn_option = driver.find_element_by_xpath('//span[@data-icon="down-context"]')**
btn_option.click()
sleep(2)
btn_delete = driver.find_element_by_class_name("_11srW _2xxet _3lsW-")
btn_delete.click()
msg_padrão = "Olá {}, tudo bem com você? Seu codigo de rastreamento é {}"
lista_contatos = ["João bot","Júlia bot","João bot", "Julia bot"]
lista_codigos = ["12312513","534534743","62354236323","234234226"]
for contato,codigo in zip(lista_contatos,lista_codigos):
send_message(contato,msg_padrão.format(contato,codigo))
contatos_recentes = ["João bot","Júlia bot"]
for contato in contatos_recentes:
delete_message(contato)
The steps to delete your the last sent message are:删除您最后发送的消息的步骤是:
You need to open the dialog by clicking this button (//div[contains(@class,'message-out')]//div[@role='button'])[last()]
您需要通过单击此按钮打开对话框
(//div[contains(@class,'message-out')]//div[@role='button'])[last()]
However, you have to hover over this element to click on it.但是,您必须在此元素上 hover 才能单击它。 So you need to import this:
所以你需要导入这个:
from selenium.webdriver.common.action_chains import ActionChains
and then接着
btn_option = driver.find_element_by_xpath("(//div[contains(@class,'message-out')]//div[@role='button'])[last()]")
hover = ActionChains(firefox).move_to_element(btn_option)
hover.perform()
btn_option.click()
Then select Delete message
option by clicking on //div[@aria-label='Delete message']
element.然后 select 通过单击
//div[@aria-label='Delete message']
元素Delete message
选项。
Now you can click (//div[@data-animate-modal-popup='true']//div[@role='button'])[last()]
if you wish to delete that message for everyone or (//div[@data-animate-modal-popup='true']//div[@role='button'])[1]
to delete it for you only.现在您可以单击
(//div[@data-animate-modal-popup='true']//div[@role='button'])[last()]
如果您希望为所有人删除该消息或(//div[@data-animate-modal-popup='true']//div[@role='button'])[1]
只为您删除它。
BTW, I see you are using very long and complex css
selectors.顺便说一句,我看到您使用的是非常长且复杂
css
选择器。 It's not the best practice.这不是最佳做法。
The final code to delete:最后要删除的代码:
def delete_message(contato):
#Open new chat on whatsapp web
new_msg_button = driver.find_element_by_xpath(NEW_CHAT)
new_msg_button.click()
sleep(1)
#Search the contact
search_field = driver.find_element_by_xpath(SEARCH_CONTACT)
search_field.click()
search_field.send_keys(contato)
sleep(1)
#Click on the firts contact with the name that I told
first_contact = driver.find_element_by_xpath(FIRST_CONTACT)
first_contact.click()
sleep(2)
#open the dialog by clicking this button
btn_option = driver.find_element_by_xpath("(//div[contains(@class,'message-out')]//div[@role='button'])[last()]")
hover = ActionChains(driver).move_to_element(btn_option)
hover.perform()
dialog_click = driver.find_element_by_xpath('//*[@id="main"]/div[3]/div/div/div[3]/div[16]/div[1]/div/span[2]/div')
dialog_click.click()
sleep(1.5)
#Then select Delete message option by clicking on
btn_option_delete = driver.find_element_by_xpath('//*[@id="app"]/div[1]/span[4]/div/ul/div/li[5]')
btn_option_delete.click()
sleep(2)
#delete that message for everyone
delete = driver.find_element_by_xpath("(//div[@data-animate-modal-popup='true']//div[@role='button'])[last()]")
delete.click()
sleep(2)
#Confirm the option
finish = driver.find_element_by_xpath('//*[@id="app"]/div[1]/span[2]/div[1]/span/div[1]/div/div/div/div/div[2]/div[2]/div/div')
finish.click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.