简体   繁体   中英

Why can't I make Selenium write a message in my WhatsApp Web using Python 3.8 and Chrome?

So I use this script:

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 
import time 

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome('C:/Users/N-Thalpy/Documents/NT/py/chromedriver.exe', chrome_options=chrome_options) 

driver.get("https://web.whatsapp.com/") 
wait = WebDriverWait(driver, 600) 


target = '"Batman"'

string = "Python 3.8"

x_arg = '//span[contains(@title,' + target + ')]'
group_title = wait.until(EC.presence_of_element_located(( 
    By.XPATH, x_arg))) 
group_title.click() 
inp_xpath = '//div[@class="_3u328"][@dir="ltr"][@data-tab="1"]'
input_box = wait.until(EC.presence_of_element_located(( 
    By.XPATH, inp_xpath))) 
for i in range(100): 
    input_box.send_keys(string + Keys.ENTER) 
    time.sleep(1) 

And Chrome opens in web.whatsapp.com as expected - then it opens "Batman's" chat. However, it doesn't write anything. I checked the attributes and even tried to be more specific about them, but nothing seems to work. I tried it without sandbox option and still nothing.

Edit : the only message I get from the py.exe console is this:

"[13920:4684:0130/154024.795:ERROR:wmi_refresher.cc(129)] Unable to add the Win32_PerfRawData_PerfDisk_PhysicalDisk enum."

I managed to solve it by:

  • Downloading the correct chromedriver version as I was using Chrome 79 and using the driver for Chrome 80.
  • Searching for ALL the attributes of the div, includin spellcheck, contenteditable and dir (I don't how relevant this actually is, but just in case).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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