You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pathlib路径作为pyautogui.click()参数触发TypeError错误求助

问题解决:TypeError: int() argument must be a string... not 'PureWindowsPath'

问题原因

PyAutoGUI的click()方法无法直接接收PureWindowsPath(或其他Path类对象)作为图片路径参数,它需要的是字符串格式的路径。你传入的路径对象类型不匹配,导致了类型错误。

解决方案

将Path对象转换为字符串后传入click()即可,有两种常用方式:

方式1:直接用str()转换路径对象

修改代码最后一行:

pya.click(str(imageFolder / "tiaoseban.png"))

方式2:改用Path类替代PureWindowsPath(跨系统更友好)

PureWindowsPath是仅用于Windows路径的纯路径对象,而Path会根据当前系统自动适配,使用起来更灵活。完整修改后的代码:

import pyautogui as pya
from pathlib import Path

imageFolder = Path(__file__).parent / 'images'
print(imageFolder)
pya.PAUSE = 2
# 转换为字符串传入click方法
pya.click(str(imageFolder / "tiaoseban.png"))

补充说明

如果需要确保路径格式符合Windows风格,也可以用imageFolder / "tiaoseban.png"的as_posix()方法(会返回正斜杠分隔的字符串路径),但str()已经足够满足PyAutoGUI的需求。

内容的提问来源于stack exchange,提问作者Jimmy Xee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 01:31:07