eightban's memo

残しておきたい記事をまとめてみました。このブログに書いてあるドキュメントやブログで配布しているファイルの使用によって発生するいかなる損害に対してもこのブログの管理者は責任を負いません。使用する場合は自己責任のもとに使用してください。

AutoHotkey

AutoItを使ってプログラムを組まないで自動化する

更新日:

関数を記述してる CSV ファイルを読み込んで自動化する方法です

XPathが変更になった時に対応が楽になります

準備

ソースコード

; ---------------------------------------------------
; 2024-05-13

; ---------------------------------------------------
; "Loop" start,end,loop
; "N"  [] -> $n
; "FileReadLine" $n

; ---------------------------------------------------
; "IfNum" true/false.start,end -> $active
; "IfStr" true/false.Str -> $active
; "Active" true/false -> $active
; ---------------------------------------------------

; "Add" $num+$num2 or $num+[] -> $num
; "Addn" $num+[] -> $num
; "Array" $num ->$str
; "ClipGet" -> $str
; "ClipPut" $text or []

; "ConsoleWrite" $n $text
; "ControlClick" "title", "text", controlID
; "ControlSend" "title", "text", controlID, $text []
; "Div" $num/$num2 or $num/[] -> $num
; "Divx" -> $num
; "FileWriteLine" $num
; "Mod" mod $num/$num2 or $num/[] -> $num
; "Modx" -> $num
; "MsgBox" $n $text
; "Mul" $num*$num2 or $num*[] -> $num
; "Num" $n or [] -> $num
; "Num2" $num or [] -> $num2
; "Run" $str ,$text
; "Sleep" ms
; "Str"  -> $str
; "Str2" $str or [] -> $str2
; "StrStr2" $str2 or [] -> $str
; "StrAdd" $str&$str2 or $str&[] -> $str
; "StrText" $text or [] -> $str
; "StringFormatn" $num -> $text
; "StringFormatns" $num $str -> $text
; "StringFormats" $str -> $text
; "StringFormatsn" $str $num -> $text
; "Sub" $num-$num2 or $num-[] -> $num
; "URL" -> $url
; "URLAdd" $url&$num or $url&[] -> $url
; "URLAdds" $url&$str or $url&[] -> $url
; "WinWaitActive" "title"
; "XPath"
; "XPathAdd" $xpath&$num or $xpath&[]
; "XPathAdds" $xpath&$str or $xpath&[]
; "_WD_DownloadFile" $url [],$text []
; "_WD_ElementAction"  $xpath [],$text [] [click] [text] [clear] -> $str
; "_WD_Navigate" url
; "_WD_SetElementValue" $xpath [],$text []

#include <wd_core.au3>
#include <wd_capabilities.au3>
#include <wd_helper.au3>
#include <Array.au3>

if $CmdLine[0] = 0 Then
 Global  $idir = ""

Else
 Global  $idir = "\" & $CmdLine[1]

EndIf
Global $fn =  @ScriptDir & $idir & "\xsend_linems"  & ".csv"

;
Global $fn =  @ScriptDir & $idir & "\xsendkey"  & ".csv"

Global $mm
Global $mn
Global $nCount = 100
Global $n = 0
Global $num = 0
Global $active = true
Global $start1 = 1
Global $end1 = 1
Global $loop1 = 1
Global $text = ""
Global $str = ""
Global $str2 = ""
Global $xpath = ""
Global $url = ""

Dim $linex[$nCount]

;????????WebDriver???????
Const $CHROME_DRIVER = 'C:\app\AutoIt3\chromedriver.exe'
Const $CHROME_profile = 'C:\Users\User\AppData\Local\Google\Chrome\User DatAu3'
Global $web_driver_pid
Global $session

Global $web_driver_pid = StartupWebDriver()
;If $web_driver_pid = 0 Then MsgBox(4096, "ERROR", "????")
If @error Then
  Msgbox(4096,"error", "Failed to start webdriver",10)
  Exit(1)
EndIf
Local $capabilities = SetupBrowser()
Sleep(2000)

$session = _WD_CreateSession($capabilities)
If @error Then
  MsgBox(4096, "error", "browser did not start",10)
  If $web_driver_pid Then _WD_Shutdown()
  Exit
EndIf
_csvread()
$end1 = $mn

