简体   繁体   English

我收到错误和回溯尝试发送带有 selenium 的 whatsapp

[英]I get an error and a traceback trying to send a whatsapp with selenium

I im tring to send a whatsapp with selenium and chromedriver, but when i run the code I get an error and a traceback.我想用 selenium 和 chromedriver 发送一个 whatsapp,但是当我运行代码时,我得到一个错误和一个回溯。

I have tried everything that I could (I know a little of python, i'm lerning, and i know like nothing about selenium, but i have to get this working as soon as posible), I also have goggle a lot of this and tried a lot of things but i could not make this work.我已经尽我所能(我知道一点python,我正在学习,我对selenium一无所知,但我必须尽快让它工作),我也有很多这样的护目镜和尝试了很多东西,但我无法完成这项工作。

If someone can help me I would be very grateful.如果有人可以帮助我,我将不胜感激。

The error is this:错误是这样的:

[WDM] - ====== WebDriver manager ====== [WDM] - Current google-chrome version is 89.0.4389 [WDM] - Get LATEST driver version for 89.0.4389 [WDM] - Driver [C:\Users\Crist.wdm\drivers\chromedriver\win32\89.0.4389.23\chromedriver.exe] found in cache [WDM] - ====== WebDriver manager ====== [WDM] - 当前 google-chrome 版本是 89.0.4389 [WDM] - 获取 89.0.4389 [WDM] 的最新驱动程序版本 - 驱动程序 [C :\Users\Crist.wdm\drivers\chromedriver\win32\89.0.4389.23\chromedriver.exe] 在缓存中找到

DevTools listening on ws://127.0.0.1:56076/devtools/browser/0c5e0486-3e61-47d1-80f0-7ecfe21ffb6e Scan QR Code, And then Enter DevTools 监听 ws://127.0.0.1:56076/devtools/browser/0c5e0486-3e61-47d1-80f0-7ecfe21ffb6e 扫描二维码,然后回车

Logged In [14880:8104:0320/205615.019:ERROR:device_event_log_impl.cc(214)] [20:56:15.018] USB: usb_device_handle_win.cc:1056 Failed to read descriptor from node connection: Uno de los dispositivos conectados al sistema no funciona.登录 [14880:8104:0320/205615.019:ERROR:device_event_log_impl.cc(214)] [20:56:15.018] USB:usb_device_handle_win.cc:1056 无法从节点连接读取描述符:Uno de losectema dispositivos功能。 (0x1F) [14880:8104:0320/205615.046:ERROR:device_event_log_impl.cc(214)] [20:56:15.046] Bluetooth: bluetooth_adapter_winrt.cc:1072 Getting Default Adapter failed. (0x1F) [14880:8104:0320/205615.046:ERROR:device_event_log_impl.cc(214)] [20:56:15.046] 蓝牙:bluetooth_adapter_winrt.cc:1072 获取默认适配器失败。 Traceback (most recent call last): File "C:\Users\Crist\Desktop\Whattsapp mama\prueba.py", line 19, in input_box_search = WebDriverWait(driver,50).until(lambda driver: driver.find_element_by_xpath(inp_xpath_search)) File "C:\Users\Crist\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:回溯(最后一次调用):文件“C:\Users\Crist\Desktop\Whattsapp mama\prueba.py”,第 19 行,在 input_box_search = WebDriverWait(driver,50).until(lambda driver: driver.find_element_by_xpath(inp_xpath_search) )) 文件“C:\Users\Crist\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py”,第 80 行,直到引发 TimeoutException(消息,屏幕,堆栈跟踪)selenium.common.exceptions.TimeoutException:消息:

C:\Users\Crist\Desktop\Whattsapp mama>[14880:8104:0320/210116.161:ERROR:device_event_log_impl.cc(214)] [21:01:16.161] USB: usb_service_win.cc:258 Failed to get device driver name: No se ha encontrado el elemento. C:\Users\Crist\Desktop\Whattsapp mama>[14880:8104:0320/210116.161:ERROR:device_event_log_impl.cc(214)] [21:01:16.161] USB: usb_service_win.cc:258 Failed to get device driver name :没有se ha encontrado el elemento。 (0x490) (0x490)

And the code is this (it´s just what it takes to make it work):代码是这样的(它只是让它工作所需要的):

contact = ["cristobal de toro", "cristobal", "gonzalo molina"]
text = "Hey, this message was sent using Selenium"
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://web.whatsapp.com")
print("Scan QR Code, And then Enter")
input()
print("Logged In")
inp_xpath_search = "//input[@title='Search or start new chat']"
input_box_search = WebDriverWait(driver,50).until(lambda driver: driver.find_element_by_xpath(inp_xpath_search))
input_box_search.click()
time.sleep(2)
input_box_search.send_keys(contact)
time.sleep(2)
selected_contact = driver.find_element_by_xpath("//span[@title='"+contact+"']")
selected_contact.click()
inp_xpath = '//div[@class="_2S1VP copyable-text selectable-text"][@contenteditable="true"][@data-tab="1"]'
input_box = driver.find_element_by_xpath(inp_xpath)
time.sleep(2)
input_box.send_keys(text + Keys.ENTER)
time.sleep(2)
driver.quit()

So I looked at the code for the whatsapp web version, and it seems like each persons contact is assigned to a random uid each time you refresh the page (at least in my case).因此,我查看了 whatsapp web 版本的代码,似乎每次刷新页面时,每个联系人都被分配了一个随机 uid(至少在我的情况下)。 This means that you would need to use something like pyautogui to click on a predictable spot on the page.这意味着您需要使用 pyautogui 之类的工具来单击页面上可预测的位置。 As for the actual input area, it's not even an input, so selenium wouldn't work in this case.至于实际的输入区域,它甚至不是输入,所以 selenium 在这种情况下是行不通的。 I just wrote some example code to show how this would work - I'm on a 13" Laptop, so you may need to fix the coordinates with我刚刚写了一些示例代码来展示它是如何工作的——我在一台 13 英寸的笔记本电脑上,所以你可能需要用

pyautogui.position()

Install pyautogui with:使用以下命令安装 pyautogui:

python3 -m pip install pyautogui

Heres the example code:下面是示例代码:

import pyautogui
import os
import time

FIRSTX = 155
FIRSTY = 268

INPUTX = 605
INPUTY = 696






os.system('open https://web.whatsapp.com/')
time.sleep(7)

#Click on the first person in your contacts
pyautogui.click(x=FIRSTX, y=FIRSTY)
time.sleep(1)


#Click the input
pyautogui.click(x=INPUTX, y=INPUTY)
pyautogui.write("Your Message")
pyautogui.press('enter')

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

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