eightban's memo

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

AutoIt

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

更新日:

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

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

ユーザー関数 対応しました

準備

ソースコード

; ---------------------------------------------------
; 2024-06-20

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

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

; "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 []
; "CallClear" -> $aArgs2
; "CallSet" [] -> $aArgs2
; "Div" $num/$num2 or $num/[] -> $num
; "Divx" -> $num
; "FileWriteLine" $num
; "Int" -> $num
; "Abs" -> $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
; "StringStripWS" $text or [] -> $str
; "StringFormatn" $num -> $text
; "StringFormatns" $num $str -> $text
; "StringFormats" $str -> $text
; "StringFormatsn" $str $num -> $text
; "TextStr" $str -> $text
; "Sub" $num-$num2 or $num-[] -> $num
; "URL" $text or []-> $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 = ""
;Global $fn =  @ScriptDir & $idir & "\xsend_linetm"  & ".csv"
;Global $fn =  @ScriptDir & $idir & "\xsend_linetmrls"  & ".csv"
;
Global $fn =  @ScriptDir & $idir & "\xsend_linems"  & ".csv"
;Global $fn =  @ScriptDir & $idir & "\xsendkey"  & ".csv"
;Global $fn =  @ScriptDir & $idir & "\chrome-for-testing"  & ".csv"

Else
 Global  $idir = "\" & $CmdLine[1] & "\"
 Global $fn =  @ScriptDir & $idir & $CmdLine[2]

EndIf

Global $mm
Global $mn
Global $nCount = 300
Global $n = 0
Global $num = 0
Global $num2 = 0
Global $active = true
Global $exitloop1 = false
Global $start1 = 1
Global $end1 = 1
Global $loop1 = 1
Global $text = ""
Global $str = ""
Global $str2 = ""
Global $xpath = ""
Global $url = ""
Global $sCount = 10
Global $aArgs2[$sCount]

For $iz=1 To $sCount Step 1
  $aArgs2[$iz-1] = ""
Next


Dim $linex[$nCount]

;????????WebDriver???????
Const $CHROME_DRIVER = 'C:\app\AutoIt3\chromedriver.exe'
Const $CHROME_profile = 'C:\Users\username\AppData\Local\Google\Chrome\User DatAu3'
Const $EDGE_driver = 'C:\app\AutoIt3\msedgedriver.exe'
Const $EDGE_profile = 'C:\Users\username\AppData\Local\Microsoft\Edge'

;
Global $web_driver_pid
Global $session
_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 "_StartWeb"
						$value=$array2x[2]
						if $value== "" Then	$value=	 "chrome"
						_StartWeb($value)

					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)
  If 	$exitloop1 = true Then ExitLoop 2

 Next
Next
If $mm >  $end1 Then
For $i=$end1 To $mm -1 Step 1
 _sendkyx($i)
Next
EndIf
Sleep(3000)


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

	if $linex[$mx]<> "" Then
		Sleep(100)
		$array2x = StringSplit($linex[$mx], ",")
		$aArgs = 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 "_EndWeb"
						_EndWeb()

					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)
							$str =  "failed"

						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)
							$str =  "failed"
						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"
						$value=$array2x[5]
						if $value== "" Then	$value="left"
						$value2=$array2x[6]
						if $value2== "" Then	$value2=1
						ControlClick($array2x[2],$array2x[3],$array2x[4],$value,$value2)
						 Sleep(2000)
					Case "ControlSend"
						$value=$array2x[5]
						if $value== "" Then	$value=$text
						ControlSend($array2x[2],$array2x[3],$array2x[4],$value)
						 Sleep(2000)
					Case "XPath"
						$value=$array2x[2]
						if $value== "" Then	$value=$text
						$xpath=$value
					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"
						$value=$array2x[2]
						if $value== "" Then	$value=$text
						$url=$value
					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 "TextStr"
						$value=$array2x[2]
						if $value== "" Then	$value=$str
						$text =  $value
					Case "StringStripWS"
						$value=$array2x[2]
						if $value== "" Then	$value=$text
						$value2=$array2x[3]
						if $value2== "" Then	$value2=3
						$str =  StringStripWS($value, $value2)
					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 "StringFormatnn2"
						$text = StringFormat($array2x[2],$num,$num2)
					Case "StringFormatn2n"
						$text = StringFormat($array2x[2],$num2,$num )
					Case "StringFormatsn"
						$text = StringFormat($array2x[2],$str,$num )
					Case "StringFormatsns2"
						$text = StringFormat($array2x[2],$str,$num,$str2 )
					Case "StringFormatns2s"
						$text = StringFormat($array2x[2],$num,$str2,$str )
					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
						if mod($num, $value)== 0 Then	$num = $num - 1
						$num = Int($num / $value) + 1
					Case "Abs"
						$num = Abs($num)
					Case "Int"
						$num = Int($num)
					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)
						ConsoleWrite($value & " " & $value2 &@CRLF)

					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($n & " : " & $value &@CRLF)

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

					Case "CallClear"
						For $iz=1 To $sCount Step 1
						  $aArgs2[$iz-1] = ""
						Next

					Case "CallSet"
						$value=$array2x[2]-1

						if $value < UBound($aArgs2) Then
								$aArgs2[$value] = $text
						EndIf

					Case "ExitLoop"
						$exitloop1 = true

					Case ""
						Exit

					Case "_StartWeb"
						ConsoleWrite($num & " Console: " & $array2x[1] &@CRLF)

					Case Else
						$tx = StringLeft ( $array2x[1],1)
			;			MsgBox(4096,"M", $array2x[1],5)
						if $tx== "_" Then

				;
							_ArrayDelete($aArgs, 0)
							_ArrayDelete($aArgs, 0)

							For $iz=1 To  UBound($aArgs)  Step 1
								if $aArgs[$iz-1] == "" Then	$aArgs[$iz-1] = $aArgs2[$iz-1]
							Next

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

							Call($array2x[1], $aArgs)
						EndIf




				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)

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

	Else
		FileWriteLine($file,$num)
		FileClose($file)
	EndIf
