Python自动重试加入满员会议脚本出现TypeError: 'NoneType' object is not subscriptable错误求助
Hey there! Let's break down why this error is popping up and get your script running reliably in a loop.
What's Causing the Error?
The error TypeError: 'NoneType' object is not subscriptable on line 13 (x=posid[0]) tells us that posid is None. This happens when pyautogui.locateOnScreen('id.png', confidence=.6) can't find the id.png image on your screen — it returns None instead of a position tuple, and you can't index ([0]) a None value.
Why It Works Once But Not In a Loop
When you run just join() once, your screen is in the exact state the script expects: the join button is visible, and after clicking it, the ID input screen loads quickly enough that sleep(1) gives it time to appear.
But when you run the loop:
- After the first full pass (join → enter ID → enter passcode → choose video → handle capacity error), your screen might not reset to the initial state where the join button is visible again.
- Even if it does, the timing might be off — the fixed
sleep(1)afterjoin()might not be long enough for the ID input screen to fully load the second time, solocateOnScreencan't findid.png.
How to Fix It
We need to add safety checks to handle cases where images aren't found, and make the script wait dynamically for elements to appear instead of relying on rigid sleep times. Here's a revised version of your code with these fixes:
Modified Working Code
import pyautogui as pt from time import sleep sleep(2) def wait_for_image(image_path, confidence=0.6, max_retries=5, retry_delay=0.5): """Helper function to wait for an image to appear on screen with retries""" pos = None for _ in range(max_retries): pos = pt.locateOnScreen(image_path, confidence=confidence) if pos is not None: return pos sleep(retry_delay) print(f"Failed to find image: {image_path} after {max_retries} retries") return None def join(): posjoin = wait_for_image('join.png') if posjoin is None: return x = posjoin[0] y = posjoin[1] pt.moveTo(x+20, y+5) pt.click() def enterid(): posid = wait_for_image('id.png') if posid is None: return x = posid[0] y = posid[1] pt.moveTo(x+20, y+5) pt.click() pt.typewrite('meetingid') pt.moveRel(929-780, 459-251) pt.click() def passcode(): pospass = wait_for_image('pass.png') if pospass is None: return x = pospass[0] y = pospass[1] pt.moveTo(x+20, y+5) pt.click() pt.typewrite('meetingpassword') pt.moveRel(826-686, 498-291) pt.click() def choose(): poscho = wait_for_image('video.png') if poscho is None: return x = poscho[0] y = poscho[1] pt.moveTo(x+20, y+5) pt.click() def capacity(): poscap = wait_for_image('capacity.png') if poscap is None: return x = poscap[0] y = poscap[1] pt.moveTo(x+316, y+266) pt.click() a = 1 while a < 3: join() sleep(1) enterid() sleep(1.5) passcode() sleep(3.5) choose() sleep(3.5) capacity() # Add a delay to ensure the error window closes and we're back to the initial state sleep(2) a += 1
Extra Tips to Improve Reliability
- Verify Your Images: Make sure
id.png,join.png, etc., are precise screenshots of the exact elements you're targeting — no overlapping windows or extra content. - Tweak Confidence: If images are still hard to find, adjust the
confidenceparameter (e.g., 0.5 for more lenient matching, 0.7 for stricter). - Replace Fixed Sleeps: For even better reliability, extend the
wait_for_imagehelper to handle waiting for post-click state changes instead of relying on arbitrarysleeptimes.
内容的提问来源于stack exchange,提问作者Ahmet Bilgin

