You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Windows10下用Selenium+Python通过WhatsApp Web发送PDF的问题

Fixing WhatsApp Web File Attachment via Selenium + Win32gui

I see where you're hitting snags here—let's break down the issues in your current code and fix them step by step:

Key Problems in Your Current Approach

  • Hardcoded Control ID: The control ID 0x000047C you're using isn't guaranteed to be the same across different Windows installations or even different dialog sessions. You need to dynamically find the correct edit field in the "Abrir" dialog.
  • Incorrect Enter Simulation: Using SetDlgItemText to send "{ENTER}" won't work—this method only sets the text content of a control, it doesn't simulate pressing a key. You need to use key press or dialog command APIs instead.

Corrected Code Implementation

Here's an updated version of your code that addresses these issues:

import win32gui
import win32api
import win32con
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

send_to = 'John Smith'
attach_name = "GyG Recibo_00741.pdf" # Use full path if file isn't in script's working directory

# Initialize Chrome driver
web_driver = webdriver.Chrome('ChromeDriver')
web_driver.get('http://web.whatsapp.com')
web_driver.implicitly_wait(100) # Wait for QR code scan

# Find recipient and open chat
new_chat = web_driver.find_element_by_id('input-chatlist-search')
new_chat.send_keys(send_to, Keys.ENTER)

# Click Attach button
attach_button = web_driver.find_element_by_xpath('//*[@id="main"]/header/div[3]/div/div[2]/div/span')
attach_button.click()

# Click Document option
doc_button = web_driver.find_element_by_xpath('//*[@id="main"]/header/div[3]/div/div[2]/span/div/div/ul/li[3]/button')
doc_button.click()

# Wait for the "Abrir" dialog to appear (with small sleep to reduce CPU load)
hdlg = 0
while hdlg == 0:
    hdlg = win32gui.FindWindow(None, "Abrir")
    time.sleep(0.5)

# Function to dynamically find the filename edit control in the dialog
def find_edit_control(parent_handle):
    def child_callback(handle, extra):
        if win32gui.GetClassName(handle) == "Edit":
            extra.append(handle)
        return True
    child_controls = []
    win32gui.EnumChildWindows(parent_handle, child_callback, child_controls)
    return child_controls[0] if child_controls else None

# Get the edit control handle
edit_handle = find_edit_control(hdlg)
if edit_handle:
    # Set the filename in the input field
    win32gui.SetDlgItemText(hdlg, win32gui.GetDlgCtrlID(edit_handle), attach_name)
    # Simulate pressing "Abrir" (equivalent to Enter key)
    win32api.SendMessage(hdlg, win32con.WM_COMMAND, win32con.IDOK, 0)
else:
    print("Could not locate the filename input field in the dialog.")

# Optional: Clean up after sending
# web_driver.quit()

What's Changed?

  1. Dynamic Control Discovery: The find_edit_control function uses EnumChildWindows to locate the "Edit" class control in the dialog, so you don't rely on a fixed, unreliable control ID.
  2. Reliable Confirmation: Instead of trying to set text as Enter, we send the IDOK command to the dialog—this is the proper way to trigger the "Open" action, which works consistently across Windows versions.
  3. Reduced CPU Usage: Added a small time.sleep(0.5) in the dialog wait loop to avoid hogging your CPU while waiting for the dialog to appear.

Additional Tips

  • If your file isn't in the same folder as your script, use the full absolute path for attach_name (e.g., C:/Users/YourName/Documents/GyG Recibo_00741.pdf).
  • WhatsApp Web's DOM structure might change over time—if your XPATH selectors stop working, inspect the page to update them.
  • If the dialog still doesn't respond, you can try simulating an Enter key press directly on the edit control using win32api.keybd_event(win32con.VK_RETURN, 0, 0, 0) after setting the filename.

内容的提问来源于stack exchange,提问作者M.Colosso

火山引擎 最新活动