利用AutoIt自动登录

1.关于AutoIt

下载
https://www.autoitscript.com/site/autoit/

相关资料
Language Reference
https://www.autoitscript.com/autoit3/docs/

中文资料
https://www.jb51.net/shouce/autoit/

调试器
http://www.thefoolonthehill.net/drupal/AutoIt%20Debugger

2.关于Chrome UDF

Chrome UDF是AutoIt对chrome的扩展支持.
安装说明见:https://www.autoitscript.com/forum/topic/154439-chrome-udf/

运行上面连接中EXAMPLE #1的chrome_example.au3.
操作表单正常.


如果没有效果,可能的原因:
autoit-chrome-native-messaging-host.exe,的manifest.json中的allowed_origins的id与chrome插件不同,修改为插件的值。
出处见:
https://www.autoitscript.com/forum/topic/154439-chrome-udf/?page=2

If autoit-chrome-native-messaging-host.exe isn't running when you start Chrome then get the value of the registry key above, and in Windows Explorer go to this location (i.e. C:UsersJOHNAppDataRoamingAutoIt3Chrome Native Messaging Host).  Check the file  autoit-chrome-native-messaging-host.exe and manifest.json exist.  Open manifest.json in Notepad.  Make sure "path" is set to the full path of autoit-chrome-native-messaging-host.exe, and the path includes double-backslashes.  Also make sure the value for "allowed_origins"opmlbgppkkdeleedejakphgmgiigjjga/) matches the value in the ID field of the Chrome Extension.  Check this by opening Chrome and go to the Tools -> Extensions menu item.  In the Extensions page make sure Developer mode is ticked, and then locate the ID field for the AutoIT for Chrome extension.  This value should match the value for "allowed_origins" in the manifest.json file.

测试www.baidu.com正常.

#Include <Array.au3>
#Include <Chrome.au3>

_ChromeStartup("https://www.baidu.com")
Sleep(5000);
_chromeObjSetValueByName("wd","autoit3");
_ChromeInputClickByType1("submit")

Chrome UDF的2个问题:
1.中文支持
--_chromeObjSetValueByName设置中文内容无效果
--_ChromeEval("document.getElementById('TPL_username_1').value")中文返回乱码
2.与winapi.au3冲突(环境windows 8中文版)
#include <winapi.au3>
#Include <Chrome.au3>
提示错误:
"C:\Program Files (x86)\AutoIt3\Include\Chrome.au3" (38) : ==> Can not redeclare a constant.:
Const $CSIDL_LOCAL_APPDATA = 28


如果不考虑中文问题,下面的代码可以正常运行:

#Include <Array.au3>
#Include <Chrome.au3>

Func _ChromeObjSetValueById($objname, $value, $index = 0, $timeout = 5)
    $response = _ChromeEval("document.getElementById('" & $objname & "').value = '" & $value & "';", $timeout)
    SetError(@error)
    return $response
EndFunc
Func _ChromeInputClickById($objname, $index = 0, $timeout = 5)
    $response = _ChromeEval("document.getElementsById('" & $objname & "').click();", $timeout)
    SetError(@error)
    return $response
EndFunc


func login($user,$pswd,$tag)
_ChromeStartup("https://login.taobao.com/member/login.jhtml")
_ChromeDocWaitForExistenceByTitle("淘宝网 - 淘!我喜欢", 10)

_ChromeObjSetValueById("TPL_username_1",$user)
_ChromeObjSetValueById("TPL_password_1",$pswd);
_ChromeInputClickByType1("submit")
_ChromeEval("document.getElementById('J_SubmitStatic').click()",5)

EndFunc

login("user","password","cainiao")

3.自动登录代码

。未使用Chrome UDF.

。用EditThisCookie插件导出json格式的cookies文件.

#Include <Array.au3>
#include <WinAPIFiles.au3>

func login($user,$pswd,$tag)
local $ini_file_name = "jjp.ini"
local $chrome_path = IniRead ( $ini_file_name, "general", "chrome","")
local $home_path = IniRead($ini_file_name,"general","home","")

Local $val = Run($chrome_path & "--start-maximized")
WinWaitActive("[CLASS:Chrome_WidgetWin_1]")
Sleep(2000)
;打开新标签页
Send("^t");
Sleep(500)
;输入页面地址
Send("https://login.taobao.com/member/login.jhtml");
;打开页面
Send("{enter}");
Sleep(2000)

;定位到用户名输入框
Send("{TAB}")
Sleep(2000);
Send($user)
Sleep(2000);
Send("{ESC}")
Sleep(2000);

;定位到密码输入框
Send("{TAB}")
Sleep(2000);
Send($pswd)
Sleep(5000);

;拖动滑块
MouseClickDrag("left",913,509,1185,509);
Sleep(1000)

;点击登录
MouseClick("left",1054,576,1);
Sleep(2000)

;点击EditThisCookie按钮
MouseClick("left",1193,56,1);
Sleep(2000);
;导出cookies
MouseClick("left",1000,88,1);
Sleep(2000)

;保存cookies文件
$cookies = ClipGet()
$fn = $home_path&$tag&".cookies\1101.json";
FileDelete($fn);
FileWrite($fn,$cookies)
EndFunc

login("user","password","cainiao")

配置jjp.ini

[general]
chrome=C:\Users\Think\AppData\Local\Google\Chrome\Application\chrome.exe
home=E:\home\jjp\

猜你喜欢

转载自blog.csdn.net/wherwh/article/details/83960546