Jump to content

WebDriver UDF - Help & Support (II)


Danp2
 Share

Recommended Posts

@Danp2 I should've known that, sorry :)

Spoiler

Note: Normally I use a function that checks errors after every line. I verified everything works through the console output until the final ShadowRoot function

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

Main()

Func Main()

    _WD_Option('Driver', @UserProfileDir & '\Documents\chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--log-path=' & @UserProfileDir & '\Documents\chrome.log')
    Local $sDesiredCapabilities = '{"capabilities":{"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["--disable-extensions","--profile-directory=Default"]}}}}'

    _WD_Startup()
    Local $sSession = _WD_CreateSession($sDesiredCapabilities)

    Sleep(50)
    _WD_ConsoleVisible(False)

    _WD_Navigate($sSession, "chrome://settings/content/pdfDocuments")
    Local $oSettingsUISR = _WD_GetShadowRoot($sSession, $_WD_LOCATOR_ByTagName, "settings-ui")
    Local $oTemp = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "#container", $oSettingsUISR)
    Local $oSettingsMainSR = _WD_GetShadowRoot($sSession, $_WD_LOCATOR_ByTagName, "settings-main", $oTemp)
    Local $oSettingsBasicPageSR = _WD_GetShadowRoot($sSession, $_WD_LOCATOR_ByTagName, "settings-basic-page", $oSettingsMainSR)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : "_WD_GetShadowRoot" & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    ; Haven't used the result yet
    #forceref $oSettingsBasicPageSR

    ; Untested section, just guessed with an XPath
;~  Local $oSettingsPrivacySR = _WD_GetShadowRoot($sSession, $_WD_LOCATOR_ByXPath, "//div/settings-section/settings-privacy-page", $oSettingsBasicPageSR)
;~  Local $oSettingsPdfSR = _WD_GetShadowRoot($sSession, $_WD_LOCATOR_ByXPath, "/settings-animated-pages/settings-subpage/settings-pdf-documents", $oSettingsPrivacySR)
;~  Local $oToggleButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "/settings-toggle-button", $oSettingsPdfSR)

;~  _WD_ElementAction($sSession, $oToggleButton, "click")

EndFunc

 

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

image.png.64a19767e5d679efed55ffd5484805e7.png

But with your modifikation of my test script I still get the same results. All @error = 0 and all @extended from WD_Status = 200. And I don't find the reason. @error behind Session=Dummy and SessionDeleted must be > 0. That workr in other programs.

  

Edited by HJL
Link to comment
Share on other sites

@HJL It shouldn't be too difficult to add some coding to check for the invalid session and return an appropriate error code. But this will add some additional overhead because the returned JSON string will need to be examined to see if an error has occurred. For now, you can check the return value of the function yourself and take the appropriate action.

For example, if you change your script so that you capture the return value of _WD_Timeouts, you will then have a variable containing text like one of these --

{"value":{"script":30000,"pageLoad":300000,"implicit":0}}

{"value":{"error":"invalid session id","message":"Got unexpected session id Dummy","stacktrace":""}}

So the latter one would indicate that the session isn't valid any longer.

As far as _WD_Status, I've looked into its functionality and I believe that it is working properly. See here for details from the W3C specs. I have updated the function so that it now returns a Dictionary object with "message" and "ready" items that hold the response from the webdriver.

Link to comment
Share on other sites

There must be another error in my testscript not in your scripts:

Func _WD_SessionNotExists($sSession)  ; Check if session still exists
    _WD_Timeouts($sSession)
    Return @error
EndFunc   ; ==>_WD_SessionExists

This works fine and returns the right value in @error ( 0 / 10 ), I don't understand why in my testscript all returned values for @error are 0.

Link to comment
Share on other sites

Most of the time this kind of problem is caused by a funtion resetting @error and @extended.
Could you create the output in two lines? Just to make sure that TimerDiff does not reset @error and @extended.
Means replace

_WD_Status()
    $sE2 = $sE2 & $sText & @TAB & @error&"/"&@extended & @TAB & @TAB & TimerDiff($hTimer) & @CRLF

with

_WD_Status()
    $sE2 = $sE2 & $sText & @TAB & @error & "/" & @extended & @TAB & @TAB
    $sE2 = $sE2 & TimerDiff($hTimer) & @CRLF

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I think the problem is caused by a function you call before $sE2 has been fully created.
According to the help file for function SetError: "When entering a function @error is set to 0." Means even if you do nothing in a function that sets @error, it will be reset to 0 on entry to the function.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@DAN
see my last posts, that was my script and the results as screenshots.

@water

