如何在 WebDriver Edge 中保持对 Whatsapp Web 的关注而不丢失“在线”标签?

问题描述 投票:0回答:1

我正在 Edge 上使用 WebDriver(尽管可以使用任何浏览器)来管理 Whatsapp Web 上的一些测试? 我遇到过这样的情况:如果我不移动鼠标,Whatsapp Web 就会失去焦点,例如,即使其他用户在线,“在线”标签之类的内容也不会出现/消失。

我尝试使用 ActionChains 和 actions.move_by_offset(100, 50) 来模拟鼠标移动或使用 driver.execute_script('window.focus();') 执行 javascript 代码,但它不起作用。如果我自己不手动移动指针(基本上激活焦点),我似乎无法在另一个用户的屏幕上保留“在线”标签。

这是我当前的代码:

from selenium import webdriver
import time
import csv
from datetime import datetime
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains



# Path to your Microsoft Edge WebDriver executable
driver_path = 'THISISYOURPATH'


options = webdriver.EdgeOptions()

# Initialize the Edge WebDriver
driver = webdriver.Edge()

# Open WhatsApp Web
driver.get('https://web.whatsapp.com/')

# Wait for the user to scan QR code and log in
input('Press Enter after scanning QR code and logging in...')

def get_online_status():
    try:
        # Wait for the element to be visible on the page
        online_status_element = WebDriverWait(driver, 5).until(
            EC.visibility_of_element_located((By.XPATH, '//span[@title="en línea" or @title="escribiendo..."]'))
        )
        online_status = online_status_element.text
        return online_status
    except:
        return "Status not found"



# CSV file setup
csv_file_path = 'online_status_log.csv'
with open(csv_file_path, 'w', newline='') as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(['Timestamp'])


try:
    i = 0
    while True:
        i = i+1
        online = get_online_status()
        print(f'Online Status: {online}. Test {i}')

        if online.lower() == 'en línea' or online.lower() == 'escribiendo...':
            # Log the timestamp to the CSV file
            with open(csv_file_path, 'a', newline='') as csv_file:
                csv_writer = csv.writer(csv_file)
                csv_writer.writerow([datetime.now().strftime('%Y-%m-%d %H:%M:%S'), online])

         # Execute a JavaScript scroll on the page to trigger updates
        
        #driver.execute_script('window.focus();')
        # Create an ActionChains object
        actions = ActionChains(driver)

        if i % 2 == 0:
            actions.move_by_offset(100, 50)
        else:
            actions.move_by_offset(-100, -50)
        time.sleep(2)  # Check every 2 seconds

except KeyboardInterrupt:
    print("Stopping")
    # Close the browser when interrupted
    driver.quit()

有什么建议吗?可能是 Edge 配置问题? 非常感谢。

python selenium-webdriver webdriver microsoft-edge whatsapp
1个回答
0
投票

您可以尝试定期执行此代码来模拟用户交互。该脚本调度鼠标移动事件,这有助于保持会话活动。将此脚本集成到您的循环中,定期执行它。

js_script = """
document.dispatchEvent(new MouseEvent('mousemove', {
  view: window,
  bubbles: true,
  cancelable: true,
  clientX: 100,
  clientY: 100
}));
"""
driver.execute_script(js_script)
© www.soinside.com 2019 - 2024. All rights reserved.