EndFunc
Func _StartupWebDriver($browser)
	$driver = $CHROME_DRIVER
	if $browser == "edge" Then $driver = $EDGE_driver

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

	return _WD_Startup()

EndFunc

Func _SetupBrowser($browser)
	$profile = $CHROME_profile
	if $browser == "edge" Then $profile = $EDGE_profile

	_WD_CapabilitiesStartup()
	_WD_CapabilitiesAdd('alwaysMatch', $browser)
	_WD_CapabilitiesAdd('w3c', True)
	_WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
	_WD_CapabilitiesAdd('args','--user-data-dir=' & $profile)
;    _WD_CapabilitiesAdd('args', '--headless')
	if $browser == "edge" Then _WD_CapabilitiesAdd('ms:edgeOptions')


	Return _WD_CapabilitiesGet()

EndFunc
Func _StartWeb($browser)
	Global $web_driver_pid = _StartupWebDriver($browser)
;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($browser)
	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
EndFunc
Func _EndWeb()

	_WD_Window($session, "close")
	If $session Then _WD_DeleteSession($session)
	Sleep(2000)

	If $web_driver_pid Then _WD_Shutdown()

EndFunc

サンプル

#,,,,,,,
FileReadLine,xsendnum.txt,,,,,,
Loop,11,17,2,,,,
_StartWeb,,,,,,,
#,,,,,,,
_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,,,,,
_EndWeb,,,,,,,
CallSet,1,,,,,,
_ArrayDisplay,,_ArrayD,,,,,

webdriverダウンロードサンプル

#,,,,,,,
_StartWeb,,,,,,,
_WD_Navigate,https://googlechromelabs.github.io/chrome-for-testing/,,,,,,
_WD_ElementAction,/html/body/section[1]/p/code[1],text,,,,,
StringFormatsn,https://storage.googleapis.com/chrome-for-testing-public/%s/win64/chromedriver-win64.zip,,,,,,
MsgBox,,,,,,,
URL,,,,,,,
_WD_DownloadFile,,C:\\app\\autoit3\\Examples\\chromedriver-win64.zip,,,,,
Run,powershell,Expand-Archive -Path C:\\app\\autoit3\\Examples\\chromedriver-win64.zip -DestinationPath C:\\app\\autoit3\\Examples\\ -Force,,,,,
_EndWeb,,,,,,,
Run,powershell,Copy-Item C:\\app\\autoit3\\Examples\\chromedriver-win64\\chromedriver.exe -Destination C:\\app\\autoit3\\,,,,,

Chrome の開くダイヤログで余計なものが入っている場合の対応

ダブルクリックで入力されている文字を選択しデリートキーで消す操作を入れています


WinWaitActive,開く,,,,,,,,,,,,,,,,,,,,,,,,,,
ControlClick,開く,,[CLASS:Edit; INSTANCE:1],left,2,,,,,,,,,,,,,,,,,,,,,,
ControlSend,開く,,[CLASS:Edit; INSTANCE:1],{DEL},,,,,,,,,,,,,,,,,,,,,,,
ControlSend,開く,,[CLASS:Edit; INSTANCE:1],,,,,,,,,,,,,,,,,,,,,,,,
ControlClick,開く,,Button1,,,,,,,,,,,,,,,,,,,,,,,,

-AutoIt

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