_WD_Status()
    $sE = @error
    $sE2 = $sE2 & $sText & @TAB & $sE & " / " & @extended & @TAB & @TAB
    $sE2 = $sE2 & TimerDiff($hTimer) & @CRLF

That was what I thought and tried before, but even that example above does not bring the correct results for @error. Strange: @extended always shows the correct values except at wd_status, @error never.

image.png.17113faa743d22450f51827902710f78.png

 

#include "wd_core.au3"
#include "wd_helper.au3"
#include <MsgBoxConstants.au3>
;MsgBox($MB_TOPMOST,"Errors", "@error:" & @error & @CRLF & "@extended:" & @extended & @CRLF)

;$_WD_DEBUG = False

Local $sDesiredCapabilities, $sSession, $sURL
Local $sE1 = "WD_Timeout:         @error/                  Time" & @CRLF & @tab & @TAB & "@extended" & @CRLF
Local $sE2 = "WD_Status:"   & @CRLF
Local $sE3 = "WD_Action:"   & @CRLF
Local $sE4 = "WD_Navigate:" & @CRLF

Test()  ; Exit with "Esc"

Func Test()
    SetUpGecko()
    _WD_Startup()
    $sSession = _WD_CreateSession($sDesiredCapabilities)
    _WD_LoadWait($sSession, 2000)
    _WD_Navigate($sSession, "http://wdr.de")
    _WD_LoadWait($sSession, 2000)

    TestTime("Normal:      ")
    _WD_LoadWait($sSession, 2000)

    Local $sSessionSave = $sSession
    $sSession = "Dummy"
    TestTime("Session=Dummy: ")

    _WD_DeleteSession($sSessionSave)
    TestTime("SessionDeleted: ")

    MsgBox($MB_SYSTEMMODAL, "Time Differences", $sE1 & @CRLF & $sE2 & @CRLF  & $sE3 & @CRLF & $sE4 & @CRLF)

    _WD_ShutDown()
EndFunc  ;==> ReadLoop

Func TestTime($sText)
    Local $hTimer = TimerInit()
    _WD_Timeouts($sSession)
    $sE = @error
    $sE1 = $sE1 & $sText & @TAB & $sE & " / " & @extended & @TAB & @TAB
    $sE1 = $sE1 & TimerDiff($hTimer) & @CRLF

    Local $hTimer = TimerInit()
    _WD_Status()
    $sE = @error
    $sE2 = $sE2 & $sText & @TAB & $sE & " / " & @extended & @TAB & @TAB
    $sE2 = $sE2 & TimerDiff($hTimer) & @CRLF

    Local $hTimer = TimerInit()
    _WD_Action($sSession, "refresh")
    $sE = @error
    $sE3 = $sE3 & $sText & @TAB & $sE & " / " & @extended & @TAB & @TAB
    $sE3 = $sE3 & TimerDiff($hTimer) & @CRLF

    Local $hTimer = TimerInit()
    _WD_Navigate($sSession, "http:sdr.de")
    $sE = @error
    $sE4 = $sE4 & $sText & @TAB & $sE & " / " & @extended & @TAB & @TAB
    $sE4 = $sE4 & TimerDiff($hTimer) & @CRLF

EndFunc

Func SetupGecko()
    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('DriverParams', '--log trace --marionette-port 2828')
    _WD_Option('Port', 4444)
    $sDesiredCapabilities = '{"capabilities":{"alwaysMatch": {"moz:firefoxOptions": {"args": ["-profile", "C:\\webdriver\\FF_WD_Profile"]}}}}'
EndFunc

 

Link to comment
Share on other sites

This example uses @extended and wd_timeouts and works fine.

image.png.6a3d36878a84b72f1d6cf7547beb6cbd.png

#include "wd_core.au3"
#include "wd_helper.au3"
#include <MsgBoxConstants.au3>
Local $sDesiredCapabilities, $sSession, $sURL

Test()

Func Test()
    SetUpGecko()
    _WD_Startup()
    $sSession = _WD_CreateSession($sDesiredCapabilities)
    _WD_LoadWait($sSession, 1000)
    _WD_Navigate($sSession, "http://wdr.de")
    $sSessionSave = $sSession

    $sE1 = _WD_SessionNotExists($sSession)

    $sSession = "Dummy"
    $sE2 = _WD_SessionNotExists($sSession)

    _WD_DeleteSession($sSessionSave)
    $sE3 = _WD_SessionNotExists($sSession)

    MsgBox($MB_SYSTEMMODAL, "Results", _
    '$sSession normal    ' & @TAB & "Session not exists: " & $sE1 & @CRLF & _
    '$sSession="Dummy"   ' & @TAB & "Session not exists: " & $sE2 & @CRLF & _
    '$sSession deleted   ' & @TAB & "Session not exists: " & $sE3)

    _WD_ShutDown()
