Python while/if 循环忽略条件

标签 python while-loop pyautogui

我正在编写一个基本的 while/if 循环,当 nextbutton.png 不在屏幕上时向下滚动页面,并在按钮打开时停止滚动屏幕。

如果按钮不在屏幕上,它应该打印 True ,如果在屏幕上,则打印按钮的位置。 问题是,当我运行此代码时,我得到以下输出:

True
True
True
True
True
True
True
True
True
True
True
True
True
True
False

按照此代码的编写方式,它永远不应该输出 FalseTrue 或位置。我可以向我正在尝试执行此操作的网站提供有关如何复制输出的说明,但我怀疑我在构建循环时犯了一个简单的逻辑错误。

import pyautogui
while True:
    if pyautogui.locateOnScreen('nextbutton.png', confidence=0.9) is None:
        print(pyautogui.locateOnScreen('nextbutton.png', confidence=0.9) is None)
        time.sleep(random.randint(0,3000)/1000) 
        pyautogui.press('pagedown')
    else:
        x4, y4 = pyautogui.locateCenterOnScreen('nextbutton.png', confidence=0.9)
        print(x4,y4)
        break

最佳答案

您调用函数 locateOnScreen() 2 次,每次可能会返回不同的输出。

我建议使用一个变量来保存 locateOnScreen() 的输出,以避免出现竞争情况。也许这对你有用:

import pyautogui

while True:
    v = pyautogui.locateOnScreen('nextbutton.png', confidence=0.9)
    if v is None:
        print(v is None)
    ...

关于Python while/if 循环忽略条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56757897/

相关文章:

python - 指示小时较小的 TimeField 的最佳方法是 'after' 小时较大的 TimeField

python - key 错误 : 'DISPLAY' when installing pyautogui on Repl.

python - 在 PyTorch 中创建具有多个 channel 的简单一维 CNN

python - 如何使用H2o库获取GBM模型的数据

python - 如何使用 python 最好使用 pil 检测 tif 图像是否有图层

python - Pyautogui 在游戏窗口中不起作用

python - PyAutoGUI 图像识别 - 像素颜色匹配的小问题

javascript - 循环提示用户直到有效输入

python - 使用列表项将列表项转换为字典

python - 列表和 While 循环