Python、pyautogui:如何明确地为 os 找到所需的 file/dir?

Python, pyautogui: how to explicitly for os to find required file/dir?

我正在编写 Selenium 测试并在需要上传图像时遇到问题,但是我想与之交互的 input 隐藏在视图中并且没有''属性。因此,我决定 使用 pyautogui 查找并附加图像 'manually'。

问题是我需要用到两个函数:

pyautogui.write()
pyautogui.press('enter')

最后一个不等待 OS 找到所需的 dir/file。 问题:如何使 pyautogui.press('enter') 函数等到文件被 OS 找到? 目前它是以某种方式通过 time.sleep(),但我不喜欢这种方法。

只有最后一段代码很重要,因此您可以跳过其余部分

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
import pyautogui
import os
from time import sleep

# set up 
chrome_service = Service(executable_path=ChromeDriverManager().install())
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)

# find the required element
driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
driver.get("https://www.online-image-editor.com/")
element = driver.find_element(By.CSS_SELECTOR, ".btn_upload > span")
element.click() # This opens the windows file selector

# find image on my pc like "home/uploads/test_image.jpeg"
JPEG = os.path.abspath(os.path.join(os.getcwd(), "uploads/test_image.jpeg"))

# split path to make it word by word like ["home", "uploads", "test_image.jpeg"]
path = JPEG.split("/")

for word in path:
   sleep(0.2)
   pyautogui.write(word)
   sleep(0.2)
   pyautogui.press("enter")

所以这里如果你删除睡眠,pyautogui.press("enter") 不会等待 OS 找到 'uploads' 并按下 enter,这会使测试失败.

您可以在 Windows 上尝试此解决方案:

while True:
    try:
        pyautogui.getWindowsWithTitle("Open")[0]
        break
    except IndexError:
        pass
pyautogui.write(JPEG)
pyautogui.press("enter")

“打开”这里是window的名称,选择文件

retry 库可以用来代替 while True 循环。你可以看看retry.retry_call函数

对于Linux你可以试试这个:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
import pyautogui
import os
from time import sleep

import sys
import subprocess
import re

def get_active_window_title():
    root = subprocess.Popen(['xprop', '-root', '_NET_ACTIVE_WINDOW'], stdout=subprocess.PIPE)
    stdout, stderr = root.communicate()

    m = re.search(b'^_NET_ACTIVE_WINDOW.* ([\w]+)$', stdout)
    if m != None:
        window_id = m.group(1)
        window = subprocess.Popen(['xprop', '-id', window_id, 'WM_NAME'], stdout=subprocess.PIPE)
        stdout, stderr = window.communicate()
    else:
        return None

    match = re.match(b"WM_NAME\(\w+\) = (?P<name>.+)$", stdout)
    if match != None:
        return match.group("name").strip(b'"')

    return None



# set up
chrome_service = Service(executable_path=ChromeDriverManager().install())
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)

# find the required element
driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
driver.get("https://www.online-image-editor.com/")
element = driver.find_element(By.CSS_SELECTOR, ".btn_upload > span")
element.click() # This opens the windows file selector

# find image on my pc like "home/uploads/test_image.jpeg"
JPEG = os.path.abspath(os.path.join(os.getcwd(), "uploads/test_image.jpeg"))

while True:
    try:
        assert b"Open Files" in get_active_window_title()
        break
    except AssertionError:
        pass
pyautogui.write(JPEG)
pyautogui.press("enter")

Source