EndFunc  ;==> ReadLoop

Func _WD_SessionNotExists($sSession)  ; Check if session still exists
    _WD_Timeouts($sSession)
    Return @extended > 200
EndFunc   ; ==>_WD_SessionExists

Func SetupGecko()
    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('DriverParams', '--log trace --marionette-port 2828')
    _WD_Option('Port', 4444)
    $sDesiredCapabilities = '{"capabilities":{"alwaysMatch": {"moz:firefoxOptions": {"args": ["-profile", "C:\\webdriver\\FF_WD_Profile"]}}}}'
EndFunc

 

 

image.png

Edited by HJL
Link to comment
Share on other sites

@HJL Can you please specify which version of the WebDriver UDF you use? Or did I overlook this information?
You will find this information in the SciTE output pane:

Quote

_WDStartup: WD.au3:    0.2.0.7

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

When running the following script with version 0.2.0.8 of the UDF there seems to be a problem with the update check (I'm running from a company network).
So I always get: "Update available" but I think it is a connection problem.

#include "wd_core.au3"
#include "wd_helper.au3"

Local $sDesiredCapabilities, $sSession
SetupGecko()
_WD_Startup()
If @error <> $_WD_ERROR_Success Then Exit -1
$sSession = _WD_CreateSession($sDesiredCapabilities)
MsgBox(0, "", "Finished!")
_WD_DeleteSession($sSession)
_WD_Shutdown()

Func SetupGecko()
    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('DriverParams', '--log trace')
    _WD_Option('Port', 4444)

    $sDesiredCapabilities = '{"desiredCapabilities":{"javascriptEnabled":true,"nativeEvents":true,"acceptInsecureCerts":true}}'
EndFunc   ;==>SetupGecko

I modified function _WD_Startup to get the error code:

If @error = 0xDEAD And @extended = 0xBEEF Then
            $lLatest = True
        ElseIf @error Then
            $sUpdate = " (@error = " & @error & " when checking for Update)"
        Else
            $sUpdate = $lLatest ? "" : " (Update available)"
        EndIf
_WD_IsLatestRelease: 
_WD_IsLatestRelease ==> Send / Recv error
_WDStartup: OS: WIN_10 WIN32_NT 17134 
_WDStartup: AutoIt: 3.3.14.5
_WDStartup: WD.au3: 0.2.0.8 (@error = 6 when checking for Update)
_WDStartup: WinHTTP:    1.6.4.1
_WDStartup: Driver: geckodriver.exe
_WDStartup: Params: --log trace
_WDStartup: Port:   4444

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water I changed that section of code to this --

Select
            Case @error = 0xDEAD And @extended = 0xBEEF
                $sUpdate = "" ; update check not performed

            Case @error
                $sUpdate = " (Update status unknown [" & @error & "])"

            Case $lLatest
                $sUpdate = " (Up to date)"

            Case Not $lLatest
                $sUpdate = " (Update available)"

        EndSelect

Appreciate the feedback!

Link to comment
Share on other sites

Hi,

I am trying to login to a bank's web page to download some documents automatically. 

But it looks like the page is aware of the automation script. It always navigates to an error page. The same error is shown for good and bad credentials.  The iMacros script is working well on the page. The error message is also attached. Is there anything else I need to take care of while automating a login page using web driver?

#include "wd_core.au3"
#include "wd_helper.au3"

Local $sDesiredCapabilities, $sSession
 SetupChrome()
_WD_Startup()
$sSession = _WD_CreateSession($sDesiredCapabilities)
_Login()
_WD_DeleteSession($sSession)
_WD_Shutdown()
Func _Login()
     $sURL = "https://secure.chase.com"
    $iRet = _WD_Navigate($sSession, $sURL)
    $iRet = _WD_LoadWait($sSession, 500, 5000)
    Sleep(1500)
    $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//iframe[@id='logonbox']")
    _WD_FrameEnter($sSession, $sElement)
    Sleep(1500)
    $sElement = _WD_FindElement($sSession, "xpath", "//input[@id='userId-text-input-field']")
    ;_WD_ElementAction($sSession, $sElement, 'click')
    ;Sleep(1500)
    _WD_ElementAction($sSession, $sElement, 'value',"username" )
    Sleep(1500)
    $sElement = _WD_FindElement($sSession, "xpath", "//input[@id='password-text-input-field']")
   ;_WD_ElementAction($sSession, $sElement, 'click')
   ;Sleep(1500)
    _WD_ElementAction($sSession, $sElement, 'value',"password" )
    Sleep(1500)
    $sElement = _WD_FindElement($sSession, "xpath", "//button[@id='signin-button' and @type='submit']")
    _WD_ElementAction($sSession, $sElement, 'click' )
     Sleep(1500)
     msgbox(0,"Done", "Done")

EndFunc
Func SetupChrome()
_WD_Option('Driver', 'chromedriver.exe')
_WD_Option('Port', 9515)
_WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true }}}}'
EndFunc

 

