gpt4 book ai didi

python - 如何在网速慢时停止pyautogui并恢复之前的程序代码

转载 作者:太空宇宙 更新时间:2023-11-03 21:29:31 25 4
gpt4 key购买 nike

我有一个关于 Pyautogui 的问题。我想点击按钮/链接/图像,但在我的国家,互联网速度很慢。所以我想推迟这个计划。但有时它不起作用。该程序在链接/图像/按钮出现在网站上之前单击它。这就是为什么我无法继续或整个程序失败的原因。

程序有什么方法可以定位按钮/链接/图像是否显示?如果未显示,则应再等待 30 秒或 1 分钟。这就是我希望我的程序每次都能运行的方式。我真的很想解决这个问题。我希望你能回复。

最佳答案

如果您正在查看一堆具有不同内容的不同网页,这可能会很棘手。但是,如果您知道必须存在的特定图像或链接文本,那么应该没问题。

下面的 max_wait 是为了让程序不会无限循环。将其设置为您想要的大小。

对于您知道必须在页面中显示的特定图像,您可以尝试

import time
import pyautogui

found_image = False
max_wait = 120

x = time.time()
while (found_image == False) and time.time() - x < max_wait:
if (pyautogui.locateOnScreen('my_png.png') is not None):
found_image = True

if found_image: do_main_test()

对于文本,您可以使用 pyautogui 选择所有...

import time
import pyautogui
import pyperclip

x = time.time()
found_text = False
max_wait = 120

while (found_text == False) and time.time() - x < max_wait:
pyautogui.click(150, 150) # or wherever is in the window
pyautogui.hotkey('ctrl', 'a')
pyautogui.hotkey('ctrl', 'c')
pyautogui.click(x=150, y=150)
lines = pyperclip.paste()
found_text = needed_text in lines
if not found_text: sleep(5) # this is so pyautogui isn't continually highlighting etc.

if found_text: do_main_test()

我不了解跟踪按钮,但我希​​望文本和图像示例有用。当然,您可以指定仅当 found_textfound_image 为 true 时才运行 do_main_test()

关于python - 如何在网速慢时停止pyautogui并恢复之前的程序代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53611445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com