For $i=0 To $mm -1 Step 1

	if $linex[$i]<> "" Then
		$array2x = StringSplit($linex[$i], ",")

		if 0 < UBound($array2x) Then
			if $array2x[1]<> "" Then
				Switch $array2x[1]
					Case "Loop"
						$start1 = $array2x[2]
						$end1 = $array2x[3]
						$loop1 = $array2x[4]
					Case "FileReadLine"
						_nread($array2x[2])

					Case "N"
						$n=$array2x[2]
					Case ""
						Exit
					Case Else
				EndSwitch

			EndIf
		endif
	EndIf
Next
_csvread()

Sleep(1000)
If 1 <  $start1 Then
For $i=0 To $start1-2 Step 1
 _sendkyx($i)
Next
EndIf
For $j=0 To $loop1-1 Step 1
 For $i=$start1-1 To $end1 -1 Step 1
  _sendkyx($i)
 Next
Next
If $mm >  $end1 Then
For $i=$end1 To $mm -1 Step 1
 _sendkyx($i)
Next
EndIf
Sleep(3000)

_WD_Window($session, "close")
If $session Then _WD_DeleteSession($session)
If $web_driver_pid Then _WD_Shutdown()


Func _sendkyx($mx)
;MsgBox(4096,"err", $mx,5)
						ConsoleWrite($mx & " : " & $num & " : " & $text & " : " & $linex[$mx] &@CRLF)

	if $linex[$mx]<> "" Then
		Sleep(1000)
		$array2x = StringSplit($linex[$mx], ",")

		if 0 < UBound($array2x) Then
			if $array2x[1]<> "" Then
			;			MsgBox(4096,"M", $array2x[1],5)
	;	Sleep(1000)

				Switch $array2x[1]
					Case "Active"
						If $array2x[2] <>  "false" Then
						  $active = true
						Else
						  $active = false
						EndIf
					Case "IfNum"
						If $array2x[2] <>  "false" Then
						  $value = true
						Else
						  $value = false
						EndIf

						Switch Int($num)
						     Case $array2x[3] To $array2x[4]
								$active = $value
							Case Else
								$active = not $value

						EndSwitch

					Case "IfStr"
						If $array2x[2] <>  "false" Then
						  $value = true
						Else
						  $value = false
						EndIf
						Switch $str
						     Case $array2x[3]
								$active = $value
							Case Else
								$active = not $value

						EndSwitch
				EndSwitch
				If $active <>  false Then

				Switch $array2x[1]
					Case "_WD_Navigate"
						_WD_Navigate($session, $array2x[2])
						If @error Then
						  MsgBox(4096, "error", "Could not open the specified URL. extended=" & @extended ,10)
						  Exit
						EndIf
						Sleep(3000)
					Case "_WD_SetElementValue"
						$value=$array2x[2]
						if $value== "" Then	$value=$xpath
						$value2=$array2x[3]
						if $value2== "" Then	$value2=$text
						$element = _WD_WaitElement($session, $_WD_LOCATOR_ByXPath, $value)
						If @error Then
							Msgbox(4096,"error", $mx&" element get failed",10)
						Else
							_WD_SetElementValue($session, $element, $value2)
							 Sleep(2000)
						EndIf
					Case "_WD_ElementAction"
						$value=$array2x[2]
						if $value== "" Then	$value=$xpath
						$value2=$array2x[3]
						if $value2== "" Then	$value2=$text
						$element = _WD_WaitElement($session, $_WD_LOCATOR_ByXPath, $value)
						If @error Then
							Msgbox(4096,"error", $mx&$value&" element get failed",10)
						Else
							$txt = _WD_ElementAction($session, $element, $value2)
							 Sleep(2000)
							if $value2 ==  "text" Then
								$str = $txt
								MsgBox(4096, "text", $txt,10)
							EndIf
						EndIf
					Case "_ElementActionEx"
						$value=$array2x[2]
						if $value== "" Then	$value=$xpath
						$value2=$array2x[3]
						if $value2== "" Then	$value2=$text
						$element = _WD_WaitElement($session, $_WD_LOCATOR_ByXPath, $value)
						If @error Then
							Msgbox(4096,"error", $mx&$value&" element get failed",10)
						Else
							$txt = _WD_ElementActionEx($session, $element, $value2)
							 Sleep(2000)
							if $value2 ==  "text" Then
								$str = $txt
								MsgBox(4096, "text", $txt,10)
							EndIf
						EndIf
					Case "_WD_DownloadFile"

						$value=$array2x[2]
						if $value== "" Then	$value=$url
						$value2=$array2x[3]
						if $value2== "" Then	$value2=$text
						_WD_DownloadFile($value, $value2)
					Case "Sleep"
						Sleep($array2x[2])
					Case "WinWaitActive"
						WinWaitActive($array2x[2])
					Case "ControlClick"
						ControlClick($array2x[2],$array2x[3],$array2x[4])
						 Sleep(2000)
					Case "ControlSend"
						$value=$array2x[5]
						if $value== "" Then	$value=$text
						ControlSend($array2x[2],$array2x[3],$array2x[4],$value)
						 Sleep(2000)
					Case "XPath"
						$xpath=$array2x[2]
					Case "XPathAdd"
						$value=$array2x[2]
						if $value== "" Then	$value=$num
						$xpath = $xpath & $value
					Case "XPathAdds"
						$value=$array2x[2]
						if $value== "" Then	$value=$str
						$xpath = $xpath & $value
					Case "URL"
						$url=$array2x[2]
					Case "URLAdd"
						$value=$array2x[2]
						if $value== "" Then	$value=$num
						$url = $url & $value
					Case "URLAdds"
						$value=$array2x[2]
						if $value== "" Then	$value=$str
						$url = $url & $value
					Case "Str"
						$str=$array2x[2]
					Case "Str2"
						$value=$array2x[2]
						if $value== "" Then	$value=$str
						$str2 =  $value
					Case "StrStr2"
						$value=$array2x[2]
						if $value== "" Then	$value=$str2
						$str =  $value
					Case "StrAdd"
						$value=$array2x[2]
						if $value== "" Then	$value=$str2
						$str = $str & $value
					Case "StrText"
						$value=$array2x[2]
						if $value== "" Then	$value=$text
						$str =  $value
					Case "StringFormatn"
			;			MsgBox(4096,"M", $n,5)
						$text = StringFormat($array2x[2],$num )
					Case "StringFormats"
						$text = StringFormat($array2x[2] ,$str)
					Case "StringFormatns"
						$text = StringFormat($array2x[2],$num ,$str)
					Case "StringFormatsn"
						$text = StringFormat($array2x[2],$str,$num )
					Case "Num"
						$num=$array2x[2]
						if $num== "" Then	$num = $n
					Case "Num2"
						$num2=$array2x[2]
						if $num2== "" Then	$num2 = $num
					Case "Addn"
						$n = $n + $array2x[2]
						$num = $n
					Case "Add"
						$value=$array2x[2]
						if $value== "" Then	$value=$num2
						$num = $num + $value
					Case "Sub"
						$value=$array2x[2]
						if $value== "" Then	$value=$num2
						$num = $num - $value
					Case "Mul"
						$value=$array2x[2]
						if $value== "" Then	$value=$num2
						$num = $num * $value
					Case "Div"
						$value=$array2x[2]
						if $value== "" Then	$value=$num2
						if $value== 0 Then	$value=1
						$num = $num / $value
					Case "Divx"
						$value=$array2x[2]
						if $value== "" Then	$value=$num2
						if $value== 0 Then	$value=1
						$num = $num / $value + 1
						if mod($num, $value)== 0 Then	$num = $num - 1
					Case "Mod"
						$value=$array2x[2]
						if $value== "" Then	$value=$num2
						if $value== 0 Then	$value=1
						$num = mod($num, $value)
					Case "Modx"
						$value=$array2x[2]
						if $value== "" Then	$value=$num2
						if $value== 0 Then	$value=1
						$num = mod($num, $value)
						If $num = 0 Then 	$num = $value
					Case "Array"
			;			MsgBox(4096,"M", $num,5)

						if $num < UBound($array2x)-1 Then
							if $array2x[$num+1]<> "" Then
								$str = $array2x[$num+1]
							EndIf
						EndIf

					Case "FileWriteLine"
						_numWrite($array2x[2])

					Case "Run"
						$value=$array2x[2]
						if $value== "" Then	$value=$str
						$value2=$array2x[3]
						if $value2== "" Then	$value2=$text
						Run($value & " " & $value2)

					Case "MsgBox"
						$value=$array2x[2]
						if $value== "" Then	$value=$text
						$value2=$array2x[3]
						if $value2== "" Then	$value2=5
						MsgBox(4096,$num, $value,$value2)

					Case "ConsoleWrite"
						$value=$array2x[2]
						if $value== "" Then	$value=$text
						ConsoleWrite($num & " : " & $value &@CRLF)

					Case "ClipGet"
						$str = ClipGet()
					Case "ClipPut"
						$value=$array2x[2]
						if $value== "" Then	$value=$text
						ClipPut($value)
					Case ""
						Exit
					Case Else
				EndSwitch

			EndIf
			EndIf
			Sleep(2000)

		endif
	EndIf
