我正在使用这段代码来自动发送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.by import By
from selenium.webdriver.common.keys import Keys
import time
options=webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:/Users/Sameer/AppData/Local/Google/Chrome/User Data/Person 1")
# options.add_argument('--profile-directory=Default')
driver=webdriver.Chrome(executable_path='chromedriver.exe',options=options)
driver.get("https://web.whatsapp.com/")
wait=WebDriverWait(driver,100)
target='"My 2"'
message="Hello"
# number_of_times=10 #No. of times to send a message
contact_path='//span[contains(@title,'+ target +')]'
contact=wait.until(EC.presence_of_element_located((By.XPATH,contact_path)))
contact.click()
message_box_path='//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]/p'
# '//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]'
message_box=wait.until(EC.presence_of_element_located((By.XPATH,message_box_path)))
for x in range(5):
message_box.click()
message_box.clear()
message_box.send_keys(message)
message_box.send_keys(Keys.ENTER)
# time.sleep(0.2)
# print(x)
# message_box.send_keys(message + Keys.ENTER)
# time.sleep(0.2)
1条答案
按热度按时间lx0bsm1f1#
尝试在循环内定位
message_box
。另外,你必须改进你的定位器。
而且它应该是
element_to_be_clickable
预期条件存在,而不仅仅是presence_of_element_located
。所以,请尝试以下代码: