from uiautomation import WindowControl,MenuControl import pandas as pd import numpy as np 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) msg=df.apply(lambda x:x['回复内容']if x['关键词']in last_msg else None,axis=1) msg.dropna(axis=0,how='any',inplace=True) ar=np.array(msg).tolist() if ar: wx.SendKeys(ar[0].replace('{br}','{Shift}{Enter}'),waitTime=0) wx.SendKeys('{Enter}',waitTime=0) wx.TextControl(SubName=ar[0][:5]).RightClick() else: wx.SendKeys('我没明白你的意思',waitTime=0) wx.SendKeys('{Enter}',waitTime=0) wx.TextControl(SubName=last_msg[:5]).RightClick() 这行代码出错了 Traceback (most recent call last): File "E:\pycharm\002.py", line 43, in <module> wx.TextControl(SubName=last_msg[:5]).RightClick() File "E:\Python\lib\site-packages\uiautomation\uiautomation.py", line 6580, in RightClick point = self.MoveCursorToInnerPos(x, y, ratioX, ratioY, simulateMove) File "E:\Python\lib\site-packages\uiautomation\uiautomation.py", line 6507, in MoveCursorToInnerPos rect = self.BoundingRectangle File "E:\Python\lib\site-packages\uiautomation\uiautomation.py", line 5907, in BoundingRectangle rect = self.Element.CurrentBoundingRectangle File "E:\Python\lib\site-packages\uiautomation\uiautomation.py", line 6224, in Element self.Refind(maxSearchSeconds=TIME_OUT_SECOND, searchIntervalSeconds=self.searchInterval) File "E:\Python\lib\site-packages\uiautomation\uiautomation.py", line 6475, in Refind raise LookupError('Find Control Timeout({}s): {}'.format(maxSearchSeconds, self.GetSearchPropertiesStr())) LookupError: Find Control Timeout(10s): {SubName: '你你你', ControlType: TextControl} 进程已结束,退出代码1 是什么问题,怎么解决
时间: 2024-01-29 16:03:31 浏览: 171
这个错误提示表明在使用uiautomation库的时候,查找窗口控件超时了。可能是由于该控件没有及时加载或者在当前窗口中不存在。
你可以尝试增加等待时间,或者使用其他查找控件的方法。例如,可以使用FindAll方法查找所有符合条件的控件,并从中选择需要的控件。另外,也可以使用TryGetClickablePoint方法来判断控件是否可点击。
以下是使用FindAll方法查找控件的示例代码:
```python
import uiautomation as auto
# 查找所有SubName为'你你你'的TextControl控件
controls = auto.FindAll('ControlType.TextControl', SubName='你你你')
# 从中选择第一个控件并右键点击
if controls:
control = controls[0]
control.RightClick()
else:
print('未找到控件')
```
如果还是无法解决问题,可能需要检查窗口是否已打开或者控件是否已加载完成。
相关问题
from uiautomation import WindowControl,MenuControl import pandas as pd import numpy as np 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) msg=df.apply(lambda x:x['回复内容']if x['关键词']in last_msg else None,axis=1) msg.dropna(axis=0,how='any',inplace=True) ar=np.array(msg).tolist() if ar: wx.SendKeys(ar[0].replace('{br}','{Shift}{Enter}'),waitTime=0) wx.SendKeys('{Enter}',waitTime=0) wx.TextControl(SubName=ar[0][:5]).RightClick() else: wx.SendKeys('我没明白你的意思',waitTime=0) wx.SendKeys('{Enter}',waitTime=0) wx.TextControl(SubName=last_msg[:5]).RightClick() 这行代码怎么优化
这行代码 `wx.TextControl(SubName=ar[0][:5]).RightClick()` 可以进行以下优化:
1. 对ar数组进行判空,防止出现IndexError错误:
```python
if ar:
sub_name = ar[0][:5]
wx.TextControl(SubName=sub_name).RightClick()
```
2. 将右键点击操作封装成一个函数,方便多次调用:
```python
def right_click(control):
control.RightClick()
# 调用right_click函数
if ar:
sub_name = ar[0][:5]
control = wx.TextControl(SubName=sub_name)
right_click(control)
else:
control = wx.TextControl(SubName=last_msg[:5])
right_click(control)
```
这样代码可读性更好,也更易于维护。
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)
```
这样就可以输出符合条件的数字了。
阅读全文