python - 为什么我不能让 Selenium 使用 Python 3.8 和 Chrome 在我的 WhatsApp Web 中写消息?
问题描述
所以我使用这个脚本:
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)
Chrome 会按预期在 web.whatsapp.com 中打开 - 然后它会打开“蝙蝠侠”聊天。但是,它什么也没写。我检查了属性,甚至试图更具体地了解它们,但似乎没有任何效果。我在没有沙箱选项的情况下尝试了它,但仍然没有。
编辑:我从 py.exe 控制台得到的唯一消息是:
“[13920:4684:0130/154024.795:ERROR:wmi_refresher.cc(129)] 无法添加 Win32_PerfRawData_PerfDisk_PhysicalDisk 枚举。”
解决方案
我设法通过以下方式解决它:
- 下载正确的 chromedriver 版本,因为我正在使用 Chrome 79 并使用 Chrome 80 的驱动程序。
- 搜索 div 的所有属性,包括拼写检查、contenteditable 和 dir(我不知道这实际上有多相关,但以防万一)。
推荐阅读
- ssh - ssh 进入 OCI 中的计算实例 - 权限被拒绝
- sql - SQLServer - 在两个日期之间,生成年份、ISO 周以及开始和结束日期的表
- three.js - 将图像与点云对齐
- r - 测试混合模型中随机效应的显着性
- django - 为什么 Twilio 在埃及有效而在印度无效?
- r - R 和 Postgres,在 Postgres 函数中运行 R 脚本
- java - How to Serialize Java Object into json and Reverse Json to Object when using springboot with Redis?
- c++ - 为什么我收到错误:“使用已删除的函数”和静态原子变量?
- flutter - 我无法向按钮添加声音。在我的颤振应用程序中
- swiper.js - 你好,请问swiperjs如何安装和使用?