EndFunc

Func _csvread()
	$mm = 0
	$mn = 0
	$file = FileOpen($fn, 0)

;ファイルが読み込みモードで開かれたかどうかチェック
	If $file = -1 Then

	Else
	;EOFに達するまで1文字づつ読み込む。
	While 1
		$linex[$mm] = FileReadLine($file)
		If @error = -1 Then ExitLoop
;		ConsoleWrite("Number of matches: " & $linex[$mm] &@CRLF)
		$mn=$mn+1

		$result = StringLeft($linex[$mm] , 1)
		if $result<> "#" Then
			$mm=$mm+1
		Else
			If $mm <  $start1 Then 	$start1=$start1-1
			If 1 >  $start1 Then 	$start1=1
			If $mm <  $end1 Then 	$end1=$end1-1
		EndIf
;
		If $mm >  $nCount -1 Then ExitLoop
	Wend
	FileClose($file)
	EndIf
;
MsgBox(4096,$start1, $end1,5)

EndFunc

Func _nread($fnn)
	$n = 0
	$file = FileOpen($fnn, 0)
;ファイルが読み込みモードで開かれたかどうかチェック
	If $file = -1 Then

	Else
		$n = FileReadLine($file)
		If @error = -1 Then 	$n = 0
		FileClose($file)
	EndIf