Chase.png

Link to comment
Share on other sites

@PoojaKrishna I see the same thing with Chrome. Worked fine for me when testing with Firefox. <shrug>

If you check out the Developer Tools on the open Chrome browser, you should see an entry like this in the console --

?fromOrigin=https://secure07b.chase.com:51 POST https://secure07b.chase.com/auth/fcc/login 403 (Forbidden)

So the form POST attempt is erroring out with a response of 403 (Forbidden).

Link to comment
Share on other sites

Hi @Danp2 thanks for the udf, its beautiful.

 

My question is as follows:

I've a work page with this datepicker on it: http://eonasdan.github.io/bootstrap-datetimepicker/ that you use to download an excel in beetween 2 dates.

 

The problem is that it only works when you populate the input via text or with the popup. It doesn't fill the input value element, but it uses the bootstrap and js to do it, so when I use the code below it fills the html but when I click the button to download the excel it doens't do anything, as if the input were empty.

 

$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, '//*[@id="fechaDesde"]')
    _WD_ElementAction($sSession, $sElement, 'value', '07/05/2020')

 

Link to comment
Share on other sites

@JackER4565 It's not clear from your example, but I believe that you are targeting the wrong element. You have to target the input element, not the div element that represents the main datetime picker. The following appears to me to work correctly on the link you provided --

$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='datetimepicker1']//input[@class='form-control']")
_WD_ElementAction($sSession, $sElement, 'value', '07/05/2020')

Please try and let me know if this works for you.

Link to comment
Share on other sites

It looks like the ShadowRoot issue I was having is related to this 2 year old Selenium issue. Tldr; ShadowRoot objects can't use $_WD_LOCATOR_ByTagName and $_WD_LOCATOR_ByXPath. I'm not sure how $_WD_LOCATOR_ByLinkText works, but that might not be supported either.

To fix my code, I replaced the XPaths with ByCSSSelector and swapped all /'s with spaces:

Spoiler

 

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

main()

Func Main()

    _WD_Option('Driver', @UserProfileDir & '\Documents\chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--log-path=' & @UserProfileDir & '\Documents\chrome.log')
    Local $sDesiredCapabilities = '{"capabilities":{"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["--disable-extensions","--profile-directory=Default"]}}}}'

    _WD_Startup()
    Local $sSession = _WD_CreateSession($sDesiredCapabilities)

    Sleep(50)
    _WD_ConsoleVisible(False)

    _WD_Navigate($sSession, "chrome://settings/content/pdfDocuments")
    Local $oSettingsUISR = _WD_GetShadowRoot($sSession, $_WD_LOCATOR_ByTagName, "settings-ui")
    Local $oTemp = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "#container", $oSettingsUISR)
    Local $oSettingsMainSR = _WD_GetShadowRoot($sSession, $_WD_LOCATOR_ByCSSSelector, "#main", $oTemp)
    Local $oSettingsBasicPageSR = _WD_GetShadowRoot($sSession, $_WD_LOCATOR_ByCSSSelector, "settings-basic-page", $oSettingsMainSR)
    Local $oSettingsPrivacySR = _WD_GetShadowRoot($sSession, $_WD_LOCATOR_ByCSSSelector, "div settings-section settings-privacy-page", $oSettingsBasicPageSR)
    Local $oSettingsPdfSR = _WD_GetShadowRoot($sSession, $_WD_LOCATOR_ByCSSSelector, "settings-animated-pages settings-subpage settings-pdf-documents", $oSettingsPrivacySR)
    Local $oToggleButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "settings-toggle-button", $oSettingsPdfSR)

    _WD_ElementAction($sSession, $oToggleButton, "click")

EndFunc

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

@seadoggie01 Thanks for the reminder to look into this issue. I just tried with the following DesiredCapabilities string at it worked as expected for me --

Local $sDesiredCapabilities = '{"capabilities":{"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "prefs": {"plugins.always_open_pdf_externally": true}}}}}'

 

Link to comment
Share on other sites

  • Jos locked this topic
Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...