Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 01/01/2022 in all areas

  1. As the WebDriver UDF - Help & Support thread has grown too big, I started a new one. The prior thread can be found here.
    1 point
  2. I had to read the W3C spec to find out how async is different from sync execution. Turns out it's not very different from the sync version, the only difference is that your code gets an extra function argument (which is appended to the special arguments array) that you can call to signal that the script is done, you can also supply a value to this function which will be set as the return value. Here's the demo code I used to test this: _WD_ExecuteScript($sSession, 'setTimeout(() => arguments[0]("foobar"), 3000)', True) This does the same thing as: _WD_ExecuteScript($sSession, 'return "foobar"') but with a delay of 3 seconds. This feature is only useful in complex scripts which require waiting for something on the web-page.
    1 point
  3. I have added some comments\suggestions to the code. It's very clean for you're 2/3 rd script, you're picking things up faster than I did #NoTrayIcon #RequireAdmin #include <Constants.au3> #include <FileConstants.au3> #include <MsgBoxConstants.au3> Cleanup(False) Main() #cs #### General #### A Nice script for you 2/3 rd? script. Nice and clean with simple functions @scriptdir and @workingdir returns will include a trailing backslash if the script is in a root dir you will have issues with file paths that use these macros, which may cause copy errors. It's personal choice, but I like to add blank lines between code or comments to break it up, it makes it easier for me to read. See Main function for example add error checking for functions that you action later on, if it is vital they have succeeded. i.e. $winShell = ObjCreate("shell.application") add basic logging to keep a record of file paths, variable values, return values etc, especially if you are going to release it for others to use. If there's a problem, they can just post the log. Might help with debugging and save them some legwork add comments for future reference. It might help when you have the headscratch moment as in, WTF is that supposed to do\what was I thinking :) #### Main() #### FileOpenDialog\FileSelectFolder return string paths. A bit nit picky but $h_wimfile should be $s_wimfile and $h_folder, $s_folder if you wanted to follow variable naming conventions as a handle is not returned. FileOpenDialog - Don't use magic numbers for the options parameter. There are constants for this function 1 = $FD_FILEMUSTEXIST Again personal choice but I like to split function calls that have a lot of parameters or long parameter values for readability it makes it easier to see each parameter quickly instead of searching the line for the ',' i.e. as with PathIsWritable() dll call MsgBox - Don't use magic numbers for the flag parameter. There are constants for the msgbox types and perhaps add icons for the associated message error, info, warning etc #### MountWim #### Create a function that returns the TempWIM\MountDir paths. There is a lot of repeated code and if you decide to rename the folders later you'll have to find\replace. This way it is only set in one place. You could also add a regex to remove any trailing slash from the two macros mentioned above. you can call the function rather than keep repeating the path i.e. as in WIMFileOnly() Another personal thing but I like to declare same variable types together and on seperate lines. easier to read and add comments to describe what each variable is for (if needed) #### UnMountWim #### same as MountWim regarding variable declaration, line spacing or comments #### WIMFileOnly #### if you just want the file name of the wim then you could use a regexp Return StringRegExpReplace($s_Path, "^.*\\", "") #### ExportWim #### same as MountWim regarding variable declaration, line spacing or comments #### CopyWIM #### fails for me probably due to file paths and double '\\' "D:\New AutoIt v3 Script (2).au3" (188) : ==> The requested action with this object has failed.: $winShell.namespace($todirectory).CopyHere($fromFile, $FOF_RESPOND_YES) $winShell.namespace($todirectory)^ ERROR you could use _WinAPI_ShellFileOperation to copy the file or _WinAPI_CopyFileEx, this has a callback you can update the progress bar with. #### DISMProgress #### same as MountWim regarding variable declaration, line spacing or comments #ce Func Main() Local $s_wimfile = FileOpenDialog( _ 'Select the boot.wim file to permanently inject drivers into', _ @WorkingDir & '\', _ 'Windows Image File (*.WIM)', _ $FD_FILEMUSTEXIST, _ 'boot.wim') If $s_wimfile Then FileChangeDir(@ScriptDir) If PathIsWritable($s_wimfile) Then Local $h_folder = FileSelectFolder( _ 'Select the folder containing the drivers (Subfolders w/spaces Allowed)', _ @WorkingDir) If $h_folder Then FileChangeDir(@ScriptDir) MountWim($s_wimfile) FileChangeDir($h_folder) AddDrivers($h_folder) UnMountWim() ExportWim($s_wimfile) ; using $MB_OK (0) instead of 1 $MB_OKCANCEL. Unless you wanted ; OK and Cancel buttons. Also maybe add related icons for the msgbox ; MsgBox( _ BitOR($MB_OK, $MB_ICONINFORMATION), _ 'Process Complete...', _ 'Done.') Cleanup() Else MsgBox( _ BitOR($MB_OK, $MB_ICONERROR), _ 'Process Aborted...', _ 'No driver folder selected!') Cleanup() EndIf Else MsgBox( _ BitOR($MB_OK, $MB_ICONERROR), _ 'Process Aborted...', _ 'You do not have write access!') Cleanup() EndIf Else MsgBox( _ BitOR($MB_OK, $MB_ICONERROR), _ "Process Aborted...", _ "No WIM selected!") Exit EndIf EndFunc ;==>Main Func MountWim($h_wimfile) DirCreate(@ScriptDir & '\TempWIM') DirCreate(@ScriptDir & '\MountDir') CopyWIM($h_wimfile, @ScriptDir & '\TempWIM') Local _ $s_DISMCommand = ' /Mount-Image /ImageFile:' & '"' & @ScriptDir & '\TempWIM\' & WIMFileOnly($h_wimfile) & '"' & ' /Index:1 /MountDir:' & '"' & @ScriptDir & '\MountDir' & '"', _ $s_ProgTitle = 'Mounting Image', _ $s_ProgAction = 'Mounting boot.wim - ' DISMProgress($s_DISMCommand, $s_ProgTitle, $s_ProgAction) ProcessWaitClose('DISM.EXE') EndFunc ;==>MountWim Func AddDrivers($h_folder) RunWait(@ComSpec & ' /c DISM /Image:' & '"' & @ScriptDir & '\MountDir' & '"' & ' /Add-Driver /Driver:' & '"' & $h_folder & '"' & ' /ForceUnsigned /Recurse', @WorkingDir) EndFunc ;==>AddDrivers Func UnMountWim($s_commit = '/Commit') Local $s_DISMCommand = ' /Unmount-Image /MountDir:' & '"' & @ScriptDir & '\MountDir' & '" ' & $s_commit Local $s_ProgTitle = 'Saving Image', $s_ProgTitle2 = 'UnMounting Image', $s_ProgAction = 'Processing with ' & $s_commit & ' flag - ' Local $s_step = False DISMProgress($s_DISMCommand, $s_ProgTitle, $s_ProgAction, $s_step, $s_ProgTitle2) ProcessWaitClose('DISM.EXE') EndFunc ;==>UnMountWim Func Cleanup($b_Exit = True) If FileExists(@ScriptDir & '\MountDir') Then DirRemove(@ScriptDir & '\MountDir', $DIR_REMOVE) If FileExists(@ScriptDir & '\TempWIM') Then DirRemove(@ScriptDir & '\TempWIM', $DIR_REMOVE) If $b_Exit Then Exit EndFunc ;==>Cleanup Func WIMFileOnly($h_wimfile) Local Static $h_wimfilename = StringMid($h_wimfile, StringInStr($h_wimfile, "\", 2, -1) + 1) Return $h_wimfilename EndFunc ;==>WIMFileOnly Func ExportWim($h_wimfile) Local $s_DISMCommand = ' /Export-Image /SourceImageFile:' & '"' & @ScriptDir & '\TempWIM\' & WIMFileOnly($h_wimfile) & '"' & ' /SourceIndex:1 /DestinationImageFile:' & '"' & @ScriptDir & '\' & WIMFileOnly($h_wimfile) & '"' & ' /Compress:max' Local $s_ProgTitle = 'Exporting Image', $s_ProgAction = 'Cleaning Windows Image', $s_DestFolder = _FAF_DirectoryGetPathFromPath($h_wimfile) DISMProgress($s_DISMCommand, $s_ProgTitle, $s_ProgAction) ProcessWaitClose('DISM.EXE') FileDelete($h_wimfile) CopyWIM(@ScriptDir & '\' & WIMFileOnly($h_wimfile), $s_DestFolder) FileDelete(@ScriptDir & '\' & WIMFileOnly($h_wimfile)) EndFunc ;==>ExportWim Func _FAF_DirectoryGetPathFromPath($s_Path, $i_Backslash = 1) If $i_Backslash Then Return StringRegExpReplace($s_Path, "(^.*\\)(.*)", "\1") Return StringRegExpReplace($s_Path, "\\[^\\]*$", "") EndFunc ;==>_FAF_DirectoryGetPathFromPath Func PathIsWritable($sFile) Local $aRet = DllCall('kernel32.dll', 'handle', 'CreateFileW', _ 'wstr', $sFile, _ 'dword', 2, _ 'dword', 7, _ 'struct*', 0, _ 'dword', 3, _ 'dword', 0x02000000, _ 'handle', 0) If @error Or $aRet[0] = Ptr(-1) Or $aRet[0] = 0 Then Return False DllCall('kernel32.dll', 'bool', 'CloseHandle', 'handle', $aRet[0]) Return True EndFunc ;==>PathIsWritable Func CopyWIM($fromFile, $todirectory) Local $FOF_RESPOND_YES = 16 Local $FOF_SIMPLEPROGRESS = 256 $winShell = ObjCreate("shell.application") $winShell.namespace($todirectory).CopyHere($fromFile, $FOF_RESPOND_YES) EndFunc ;==>CopyWIM Func DISMProgress($s_DISMCommand, $s_ProgTitle, $s_ProgAction, $s_step = True, $i_alt = '') Local $i_percent = 0, $i_value = 0, $i_a = 0 Local $s_DISM = Run(@ComSpec & ' /c DISM' & $s_DISMCommand, @ScriptDir, '', 2 + 4) ProgressOn($s_ProgTitle, $s_ProgAction, ' 0 Percent') While ProcessExists($s_DISM) $line = StdoutRead($s_DISM, 5) If StringInStr($line, '.0%') Then $line1 = StringSplit($line, '.') $i_value = StringRight($line1[$line1[0] - 1], 2) $i_value = StringStripWS($i_value, 7) EndIf If $i_value == "00" Then $i_value = 100 If @error Then ExitLoop Sleep(50) If $i_percent <> $i_value Then ProgressSet($i_value, $s_ProgTitle, $s_ProgAction & $i_value & "%") $i_percent = $i_value EndIf If $s_step = False Then If $i_value = 100 Then $i_a += 1 If $i_a = 1 Then $i_value = 1 $s_ProgTitle = $i_alt EndIf EndIf ElseIf $i_value = 100 Then ExitLoop EndIf WEnd ProgressOff() EndFunc ;==>DISMProgress
    1 point
  4. Solved the authorization problem. Just reload the code from post #1
    1 point
  5. i think you should check on GPEDIT Computer Policy > Windows Settings > Script (Shutdown) This method will run script whenever you shutdown.
    1 point
  6. Great @Danp2. Sometimes knowing the right search terms helps so much. OK that worked: $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"unhandledPromptBehavior": "ignore", ' & _ '"goog:chromeOptions": {"w3c": true, "excludeSwitches": ["enable-automation"], "useAutomationExtension": false, ' & _ '"prefs": {"credentials_enable_service": false}, "args": ["start-maximized"] }}}}' Thanks
    1 point
  7. @NassauSky A better option would be to disable to feature in Chome. This has been previously posted on the forum. Search for "credentials_enable_service" and you will find it.
    1 point
  8. Ok I did it: #include "wd_helper.au3" #include "wd_core.au3" OnAutoItExitRegister("_onExit") Local $sDesiredCapabilities $_WD_DEBUG = True ; You could also use $_WD_DEBUG_Error ;~ You can also control the visibility of the console with the function _WD_ConsoleVisible. _WD_Option('Driver', 'chromedriver.exe') _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path=' & @ScriptDir & '\chrome.log') $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["--user-data-dir=C:\\Users\\' & @UserName & '\\AppData\\Local\\Google\\Chrome\\User Data\\", "--profile-directory=Default"]}}}}' _WD_Startup() $sSession = _WD_CreateSession($sDesiredCapabilities) If @error = $_WD_ERROR_Success Then _WD_Navigate($sSession, "https://www.amazon.com/s?k=guide+to+investing+crypto&i=digital-text&page=2&qid=1587374511&ref=sr_pg_2") _WD_LoadWait($sSession) _WD_jQuerify($sSession) local $jqueryCommand = "jQuery($('head').append('<script type=" & 'text/javascript' & "> var myPage;</script>'))" _WD_ExecuteScript($sSession, $jqueryCommand ) local $jqueryCommand = "jQuery($.ajax({ url:'www.amazon.com/dp/B07H71XXGY',success:function(result){myPage = result; console.log('loaded');}}))" _WD_ExecuteScript($sSession, $jqueryCommand ) while 1 local $answer = _WD_ExecuteScript($sSession, "return myPage;") if StringLen($answer) > 1000 Then ExitLoop Sleep(10) WEnd MsgBox(0,'',"where's my result page loaded by jquery ajax?") EndIf Func _onExit() _WD_DeleteSession($sSession) _WD_Shutdown() EndFunc I get the page content (even if with only the "<" tag as "\u003C"), anyway, is it possible to transform in some way a text var like this into elements parsable with xpath or jquery or I can only parse it with StringRegExp?
    1 point
×
×
  • Create New...