求助!! FlaUI 有没有大佬熟悉这个的?

By niheibie at 2024-01-27 • 0人收藏 • 781人看过


import dotNet;
import mouse;
import console;
import winex.mouse;
var Core = dotNet.load("FlaUI.Core", "C:\Users\Administrator\Desktop\FlaUInspect_1.3.0\Libs\FlaUI.Core.dll");
var UIA2 = dotNet.load("FlaUI.UIA2", "C:\Users\Administrator\Desktop\FlaUInspect_1.3.0\Libs\FlaUI.UIA2.dll");
var UIA3 = dotNet.load("FlaUI.UIA3", "C:\Users\Administrator\Desktop\FlaUInspect_1.3.0\Libs\FlaUI.UIA3.dll");
var Interop = dotNet.load("Interop.UIAutomationClient", "C:\Users\Administrator\Desktop\FlaUInspect_1.3.0\Libs\Interop.UIAutomationClient.dll");
Application = Core.import("FlaUI.Core.Application");
AutomationType = UIA3.new("FlaUI.UIA3.UIA3Automation");
TimeSpan = dotNet.import("System.TimeSpan", "mscorlib.dll")

var app = Application.Attach("WeChat.exe");

var window = app.GetMainWindow(AutomationType, TimeSpan.FromSeconds(2.0));
if (window) {
    console.log(window.title)
    var ele = window.FindFirstByXPath("Pane[2]/Pane/ToolBar/Button[3]")
    if (ele) {
        console.log( ele.Click()) //报错这里

    }
}


是这样的, 通过xpath找到控件了。 但是点击不了。


ele.Click() 点击不了.报错!


QQ截图20240127134702.png


有大佬封装好的库吗? 

7 个回复 | 最后更新于 2024-01-28
2024-01-27   #1

https://www.chengxu.xyz/t/21426

2024-01-27   #2

知道这个没用过

2024-01-28   #3
import dotNet;
import mouse;
import key;
import console;

namespace myplu.flaui;

var assembly = ..dotNet.load("FlaUI",$"~\lib\myplu\flaui\.res\FlaUI.dll");
TimeSpan = ..dotNet.import("System.TimeSpan","mscorlib");

assembly.import("FlaUI.Core");
assembly.import("FlaUI.UIA3");

application = ..FlaUI.Core.Application;
auto = ..FlaUI.UIA3.UIA3Automation();
input = null;
automationElementExtensions = null;
dbg = null;
root = auto.GetDesktop();

registerFocusChanged = function(callback){
	return auto.RegisterFocusChangedEvent(callback);
}
unRegisterFocusChanged = function(eventHandler){
	auto.UnregisterFocusChangedEvent(eventHandler);
}
pointElement = function(){
	if(!input){
		assembly.import("FlaUI.Core.Input");
		input = ..FlaUI.Core.Input.Mouse;
	}
	return auto.FromPoint(input.Position);
}
elementHighlight = function(element){
	if(!automationElementExtensions){
		assembly.import("FlaUI.Core.AutomationElements");
		automationElementExtensions = ..FlaUI.Core.AutomationElements.AutomationElementExtensions;
	}
	automationElementExtensions.DrawHighlight(element);
}
getXPathToElement = function(element, rootElement){
	if(!dbg){
		dbg = ..FlaUI.Core.Debug;	
	}
	return dbg.GetXPathToElement(element,rootElement); 
}

attach = function(exeName){
	var app = application.Attach(exeName);
	return app.GetMainWindow(auto,TimeSpan.FromSeconds(2.0));
}
findFirstByXPath = function(expression, element){
	if(expression){
		if(element){
			return element.FindFirstByXPath(expression);
		}else {
			return root.FindFirstByXPath(expression); 
		}		
	}
}
findAllByXPath = function(expression, element){
	if(expression){
		if(element){
			return element.FindAllByXPath(expression);
		}else {
			return root.FindAllByXPath(expression); 
		}		
	}
}

clickElement = function(expression, element, clickType, moveMouse, xScale=0.5, yScale=0.5){
	var ele = findFirstByXPath(expression, element);
	if(ele){
		//ele.Focus();
		ele.SetForeground();
		select(clickType) {
			case "L" {
				click(ele,moveMouse, xScale, yScale);
			}
			case "LD" {
				doubleClick(ele,moveMouse, xScale, yScale);
			}
			case "R" {
				rightClick(ele,moveMouse, xScale, yScale);
			}
			case "RD" {
				rightDoubleClick(ele,moveMouse, xScale, yScale);
			}
			else {
				click(ele,moveMouse, xScale, yScale);
			}
		}		
	}
	return ele; 
}

