from uiautomation import WindowControl, MenuControl import re import numpy as np wx = WindowControl(Name='微信') wx.SwitchToThisWindow() hw = wx.ListControl(Name='会话') print('寻找会话控制绑定', hw) # 定义包含数字的列表 numbers = [1234567890, 20000000000, 30000000000] while True: we = hw.TextControl(searchDepth=4) while not we.Exists(8): pass print('查找未读消息', we) if we.Name: we.Click(simulateMove=False) messages = wx.ListControl(Name='消息').GetChildren() # 遍历每个消息控件,获取消息内容并进行处理 for message in messages: last_msg = message.Name print('读取消息', last_msg) new_numbers = [] # 使用正则表达式匹配符合条件的数字 for num in re.findall(r'\b\d{11}\b', last_msg): if int(num) > 10000000000 and int(num) < 20000000000: new_numbers.append(int(num)) # 输出符合条件的数字 print('符合条件的数字:', new_numbers) index = 0 # 使用pop()函数提取并删除指定索引的元素 for num in new_numbers: list1 = num print(list1) wx.SendKeys(list1[0].replace('{br}', '{Shift}{Enter}'), waitTime=0) wx.SendKeys('{Enter}', waitTime=0) wx.TextControl(SubName=list1[0][:5]).RightClick() 看一下为什么后面的循环不行
时间: 2024-01-09 14:06:10 浏览: 31
根据你的代码,我发现在最后一个循环中,你使用了`list1[0]`来代表数字,但是`list1`是一个整数类型的变量,而不是一个列表类型,所以会报错。你可以将`list1`改成`new_numbers`,这样就可以正确地遍历数字列表并执行后续的操作了。你可以尝试修改代码如下:
```
index = 0
for num in new_numbers:
print(num)
wx.SendKeys(str(num).replace('{br}', '{Shift}{Enter}'), waitTime=0)
wx.SendKeys('{Enter}', waitTime=0)
wx.TextControl(SubName=str(num)[:5]).RightClick()
```
希望能够帮到你!
相关问题
from uiautomation import WindowControl,MenuControl import re wx=WindowControl( Name='微信', #searchDepth=1 ) print(wx) wx.SwitchToThisWindow() hw=wx.ListControl(Name='会话') print('寻找会话控制绑定',hw) # df = pd.read_csv('E:/pycharm/回复数据.csv', encoding='gb18030') while True: we=hw.TextControl(searchDepth=4) while not we.Exists(8): pass print('查找未读消息',we) if we.Name: we.Click(simulateMove=False) last_msg=wx.ListControl(Name='消息').GetChildren()[-1].Name #这两行代码是最重要的,绑定微信的聊天信息 print('读取最后一条消息',last_msg) new_numbers = [last_msg] for num in numbers: if num > 10000000000 and num < 20000000000: new_numbers.append(num) 这行代码有问题吗
这段代码有一些问题。首先,代码中的`numbers`变量没有定义,需要先定义一个包含数字的列表。其次,代码中的`new_numbers`列表只包含了最后一条消息,而没有包含所有符合条件的数字。最后,在`for`循环中需要使用`new_numbers`列表而不是`numbers`列表。
下面是修改后的代码示例:
```
from uiautomation import WindowControl, MenuControl
import re
wx = WindowControl(Name='微信')
print(wx)
wx.SwitchToThisWindow()
hw = wx.ListControl(Name='会话')
print('寻找会话控制绑定', hw)
# 定义包含数字的列表
numbers = [1234567890, 20000000000, 30000000000]
while True:
we = hw.TextControl(searchDepth=4)
while not we.Exists(8):
pass
print('查找未读消息', we)
if we.Name:
we.Click(simulateMove=False)
last_msg = wx.ListControl(Name='消息').GetChildren()[-1].Name
# 这两行代码是最重要的,绑定微信的聊天信息
print('读取最后一条消息', last_msg)
new_numbers = []
for num in numbers:
if num > 10000000000 and num < 20000000000:
new_numbers.append(num)
# 输出符合条件的数字
print('符合条件的数字:', new_numbers)
```
这样就可以输出符合条件的数字了。
uiautomation.WindowControl
`UIAutomation.WindowControl` 是一个 Windows UI 自动化库的类,它允许开发人员通过编程的方式与 Windows 应用程序进行交互和控制。这个类提供了一系列方法和属性,可以帮助开发人员访问和操作应用程序的 UI 元素,例如窗口、按钮、文本框等等。通过使用 `UIAutomation.WindowControl`,开发人员可以自动化执行一些任务,例如测试应用程序的 UI、生成 UI 自动化脚本等等。