用于发送批量 whatsapp 消息的代码 python/selenium - 始终无法找到文本框的元素

Code python/selenium to send bulk whatsapp messages - continiously unable to locate element for text box

查看了许多选项和其他代码。下面代码中的所有内容都运行顺利,直到 whatsapp 输入框被调用:

*input_box = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')*

我也调整为:

*input_box = driver.find_element(By.XPATH, '//*@id="main"]/footer/div[1]/div[2]/div/div[2]')*

没有任何成功。我收到以下错误:


Klaas Malan *(my contact)*
As jy hierdie teks kry, dan werk die program wat ek vir Heiko skrywe. *(Message in Afrikaans)*
https://web.whatsapp.com/send?phone=Klaas Malan&text=As+jy+hierdie+teks+kry%2C+dan+werk+die+program+wat+ek+vir+Heiko+skrywe.&source=&data=
Sending message to Klaas Malan
**Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="main"]/footer/div[1]/div[2]/div/div[2]"}**
  (Session info: chrome=101.0.4951.54)
Failed to send message

下面是我的完整代码:

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
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains

import datetime
import time
import openpyxl as excel
import urllib.parse

# function to read contacts from a text file
def readContacts(fileName):
    lst = []
    file = excel.load_workbook(fileName)
    sheet = file.active
    firstCol = sheet['A']
    secondCol = sheet['B']
    driver  = webdriver.Chrome()
    driver.get('https://web.whatsapp.com')
    time.sleep(60)

    for cell in range(len(firstCol)):
        contact = str(firstCol[cell].value)
        message = str(secondCol[cell].value)
        print(contact)
        print(message)
        link = "https://web.whatsapp.com/send phone="+contact+"&text="+urllib.parse.quote_plus(message)+"&source=&data="
        print(link)  
        driver.get(link)
        time.sleep(4)
        print("Sending message to", contact)

        try:
            time.sleep(7)
            input_box = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
            for ch in message:
                if ch == "\n":
                    ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.ENTER).key_up(Keys.SHIFT).key_up(Keys.BACKSPACE).perform()
                else:
                    input_box.send_keys(ch)
            input_box.send_keys(Keys.ENTER)
            print("Message sent successfuly")
        except NoSuchElementException as exc:
            print(exc) # and/or other actions to recover
            print("Failed to send message")

targets = readContacts("./contacts-message.xlsx")

有没有人有建议或愿意分享他们的代码? 最良好的祝愿,伙计们。我是 Python 的新手,是纳米比亚的一名农民,我的目标是向我的每个牛买家发送单独的消息,感谢他们。 荣格

根据消息框的html,我可以找到以下内容:

<div title="Type a message" role="textbox" class="_13NKt copyable-text selectable-text" contenteditable="true" data-tab="10" dir="ltr" spellcheck="true"></div>

Screen shot of Whatsapp

上图,Whatsapp 的屏幕截图 - 寻找元素。

您试过通过文本查找元素吗?

input_box = driver.find_element_by_xpath('//*[text()="Type a message"]')

或者您可以使用 contains() 来查找元素:

input_box = driver.find_element_by_xpath('//*[contains(@class, "copyable-text selectable-text")]'

好的,抱歉。如果我这样做,@jfleach 建议有效:

input_box = driver.find_elements(By.XPATH, '//*[text() = "Type a message"]')

当然,现在又出现了一个新问题:

  File "C:\Users\aegju\.00. Python Projects\Shantanusk Whatsapp\Bulk Shanatanusk.py", line 64, in <module>
targets = readContacts("./contacts-message.xlsx")

  File "C:\Users\aegju\.00. Python Projects\Shantanusk Whatsapp\Bulk Shanatanusk.py", line 50, in readContacts
input_box.send_keys(ch)

AttributeError: 'list' object has no attribute 'send_keys'

或者,作为新手,我应该 post 一个新问题吗?