sendElementString = function(expression, element, str, clickType, moveMouse, xScale=0.5, yScale=0.5){
	var ele = clickElement(expression, element, clickType, moveMouse, xScale, yScale);
	if(ele){
		..key.sendString(str);
	}
	return ele; 
}


var getControlPoint = function(automationElement, xScale=0.5, yScale=0.5){
	var rect = automationElement.BoundingRectangle; 
	return rect.x + rect.Width*xScale , rect.y + rect.Height*yScale
}


click = function(automationElement,moveMouse, xScale=0.5, yScale=0.5){
	var x,y = getControlPoint(automationElement, xScale, yScale); 
	if(moveMouse){
		..mouse.moveTo(x,y,true);
	}
	..mouse.click(x,y,true);
}

doubleClick = function(automationElement,moveMouse, xScale=0.5, yScale=0.5){
	var x,y = getControlPoint(automationElement, xScale, yScale); 
	if(moveMouse){
		..mouse.moveTo(x,y,true);
	}
	..mouse.clickDb(x,y,true);
}

rightClick = function(automationElement,moveMouse, xScale=0.5, yScale=0.5){
	var x,y = getControlPoint(automationElement, xScale, yScale); 
	if(moveMouse){
		..mouse.moveTo(x,y,true);
	}
	..mouse.rb.click(x,y,true);
}

rightDoubleClick = function(automationElement,moveMouse, xScale=0.5, yScale=0.5){
	var x,y = getControlPoint(automationElement, xScale, yScale); 
	if(moveMouse){
		..mouse.moveTo(x,y,true);
	}
	..mouse.rb.click(x,y,true);
}

destory = function(){
	application = null;
	auto = null;
	root = null;
	collectgarbage("collect");
}

each = ..dotNet.each


