我正在编写一个基本的 while/if
循环,当 nextbutton.png
不在屏幕上时向下滚动页面,并在按钮打开时停止滚动屏幕。
如果按钮不在屏幕上,它应该打印 True
,如果在屏幕上,则打印按钮的位置。
问题是,当我运行此代码时,我得到以下输出:
True
True
True
True
True
True
True
True
True
True
True
True
True
True
False
按照此代码的编写方式,它永远不应该输出 False
、True
或位置。我可以向我正在尝试执行此操作的网站提供有关如何复制输出的说明,但我怀疑我在构建循环时犯了一个简单的逻辑错误。
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/