简体   繁体   English

为什么我不能让 Selenium 使用 Python 3.8 和 Chrome 在我的 WhatsApp Web 中编写消息?

[英]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. Chrome 按预期在 web.whatsapp.com 中打开 - 然后打开“蝙蝠侠”聊天。 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:编辑:我从 py.exe 控制台收到的唯一消息是:

"[13920:4684:0130/154024.795:ERROR:wmi_refresher.cc(129)] Unable to add the Win32_PerfRawData_PerfDisk_PhysicalDisk enum." “[13920:4684:0130/154024.795:ERROR:wmi_refresher.cc(129)] 无法添加 Win32_PerfRawData_PerfDisk_PhysicalDisk 枚举。”

I managed to solve it by:我设法通过以下方式解决它:

  • Downloading the correct chromedriver version as I was using Chrome 79 and using the driver for Chrome 80.下载正确的 chromedriver 版本,因为我使用的是 Chrome 79 并使用 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).搜索 div 的所有属性,包括拼写检查、contenteditable 和 dir(我不知道这实际上有多相关,但以防万一)。

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

相关问题 使用 selenium 和 python3.8 加入 whatsapp 组时出现 NoSuchElementException - NoSuchElementException while joining whatsapp group using selenium and python3.8 如何使用 python 3.8 selenium 单击 href 按钮? - How can I click a href button using python 3.8 selenium? Python 和 Selenium 与 Whatsapp Web - Python and Selenium with Whatsapp Web github和Python 3.8有问题吗? 为什么我不能运行这段代码? - Is there a problem with github and Python 3.8? Why can't I run this code? 尝试在 python 3.8 中使用 selenium 在我的个人资料上打开 chrome - Trying to open chrome on my profile with selenium in python 3.8 仅当代码使用 Selenium 和 Python 3.8 在页面上找到某个单词时,如何才能启动代码序列? - How can I make to start a code sequence only if the code finds a certain word on page using Selenium with Python 3.8? 为什么我不能用硒打开铬? - Why can't i open chrome with selenium? Python + Selenium:Chrome 驱动程序无法写入 cookies 文件夹 - Python + Selenium: Chrome driver can't write to cookies folder WhatsApp 使用 Python Selenium 的多行消息 - MultiLine Message With Python Selenium for WhatsApp 为什么我的 Selenium 选择器会拉字典? (Django/Python 3.8) - Why is my Selenium selector pulling a dict? (Django/Python 3.8)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM