4月292022
AU3自动化操作谷歌Chrome浏览器示例
--------AU3操作谷歌Chrome浏览器示例------------
示例为最 XPATH 为元素节点----
-----------------------------------------------------------
-----------------------------------------------------------
#PRE_UseX64=n
#RequireAdmin
#NoTrayIcon
#include <wd_core.au3>
#include <wd_helper.au3>
;~ $_WD_DEBUG = $_WD_DEBUG_None ;隐藏控制台 您还可以使用$ _WD_DEBUG_Error
;~ ConsoleWrite(@CRLF & $_WD_ERROR_Success)
;~ ConsoleWrite(@CRLF & $_WD_ERROR_Exception)
;~ ConsoleWrite(@CRLF & $_WD_ERROR_NoMatch)
;~ ConsoleWrite(@CRLF & $_WD_ERROR_InvalidExpression)
Dim $Png_i=0 ;用于储存截图计次
FileDelete(@ScriptDir & '\chrome.log')
WinKill("[title:" & @ScriptDir & "\chromedriver.exe") ;删除 之前可能因意外导致未关闭的cd
_WD_Option('Driver', 'chromedriver.exe')
_WD_Option('DriverParams', '--verbose --log-path="' & @ScriptDir & '\chrome.log"')
_WD_Option('Port', 9515) ;端口
Local $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "excludeSwitches": [ "enable-automation"], "useAutomationExtension": false }}}}'
_WD_Startup()
If @error <> $_WD_ERROR_Success Then
_CW("--WD启动失败")
Exit -1
EndIf
_CW("从web驱动程序请求新的会话")
Dim $sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_Window($sSession, "Maximize") ;最大化打开窗口
;--------------------------------------------------------------------------------
_CW("打开页面")
_WD_Navigate($sSession, "https://www.jianyiit.com")
Sleep(3000)
;--------------------------------------------------------------------------------
_ELE_SetV('//*[@id="s"]', 'ddddocr', "input搜索框")
Sleep(2000)
;--------------------------------------------------------------------------------
_ELE_click('//*[@id="searchform"]/button', "点击搜索")
Sleep(4000)
;--------------------------------------------------------------------------------
Local $_tile = _ELE_GetV('//*[@id="post-40"]/div[2]/h2/a', '读取标题')
MsgBox(0, '返回的标题', $_tile)
;--------------------------------------------------------------------------------
Local $rPng = _ELE_SavePNG('//*[@id="post-40"]/div[2]',"截ddddocr图片")
ShellExecute(@ScriptDir&"\"&$rPng)
Sleep(2000)
;--------------------------------------------------------------------------------
_ELE_click('//*[@id="zidingyi"]/a/img', "点击打赏图标")
Sleep(2000)
;--------------------------------------------------------------------------------
MsgBox(0,"提示","这里是不是该有赏的了。")
_CW("动作完成")
;--------------------------------------------------------------------------------
MsgBox(0, "处理完成!", "单击ok关闭浏览器和控制台")
;删除现有会话
_WD_DeleteSession($sSession)
;关闭web驱动程序控制台应用程序
_WD_Shutdown()
Exit
;------------------------------------------
;参数: xpath , 当前点标注
;点击元素
Func _ELE_click($sElementSelector, $step = "")
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sElementSelector, Default, True)
If UBound($sElement) > 0 Then
_WD_HighlightElement($sSession, $sElement[0])
Local $sResponse = _WD_ElementAction($sSession, $sElement[0], "Click")
_CW("元素点击" & @TAB & $step & @TAB & $sElementSelector)
Return 0
Else
_CW("元素点击失败" & @TAB & $step & @TAB & $sElementSelector)
Return -1
EndIf
EndFunc ;==>_ELE_click
;参数: xpath ,val ,当前点标注
;设置元素的值
Func _ELE_SetV($sElementSelector, $val, $step = "")
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sElementSelector, Default, True)
If UBound($sElement) > 0 Then
_WD_HighlightElement($sSession, $sElement[0])
_WD_SetElementValue($sSession, $sElement[0], $val)
_CW("元素设置" & @TAB & $step & @TAB & $sElementSelector)
Else
_CW("元素设置失败" & @TAB & $step & @TAB & $sElementSelector)
EndIf
EndFunc ;==>_ELE_SetV
;参数: xpath ,当前点标注
;获取 元素的值
Func _ELE_GetV($sElementSelector, $step = "")
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sElementSelector, Default, True)
If UBound($sElement) > 0 Then
_WD_HighlightElement($sSession, $sElement[0])
Local $abc = _WD_ElementAction($sSession, $sElement[0], "Text")
_CW("元素取内容" & @TAB & $step & @TAB & $sElementSelector)
Return $abc
Else
_CW("元素取内容失败" & @TAB & $step & @TAB & $sElementSelector)
Return ""
EndIf
EndFunc ;==>_ELE_GetV
;参数: xpath , 当前点标注
;保存当前元素的图片
Func _ELE_SavePNG($sElementSelector, $step = "")
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sElementSelector, Default, True)
If UBound($sElement) > 0 Then
Local $sResponse = _WD_ElementAction($sSession, $sElement[0], "Screenshot")
MsgBox(0,'$sResponse',$sResponse)
Local $hFileOpen = FileOpen("png\Screen"&$Png_i&".png", $FO_BINARY + $FO_OVERWRITE + $FO_CREATEPATH)
FileWrite($hFileOpen, _Base64Decode($sResponse))
FileClose($hFileOpen)
$Png_i +=1
_CW("元素截图" & @TAB & $step & @TAB & $sElementSelector)
Return "png\Screen"&$Png_i-1&".png" ;成功返回图片地址
Else
_CW("元素截图失败" & @TAB & $step & @TAB & $sElementSelector)
Return -1 ;失败返回 -1
EndIf
EndFunc ;==>_ELE_SavePNG
;日志
Func _CW($str = "---------------------------------")
_FileWriteLog(@ScriptDir & "\行为日志.log", $str)
ConsoleWrite($str)
EndFunc ;==>_CW <span><span> </span></span></wd_helper.au3></wd_core.au3>
》》》》 谷歌操作相关文件.rar
扫描二维码,在手机上阅读
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!