Alv*_*oAV 8 python automation process ui-automation python-2.7
我试图弄清楚如何用Python 管理进程,尽管C++可能会更好.我使用的是Python 2.7,Ubuntu 14.04是我的操作系统.
恢复我想要实现的目标:
我的目的是创建一个脚本来管理其他软件,类似于Selenium对浏览器的操作,但与任何程序相似.也许使用Python使用Python执行流程可以让我选择管理流程UI
发送操作/与正在运行的进程交互
现在我正在Linux中使用这个脚本psutil.我知道有一些Windows库喜欢pywin或pywindll.
我想管理一个流程,例如任何一种带UI的软件(Skype,Gedit,Firefox ..),我想知道是否可以发送动作来点击按钮.
我不想在计算机上管理鼠标,因为我们说这个窗口在其他窗口/东西下面是"隐藏"的:
我正在使用psutil这个过程,我有很多选择,如:
但这些动作似乎都不是我正在寻找的,那就是与流程UI进行交互......
甚至可能是我想要实现的目标?
发送击键和鼠标点击是最简单的解决方案吗?
读取内存地址值
我一直scanmem在Linux中使用找到某个变量的内存地址,一旦找到了我正在寻找的内存地址,我想在Python中使用该地址来获取存储在该地址中的值.
我发现最接近的是使用ctypes,例如:
from ctypes import string_at
from sys import getsizeof
mem_address = 0x7c3f
value = string_at(id(mem_address), getsizeof(mem_address))
Run Code Online (Sandbox Code Playgroud)
我在想一个程序何时执行必须将程序的UI发送到操作系统,可以用python "捕获"接口,并重定向到操作系统?
通过 Python 执行软件之类的东西也可以直接管理UI
我想我有一个解决方案可以从不同进程中的程序中读取变量。好吧,稍微简化一下,例如您正在尝试在用 C++ 编写的 2 个不同程序之间建立通信,比如说程序 A 和程序 B。关于您的查询,我们会遇到程序“A”尝试的情况访问程序“B”中变量的值。
在这种情况下,我认为您可以使用增强进程间通信。Boost Interprocess 是 Boost 中的一个库,它允许您使用共享内存在两个进程之间进行通信。您可以使用库中的消息队列。有关更多信息,请查看此处:
http://www.boost.org/doc/libs/1_56_0/doc/html/interprocess.html
因此,回到示例,您需要编写一些代码来支持在进程之间读取和写入变量。假设现在我们只能读取和写入数组和标量值。因此,您需要维护一个数据结构(最好是映射),将变量名称映射到内存中的位置。像这样的东西:
#include <map>
//Somewhere in your program you have a variable
int my_var = 5;
//Declare a map of string mapping to 64 bit pointers
std::map<std::string, long long> var_map;
//At any point you decide to register the reference of this value
var_map["my_var"] = (long long) &my_var;
//Now that you have registered this value,
//you can access it according to the name and
//type cast it as well to a data structure that you like
int *ptr = (int *) var_map["my_var"];
//Now you can play around with this:
*ptr = 1024;
Run Code Online (Sandbox Code Playgroud)
所以我希望你明白我在这里想要做什么,所以这种类型的代码将存在于我们的例子中的程序B中。这样做的原因是程序A可以向程序B发送命令,说我想读取一个变量称为“my_var”。
那么现在就到了实际沟通的部分了。在 Boost Interprocess 中,您可以打包可以读取命令的结构,如下所示:
typedef ReadCommand {
char *var_name;
int read_bytes;
}
Run Code Online (Sandbox Code Playgroud)
请阅读 Boost 中的文档以了解如何设置共享内存实例,因为一旦设置完毕,您就可以发送如下命令:
//Code in program A
ReadCommand read_command;
read_command.var_name = "my_var";
read_command.bytes = 4;
try {
//Need to declare message_queue, please see doc in Boost
message_queue_A->send(&read_command, sizeof(ReadCommand), 0);
} catch (boost::interprocess::interprocess_exception &ex){
//Handle exception
}
Run Code Online (Sandbox Code Playgroud)
再次回到程序 B,您可以使用如下代码来接收消息:
//Best to have struct definition in shared header file
ReadCommand read_command_B;
int some_priority;
boost::interprocess::message_queue::size_type size_of_data_recvd;
message_queue_B->receive(&read_command_B, sizeof(ReadCommand), size_of_data_recvd, some_priority);
//use information in read_command_B
//to access var_map then use another
//message queue to send back data to
//Program A which will be expecting
//some information from program B.
Run Code Online (Sandbox Code Playgroud)
无论如何,一旦完成,您就可以将 python 与 Boost python 库集成。希望这是有道理的。尝试一下,如果有任何问题请告诉我。这不是精确的实现,而是一个可以为您的读取值问题提供解决方案的想法。
至于发送操作问题,我不太确定如何与另一个进程中的 UI 交互。通常,供应商向程序员公开用于与 UI 元素交互的 API。下面发生的事情很难理解和操作,因为在大多数情况下它们是闭源的。如果您可以访问 API 中的源代码,那么情况就会有所不同。然后,您可以使用与上面提到的类似的概念来写入另一个程序中的某个位置,从而触发 UI 中发生的事件。