EndFunc
Func _numWrite($fnn)

	$fnn =  @ScriptDir & $idir & "\"  & $fnn

	$file = FileOpen($fnn, 10)
	If $file = -1 Then

	Else
		FileWriteLine($file,$num)
		FileClose($file)
	EndIf
EndFunc
Func StartupWebDriver()

	_WD_Option('Driver', $CHROME_DRIVER)
	_WD_Option('Port', 9515)
	_WD_Option('DriverParams', '--verbose --log-path="' & @ScriptDir & '\chrome.log"')

	return _WD_Startup()

EndFunc

Func SetupBrowser()

	_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')

	Return _WD_CapabilitiesGet()

EndFunc

サンプル

#,,,,,,,
FileReadLine,xsendnum.txt,,,,,,
Loop,10,16,2,,,,
#,,,,,,,
_WD_Navigate,https://www.yahoo.co.jp/,,,,,,
_WD_SetElementValue,/html/body/div[1]/div/header/section[1]/div/form/fieldset/span/input,google,,,,,
_WD_ElementAction,/html/body/div[1]/div/header/section[1]/div/form/fieldset/span/button/span/span,click,,,,,
_WD_ElementAction,/html/body/div[1]/header/div[1]/div[1]/div/div/div[1]/a,click,,,,,
#,,,,,,,
Addn,1,,,,,,
Modx,3,,,,,,
Array,A,B,C,,,,
StringFormatsn,[%s]  %%d = '%d'\n,,,,,,
_WD_SetElementValue,/html/body/div[1]/div/header/section[1]/div/form/fieldset/span/input,,,,,,
_WD_ElementAction,/html/body/div[1]/div/header/section[1]/div/form/fieldset/span/button/span/span,click,,,,,
_WD_ElementAction,/html/body/div[1]/header/div[1]/div[1]/div/div/div[1]/a,click,,,,,
#,,,,,,,
_WD_SetElementValue,/html/body/div[1]/div/header/section[1]/div/form/fieldset/span/input,google,,,,,
_WD_ElementAction,/html/body/div[1]/div/header/section[1]/div/form/fieldset/span/button/span/span,click,,,,,
#,,,,,,,
Str,_,,,,,,
_WD_ElementAction,/html/body/div[1]/header/div[2]/div/div,text,,,,,
Active,false,,,,,,
IfStr,false,_,,,,,
StringFormats,[%s] ,,,,,,
MsgBox,,,,,,,
Active,true,,,,,,
Num,,,,,,,
FileWriteLine,xsendnum.txt,,,,,,
Run,notepad,xsendnum.txt,,,,,

-AutoHotkey

Copyright© eightban's memo , 2024 All Rights Reserved Powered by STINGER.