Jump to content

WebDriver UDF (W3C compliant version) - 2024/02/19


Danp2
 Share

Recommended Posts

2 hours ago, grzesiek said:

@horphi

Try this:


_WD_ExecuteScript($sSession, 'document.getElementsByTagName('p')[1].select();')

I tried this already...works unfortunately not...:-(

 

_WD_ExecuteScript: {"value":{"error":"javascript error","message":"TypeError: document.getElementsByTagName(...)[1].select 
 --> is not a function","stacktrace":"execute_script @, line 0\ninline javascript, line 1\nsrc: \"undefined\"\nStack:\n

 

Link to comment
Share on other sites

Maybe some minor hint to _WD_NewTab

A error occurs if no browser is opend and the script interrupt.

Func _WD_NewTab($sSession, $lSwitch = True)
    Local Const $sFuncName = "_WD_NewTab"
    Local $sTabHandle = ''

    _WD_ExecuteScript($sSession, 'window.open()', '{}')

    If @error = $_WD_ERROR_Success Then
        Local $aHandles = _WD_Window($sSession, 'handles', '')
        
        $sTabHandle = $aHandles[UBound($aHandles) - 1] ;<<<<<<<<<<<<<<<<<<<<<<If this is no array, script interrupt

        If $lSwitch Then
            _WD_Window($sSession, 'Switch', '{"handle":"' & $sTabHandle & '"}')
        EndIf
    EndIf

    Return SetError($_WD_ERROR_Success, 0, $sTabHandle)
EndFunc


Func _WD_NewTab($sSession, $lSwitch = True)
    Local Const $sFuncName = "_WD_NewTab"
    Local $sTabHandle = ''

    _WD_ExecuteScript($sSession, 'window.open()', '{}')

    If @error = $_WD_ERROR_Success Then
        Local $aHandles = _WD_Window($sSession, 'handles', '')
          If IsArray($aHandles) Then
        $sTabHandle = $aHandles[UBound($aHandles) - 1]
          Else
            ConsoleWrite("> An error occurred. " & 'No Session exist.' & @CRLF)
        EndIf

        If $lSwitch Then
            _WD_Window($sSession, 'Switch', '{"handle":"' & $sTabHandle & '"}')
        EndIf
    EndIf

    Return SetError($_WD_ERROR_Success, 0, $sTabHandle)
EndFunc

 

Link to comment
Share on other sites

@horphi Probably need to check the value of @error after the call to _WD_Window. I'll have to do some testing, but would have thought that the earlier call to _WD_ExecuteScript would have failed.

Edit: Fixed _WD_ExecuteScript so that it properly sets @error

Edited by Danp2
Link to comment
Share on other sites

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • 3 weeks later...

Hello. Tested with  Microsoft WebDriver (version 16299).

 

Here the Setup Function

Func SetupEdge()
_WD_Option('Driver', 'MicrosoftWebDriver-16299.exe')
_WD_Option('Port', 17556)
EndFunc

_WD_CreateSession need to be modified becuase the sessionid is not returned here [value][sessionId] It's retuned here [sessionId] That's all

I tested just few commands navigate,FindElement,Action But I think all will work correctly.

 

Saludos

 

Link to comment
Share on other sites

  • 2 weeks later...

the handle do not return from the new tab, because this tab does not have time to create

Fixed _WD_NewTab

Func _WD_NewTab($sSession, $lSwitch = True)
    Local Const $sFuncName = "_WD_NewTab"
    Local $sTabHandle = ''
    Local $TimerNewTab = 0
    _WD_ExecuteScript($sSession, 'window.open()', '{}')
    $TimerNewTab = TimerInit()

    While 1
        If TimerDiff($TimerNewTab) > 5000 Then Return SetError($_WD_ERROR_Success, 0, $sTabHandle)
        If @error = $_WD_ERROR_Success Then
            Local $aHandles = _WD_Window($sSession, 'handles', '')
            $sTabHandle = $aHandles[UBound($aHandles) - 1]
            If $lSwitch Then
                _WD_Window($sSession, 'Switch', '{"handle":"' & $sTabHandle & '"}')
            EndIf
            ExitLoop
        EndIf
        Sleep(10)
    WEnd
    Return SetError($_WD_ERROR_Success, 0, $sTabHandle)
EndFunc   ;==>_WD_NewTab

Now, if the tab is not created within 5 seconds, it returns an SetError

Edited by alexmerfi
Link to comment
Share on other sites

7 hours ago, alexmerfi said:

the handle do not return from the new tab, because this tab does not have time to create

Thanks for the feedback. Can you provide further details so that I can replicate the issue? Which browser, etc.

Also, I can see a few issues with this revised implementation, such as --

  • Possible misplaced If @error check.
  • Wrong return value when timeout occurs
  • How do you determine that the new tab exists?

Finally, I would suggest making the timer "max" value an optional parameter.

P.S. Perhaps an even simpler implementation would be to replace the timer loop with a single Sleep command using the same optional variable to allow the calling routine to determine the desired delay length.

Link to comment
Share on other sites

On 29.06.2018 at 5:06 PM, Danp2 said:

Thanks for the feedback. Can you provide further details so that I can replicate the issue? Which browser, etc.

Firefox 61.0 64 bit, Hyperv Windows 10 x32

Autoit 3.3.14.2

#include "wd_core.au3"
#include "wd_helper.au3"
Global $sSession
Global $sDesiredCapabilities

$_WD_DEBUG = False

SetupGecko()
_WD_Startup()


$sSession = _WD_CreateSession($sDesiredCapabilities)

$HandleTab = _WD_NewTab($sSession, True)
MsgBox(0, "", $HandleTab)


_WD_DeleteSession($sSession)
_WD_Shutdown()
Exit



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


 

Link to comment
Share on other sites

On 29.06.2018 at 5:06 PM, Danp2 said:

Possible misplaced If @error check.

Yes, you're right, I have not yet found how to fix this inside this function. Perhaps, if the _WD_ExecuteScript will store errors in the global variable, and I will check it through

While 1

if $GlobalVariableOfErrorExecuteScript='okay'  then exitloop

wend

 

Link to comment
Share on other sites

On 29.06.2018 at 5:06 PM, Danp2 said:

P.S. Perhaps an even simpler implementation would be to replace the timer loop with a single Sleep command using the same optional variable to allow the calling routine to determine the desired delay length.

Yes, this is a simple implementation. But, for example, if you have a heavily loaded virtual machine and the fiefox tab will be created for a very long time, it will not always work sleep. Of course SLEEP can be done for 5 minutes :) but in some tasks some people can lose a lot of money.

 

Link to comment
Share on other sites

1 hour ago, alexmerfi said:

Firefox 61.0 64 bit, Hyperv Windows 10 x32

Autoit 3.3.14.2

Does it fail for you every time in this environment? If so, please change $_WD_DEBUG = True and post the contents of the Scite output window after running your code.

Also, is there a reason you aren't running the latest version of Autoit?

1 hour ago, alexmerfi said:
If $HandleTab<>'' then MsgBox(0,"","Table IS EXIST, because it return value of handle")
 _WD_Window($sSession, 'Switch', '{"handle":"' & $HandleTab & '"}')
EndIf

But the current implementation should return the handle of the last tab (even if the newest one hasn't appeared as you previously indicated). So $HandleTab would only be blank if there was an error from _WD_ExecuteScript.

 

1 hour ago, alexmerfi said:

Why Wrong? 

if i change timeout 5000 to 0

If TimerDiff($TimerNewTab) > 0 Then Return SetError($_WD_ERROR_Success, 0, $sTabHandle)

i return $HandleTab='' (nothing)

Actually, I meant the error code, not the return value. I would use $_WD_ERROR_Timeout here instead of $_WD_ERROR_Success

59 minutes ago, alexmerfi said:

Yes, you're right, I have not yet found how to fix this inside this function. Perhaps, if the _WD_ExecuteScript will store errors in the global variable, and I will check it through

Shouldn't be necessary to use a global here. Look at other instance where the value of @error is saved to $iErr.

Link to comment
Share on other sites

  • Danp2 changed the title to WebDriver UDF (W3C compliant version) - 2024/02/19
  • Melba23 pinned this topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...