AutoItでGoogleChromeを動かすサンプルと前回のログイン状態を使ったサンプル

2024年5月8日

install

AutoIt,AutoIt Script Editor

https://www.autoitscript.com/site/autoit/downloads/

AutoIt Script Editor.(Customised version of SciTE with lots of additional coding tools for AutoIt)

AutoIt– Self Extracting Archive (for those who don’t like/want an installer)(includes x86 and x64 components and Aut2Exe and AutoItX)

Au3Record.exe,ASWhook.dll

https://www.autoitscript.com/autoit3/files/archive/autoit/autoit-v3.3.14.0.zip

この中から抽出

WebDriver

chrome://settings/help

Chromeのバージョンを確認し同じバージョン

AutoIthttps://sites.google.com/chromium.org/driver/

AutoItフォルダへファイル入れます

UDF(User Defined Functions)

AutoItのIncludeフォルダへau3ファイル入れます

GitHub – Danp2/au3WebDriver: Web Driver UDF for AutoIt

Releases · dragana-r/autoit-winhttp (github.com)

A Non-Strict JSON UDF (JSMN) – AutoIt Example Scripts – AutoIt Forums (autoitscript.com)

サンプル

yahooにキーワードを入力し検索した件数を出すサンプルです。

前回のログイン状態を使うためにはコメントを外してください。
設定しないとプロフィールパスはテンポラリーホルダーに作成されます
;_WD_CapabilitiesAdd('args’,’–user-data-dir=’ & $CHROME_profile)

chrome://version/

デフォルトプロフィール パス
C:\Users\username\AppData\Local\Google\Chrome\User Data\Default
#include <wd_core.au3>
#include <wd_capabilities.au3>
#include <wd_helper.au3>
Const $CHROME_profile = 'C:\Users\username\AppData\Local\Google\Chrome\User DatAu3'
Const $CHROME_driver = 'C:\app\AutoIt3\chromedriver.exe'
_WD_Option('Driver', $CHROME_driver)
_WD_Option('DriverParams', ' --verbose --log-path=' & @ScriptDir & '\chrome.log')
_WD_Option('Port', 9515)
_WD_CapabilitiesStartup()
_WD_CapabilitiesAdd('alwaysMatch', 'chrome')
_WD_CapabilitiesAdd('w3c', True)
_WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
;_WD_CapabilitiesAdd('args','--user-data-dir=' & $CHROME_profile)
;_WD_CapabilitiesAdd('args', '--headless')
Local $sCapabilities = _WD_CapabilitiesGet()
Local $pid = _WD_Startup()
If @error Then
  Msgbox(4096,"error", "Failed to start webdriver",10)
  Exit
EndIf
$session = _WD_CreateSession($sCapabilities)
If @error Then
  MsgBox(4096, "error", "browser did not start",10)
  If $pid Then _WD_Shutdown()
  Exit
EndIf
Sleep(1000)
_WD_Navigate($session, "https://www.yahoo.co.jp/")
If @error Then
  MsgBox(4096, "error", "Could not open the specified URL. extended=" & @extended ,10)
EndIf
Sleep(3000)
$value="A"
$xpath = "/html/body/div[1]/div/header/section[1]/div/form/fieldset/span/input"
$element = _WD_WaitElement($session, $_WD_LOCATOR_ByXPath, $xpath)
_WD_SetElementValue($session, $element, $value)
Sleep(3000)
$xpath = "/html/body/div[1]/div/header/section[1]/div/form/fieldset/span/button/span/span"
$element = _WD_WaitElement($session, $_WD_LOCATOR_ByXPath, $xpath)
_WD_ElementAction($session, $element, 'click')
Sleep(3000)
$element = _WD_WaitElement($session, $_WD_LOCATOR_ByXPath, '/html/body/div[1]/header/div[2]/div/div')
If @error Then
	Msgbox(4096,"error", "element get failed",10)
EndIf
$txt = _WD_ElementAction($session, $element, "text")
MsgBox(4096, "text", $txt)
Sleep(3000)
_WD_Window($session, "close")
If $session Then _WD_DeleteSession($session)
If $pid Then _WD_Shutdown()



AutoIt

Posted by eightban