/*****intellisense()
myplu.flaui = 窗口自动化工具,基于flaui
myplu.flaui.application = FlaUI.Core.Application构建对象
myplu.flaui.auto = FlaUI.UIA3构建对象\n!flaUI3Obj.
myplu.flaui.root = 桌面元素,根元素\n!automationElement.
myplu.flaui.attach("__") = 指定进程名或者路径,获取主窗口元素\n!automationElement.
myplu.flaui.findFirstByXPath(.(xpath表达式,查找元素) = 以xpath方式在指定元素查找,返回第一个匹配到的元素\n@2可选\n!automationElement.
myplu.flaui.findAllByXPath(.(xpath表达式,查找元素) = 以xpath方式在指定元素查找,返回所有匹配到的元素\n@2可选\n!automationElementList.
myplu.flaui.clickElement(.(xpath表达式,查找元素,点击类型【L/LD/R/RD】,是否移动,x坐标偏移比例,y坐标偏移比例) = 以xpath方式在指定元素查找,激活并点击元素,返回所有匹配到的元素\n@1必填\n!automationElementList.
myplu.flaui.sendElementString(.(xpath表达式,查找元素,发送文字,点击类型【L/LD/R/RD】,是否移动,x坐标偏移比例,y坐标偏移比例) = 以xpath方式在指定元素查找,激活并点击元素,然后发送文本。返回所有匹配到的元素\n@1、@2必填\n!automationElementList.
myplu.flaui.click(.(automationElement,是否移动,x坐标偏移比例,y坐标偏移比例) = 控件点击,前提确认控件是否支持点击。默认不显示鼠标移动轨迹
myplu.flaui.doubleClick(.(automationElement,是否移动,x坐标偏移比例,y坐标偏移比例) = 控件双击,前提确认控件是否支持点击。默认不显示鼠标移动轨迹
myplu.flaui.rightClick(.(automationElement,是否移动,x坐标偏移比例,y坐标偏移比例) = 右键单机,默认不显示鼠标移动轨迹
myplu.flaui.rightDoubleClick(.(automationElement,是否移动,x坐标偏移比例,y坐标偏移比例) = 右键双击,默认不显示鼠标移动轨迹
myplu.flaui.each(automationElementList) = @for i,v in ??.each(__/*输入需要遍历元素数组,\n返回值 i 为当前索引,v 为当前值,\n注意并非所有 .NET 类型都支持此接口*/) {
	
} 
myplu.flaui.registerFocusChanged() = @var eventHandler = myplu.flaui.registerFocusChanged(function(element){
	/*注册监听,焦点变更的元素*/
});
myplu.flaui.unRegisterFocusChanged(eventHandler) = 解除焦点变更监听
myplu.flaui.pointElement() = 获取鼠标指向元素,不一定获取到,通过焦点变更获取
myplu.flaui.elementHighlight(__) = 元素高亮
myplu.flaui.getXPathToElement(.(目标元素,起始元素) = 获取从起始元素到目标元素的正则表达式
myplu.flaui.destory() = 释放资源
end intellisense*****/

/*****intellisense(!flaUI3Obj)
GetDesktop() = 获取桌面元素,根元素\n!automationElement.
Compare(.(automationElement1,automationElement2) = 判断是否同一个AutomationElement元素
FocusedElement() = 获取当前焦点元素\n!automationElement.
FromHandle(hwnd) = 获取指定控件句柄元素\n!automationElement.
FromPoint(point) = 获取鼠标指向控件元素\n!automationElement.
end intellisense*****/

/*****intellisense(!automationElement)
ActualHeight = 显示区域高度
ActualWidth = 显示区域宽度
AutomationId = 控件ID
BoundingRectangle = 控件显示区域
CachedChildren = 缓存子元素\n!automationElementList.
CachedParent = 缓存父元素\n!automationElement.
ClassName = 类名
ControlType = 控件类型
HelpText = 帮助文档
IsEnabled = 是否激活
Name = 控件标题
Parent = 父元素\n!automationElement.
Capture() = 截图,返回System.Drawing.Bitmap
CaptureToFile(.(filePath) = 截图保存为文件
Click(.(moveMouse) = 控件点击,前提确认控件是否支持点击。默认不显示鼠标移动轨迹
DoubleClick(.(moveMouse) = 控件双击,前提确认控件是否支持点击。默认不显示鼠标移动轨迹
FindAll(.(treeScope, condition) = 查找元素,指定搜索方式,表达式查找。\n!automationElementList.
FindAllByXPath(.(xPath) = xpath查找元素\n!automationElementList.
FindAllChildren() = 查找所有子元素\n!automationElementList.
FindAllDescendants() = 查找所有后辈元素\n!automationElementList.
FindAllWithOptions(.(treeScope, condition, traversalOptions, root) = 条件查找\n!automationElementList.
FindAt(.(treeScope, index, condition) = 下标查找,指定搜索方式,表达式查找。\n!automationElement.
FindChildAt(.(index, condition) = 下标查找查找子元素\n!automationElement.
FindFirst(.(treeScope, condition) = 查找元素,返回第一个匹配到的元素。\n!automationElement.
FindFirstByXPath(.(xPath) = xpath查找元素,返回第一个匹配到的元素。\n!automationElement.
FindFirstChild() = 查找第一个子元素\n!automationElement.
FindFirstChild(.(automationId) = 查找第一个子元素,通过控件ID。\n!automationElement.
FindFirstChild(.(condition) = 查找第一个子元素,表达式查找。\n!automationElement.
FindFirstDescendant() = 查找第一个后辈元素\n!automationElement.
FindFirstDescendant(.(automationId) = 查找第一个后辈元素,通过控件ID。\n!automationElement.
FindFirstDescendant(.(condition) = 查找第一个后辈元素,表达式查找。\n!automationElement.
FindFirstWithOptions(.(treeScope, condition, traversalOptions, root) = 条件查找\n!automationElement.
Focus() = 设置焦点
GetClickablePoint() = 获取鼠标位置
GetCurrentMetadataValue(.(targetId, metadataId) = 获取Metadata值
RightClick(.(moveMouse) = 右键单机,默认不显示鼠标移动轨迹
RightDoubleClick(.(moveMouse) = 右键双击,默认不显示鼠标移动轨迹
SetForeground() = 激活
end intellisense*****/

/*****intellisense(!automationElementList)
each() = @for i,v in myplu.flaui.each(__/*输入需要遍历的 .NET 对象或普通数组,\n返回值 i 为当前索引,v 为当前值,\n注意并非所有 .NET 类型都支持此接口*/) {
	
} 
end intellisense*****/


2024-01-28   #4

回复#3 @瞌睡蟲子 :

66666666666666666666

2024-01-28   #5

回复#3 @瞌睡蟲子 :

dll可以发一下吗? 好像和你的不同

2024-01-28   #6

$S(FIM86X8O67NY5SN56EN3.png

2024-01-28   #7

回复#6 @niheibie :

我用工具里面的dll合并工具合并了


登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...