天天看點

利用AutoIt自動登入1.關于AutoIt2.關于Chrome UDF3.自動登入代碼

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\
           

繼續閱讀