Jump to content

WebDriver UDF - Help & Support (III)


Danp2
 Share

Recommended Posts

Hi @Virgus,

IMO you are headed down the wrong path for several reasons --

  • You should always try to stay a current as possible with your software for security and compatibility reasons
  • You will likely have a difficult time obtaining the correct variations for a working combination of browser, webdriver, UDF, etc
  • You may find it difficult to get support for this setup

You seem to be fixated on storage space. "Hundreds of MBs" is nothing with today's storage.  So why is this a problem?

Dan

Link to comment
Share on other sites

Thank you Danp2 for your prompt reply.

I totally understand your points but, just to give you an idea of my actual view, I was sticking with windows XP until a few months ago.

Now I'm using Win7 mainly and run XP for a few specific purposes. I've an almost 90% portable setup and for portabilty the smallest is the better.

Just to give you an example: I often use Photoshop and my favourite portable version is 7.0 (if I'm not wrong it dates 2002) which is 57 Mb and starts in seconds even on old PCs. Why should I install a 1-2 GB software if all I need is old, small and fast ? Smallest sizes are also useful for faster backup purposes and if the backup occurs via LAN it makes you save time.

Besides all this, while looking at different geckodriver versions the oldest ones are bigger in size than the newest ones. I guess is one of the few exceptions, as everything is growing bigger in size, complexity and lack of ergonomics and friendliness unfortunately.

V.

Link to comment
Share on other sites

Danp2,

I wanted to write and document an unusual behavior of _WD_NewTab(). When I use it in a script without the options, it creates a new tab (not a separate window) where the control elements can be found with _WD_FindElement() as expected. This works fine.

However, when I create any tab using $sfeatures options such as:

_WD_NewTab($sSession, Default, Default, $URL, “toolbar=yes, width=1000, height=1500”)

it creates a new window with the $URL but the window status bar cannot be changed manually navigate to a new website unlike other normal tabs. This window also lacks a toolbar with or without the toolbar option.  This is true with FireFox, Chrome, and MS Edge.

Furthermore in Firefox, none of the control elements on this window page can be located even though these could be found opening the same site without options as a tab.

Is there a method to use the options argument and yet return a new window that behaves like any other tab? Lastly, the new WebDriver.chm documentation is greatly appreciated but the page for _WD_NewTab() says “For list of $sfeatures take a look in the following link” and there is no link. Thanks in advance.

Link to comment
Share on other sites

@CodeWriter_WD_NewTab uses the javascript command window.open when you supply the optional $sFeatures parameter. I will have to perform some tests to investigate your claims. In the meantime, you could try creating the new window like this --

$sTabHandle = _WD_Window($sSession, 'new', '{"type":"window"}')

 

Link to comment
Share on other sites

_WD_Window() has its own unusual behavior: with > 6 open tabs, when you call _WD_Window() to make a new window as you describe, as soon as the new window is created, it is hidden and NOT the active window. Instead, one of the previous tabs (not the most recent before the function call) is brought to the front as the visible and active window handle.

Link to comment
Share on other sites

Some background on this test code to replicate my situation: I have multiple copies of a webpage open and we'll use the www.nytimes.com for that. For each instance, I only save the most recently created handle as my working page. There is also an ad lib function checking in the background if the viewing active page is the NYT. If it is not the NYT, _WD_Attach() is called to activate the most recent NYT page.

When you run the code, after the last window (macrumors.com) is created and minimized, the code should attach only to the right most NYT page. Instead, you see the tabs "flickering" as it attaches to the 1st tab (washingtonpost.com in this case) and the right most NYT page, at least on my runtime environment. It's not a problem with _WD_Window() directly but some memory management of the handles may be corrupted by the ad lib function calls.

#include "wd_helper.au3"
#include "wd_capabilities.au3"

Global $GsDesiredCapabilities
Global $GsSession
$_WD_DEBUG = $_WD_DEBUG_None
SetUpFireFox()
_WD_Startup()

$GsSession = _WD_CreateSession($GsDesiredCapabilities)
_WD_Navigate($GsSession, "https://www.washingtonpost.com")
_WD_LoadWait($GsSession, 1000, 2000)

Global $nytHandle = _WD_NewTab($GsSession, True, Default, "https://www.nytimes.com")
_WD_LoadWait($GsSession, 1000, 2000)

$nytHandle = _WD_NewTab($GsSession, True, Default, "https://www.nytimes.com")
_WD_LoadWait($GsSession, 1000, 2000)

AdlibRegister("checkNYT", 1000)

$Handle = _WD_Window($GsSession, 'new', '{"type":"window"}')
_WD_Window($GsSession, 'Switch', '{"handle":"' & $Handle & '"}')
_WD_Navigate($GsSession, 'https://www.macrumors.com')
WinSetState("MacRumors: ", '',  @SW_MINIMIZE)

Sleep(10000)

_WD_DeleteSession($GsSession)
_WD_Shutdown()

; The "asynchronous" execution of this causes the issue with _WD_Window()
Func checkNYT()
    Local $url = _WD_Action($GsSession, 'URL')
    If StringInStr($url, 'nytimes') > 0 Then
        ConsoleWrite("Reading New York Times" & @CR)
    Else
        ; This should attach to the most "recent" NYT page.
        _WD_Attach($GsSession, $nytHandle)
    EndIf
EndFunc

Func SetupFireFox()
    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('DriverParams', '--marionette-port 2828')
    _WD_Option('Port', 4444)

    $GsDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"moz:firefoxOptions": {"args": ["-profile", "' & getDefaultFFProfile() & '"]}}}}'
EndFunc   ;==>SetupFireFox

Func getDefaultFFProfile()
    Local $sDefault, $sProfilePath = ''

    Local $sProfilesPath = StringReplace(@AppDataDir, '\', '/') & "/Mozilla/Firefox/"
    Local $sFilename = $sProfilesPath & "profiles.ini"
    Local $aSections = IniReadSectionNames($sFilename)

    If Not @error Then
        For $i = 1 To $aSections[0]
            $sDefault = IniRead($sFilename, $aSections[$i], 'Default', '0')

            If $sDefault = '1' Then
                $sProfilePath = $sProfilesPath & IniRead($sFilename, $aSections[$i], "Path", "")
                ExitLoop
            EndIf
        Next
    EndIf
    Return $sProfilePath
EndFunc   ;==>getDefaultFFProfile

 

 

Link to comment
Share on other sites

5 hours ago, CodeWriter said:

_WD_Window() has its own unusual behavior: with > 6 open tabs, when you call _WD_Window() to make a new window as you describe, as soon as the new window is created, it is hidden and NOT the active window.

The code you posted doesn't doesn't demonstrate the issue you described. 🤨

Quote

the code should attach only to the right most NYT page. Instead, you see the tabs "flickering" as it attaches to the 1st tab (washingtonpost.com in this case) and the right most NYT page, at least on my runtime environment.

This is because you are using _WD_Attach, which is the wrong function in this scenario. _WD_Attach cycles through all the tabs / windows until it finds a match based upon title, URL, or html. The default mode is to search by title, so your are a handle to the function and asking it to find a matching title.

You can resolve the issue by making this one change --

;~ _WD_Attach($GsSession, $nytHandle)
        _WD_Window($GsSession, 'Switch', '{"handle":"' & $nytHandle & '"}')

P.S. This issue would have been much easier to debug on your own if you hadn't set $_WD_DEBUG = $_WD_DEBUG_None

Link to comment
Share on other sites

43 minutes ago, Danp2 said:

The code you posted doesn't doesn't demonstrate the issue you described. 🤨

This is because you are using _WD_Attach, which is the wrong function in this scenario. _WD_Attach cycles through all the tabs / windows until it finds a match based upon title, URL, or html. The default mode is to search by title, so your are a handle to the function and asking it to find a matching title.

You can resolve the issue by making this one change --

;~ _WD_Attach($GsSession, $nytHandle)
        _WD_Window($GsSession, 'Switch', '{"handle":"' & $nytHandle & '"}')

P.S. This issue would have been much easier to debug on your own if you hadn't set $_WD_DEBUG = $_WD_DEBUG_None

Thanks, my main reason for posting was the unusual window that _WD_NewTab() created when using options: A window without a toolbar or any way of manually changing the website in the status bar.

This issue with _WD_Windows() was a secondary odd behavior that I noticed usually when I had more than 6 tabs open. Thought it was related to that but when I created that test code, I managed to narrow it down to the interaction with the adlib functions. I didn’t realize that _WD_Attach() was cycling through all the tabs. Thanks for explaining that.

The debug flag may be helpful, but in all honesty it’s so verbose that whenever I’ve tried to look at it, it’s been like trying to find a needle in a haystack and I’m not familiar with the output so I don’t even know what the needle looks like…

Link to comment
Share on other sites

1 hour ago, CodeWriter said:

The debug flag may be helpful, but in all honesty it’s so verbose that whenever I’ve tried to look at it, it’s been like trying to find a needle in a haystack and I’m not familiar with the output so I don’t even know what the needle looks like…

Please take a look on: https://github.com/Danp2/au3WebDriver/pull/290

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

On 4/10/2022 at 7:23 AM, CodeWriter said:

it creates a new window with the $URL but the window status bar cannot be changed manually navigate to a new website unlike other normal tabs. This window also lacks a toolbar with or without the toolbar option.  This is true with FireFox, Chrome, and MS Edge.

I tested this, but was unable to reproduce (unless I'm misunderstanding the issue).

Here's the modified version of your script I used to test --

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

Global $GsDesiredCapabilities
Global $GsSession
$_WD_DEBUG = $_WD_DEBUG_Error
SetUpFireFox()
_WD_Startup()

$GsSession = _WD_CreateSession($GsDesiredCapabilities)
_WD_Navigate($GsSession, "https://www.washingtonpost.com")
_WD_LoadWait($GsSession, 1000, 2000)

Global $nytHandle = _WD_NewTab($GsSession, True, Default, "https://www.nytimes.com")
_WD_LoadWait($GsSession, 1000, 2000)

$nytHandle = _WD_NewTab($GsSession, True, Default, "https://www.nytimes.com")
_WD_LoadWait($GsSession, 1000, 2000)

;~ AdlibRegister("checkNYT", 1000)

;~ $Handle = _WD_Window($GsSession, 'new', '{"type":"window"}')
$Handle = _WD_NewTab($GsSession, Default, Default, Default, "toolbar=yes, width=1000, height=1500")
Sleep(2000)
_WD_Window($GsSession, 'Switch', '{"handle":"' & $Handle & '"}')
_WD_Navigate($GsSession, 'https://www.macrumors.com')
;~ WinSetState("MacRumors: ", '',  @SW_MINIMIZE)

Sleep(2000)

_WD_DeleteSession($GsSession)
_WD_Shutdown()

; The "asynchronous" execution of this causes the issue with _WD_Window()
Func checkNYT()
    Local $url = _WD_Action($GsSession, 'URL')
    If StringInStr($url, 'nytimes') > 0 Then
        ConsoleWrite("Reading New York Times" & @CR)
    Else
        ; This should attach to the most "recent" NYT page.
        ;~ _WD_Attach($GsSession, $nytHandle)
        ConsoleWrite("Switching to New York Times" & @CR)

        _WD_Window($GsSession, 'Switch', '{"handle":"' & $nytHandle & '"}')

    EndIf
EndFunc

Func SetupFireFox()
    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('DriverParams', '--marionette-port 2828')
    _WD_Option('Port', 4444)

    $GsDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"moz:firefoxOptions": {"args": ["-profile", "' & getDefaultFFProfile() & '"]}}}}'
EndFunc   ;==>SetupFireFox

Func getDefaultFFProfile()
    Local $sDefault, $sProfilePath = ''

    Local $sProfilesPath = StringReplace(@AppDataDir, '\', '/') & "/Mozilla/Firefox/"
    Local $sFilename = $sProfilesPath & "profiles.ini"
    Local $aSections = IniReadSectionNames($sFilename)

    If Not @error Then
        For $i = 1 To $aSections[0]
            $sDefault = IniRead($sFilename, $aSections[$i], 'Default', '0')

            If $sDefault = '1' Then
                $sProfilePath = $sProfilesPath & IniRead($sFilename, $aSections[$i], "Path", "")
                ExitLoop
            EndIf
        Next
    EndIf
    Return $sProfilePath
EndFunc   ;==>getDefaultFFProfile

 

Link to comment
Share on other sites

That script was not to demonstrate the issue with _WD_NewTab() when called with options in my original post. Try this example, look carefully at the 2nd window created by _WD_NewTab()---it has no toolbar and you cannot edit the status bar to change the website:

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

Global $GsDesiredCapabilities
Global $GsSession
$_WD_DEBUG = $_WD_DEBUG_Error
SetUpFireFox()
_WD_Startup()

$GsSession = _WD_CreateSession($GsDesiredCapabilities)
_WD_Navigate($GsSession, "https://www.washingtonpost.com")
_WD_LoadWait($GsSession, 1000, 2000)
_WD_NewTab($GsSession, Default, Default, "https://www.nytimes.com", "toolbar=yes, width=1000, height=1500")
Sleep(30000)

_WD_DeleteSession($GsSession)
_WD_Shutdown()

Func SetupFireFox()
    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('DriverParams', '--marionette-port 2828')
    _WD_Option('Port', 4444)

    $GsDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"moz:firefoxOptions": {"args": ["-profile", "' & getDefaultFFProfile() & '"]}}}}'
EndFunc   ;==>SetupFireFox

Func getDefaultFFProfile()
    Local $sDefault, $sProfilePath = ''

    Local $sProfilesPath = StringReplace(@AppDataDir, '\', '/') & "/Mozilla/Firefox/"
    Local $sFilename = $sProfilesPath & "profiles.ini"
    Local $aSections = IniReadSectionNames($sFilename)

    If Not @error Then
        For $i = 1 To $aSections[0]
            $sDefault = IniRead($sFilename, $aSections[$i], 'Default', '0')

            If $sDefault = '1' Then
                $sProfilePath = $sProfilesPath & IniRead($sFilename, $aSections[$i], "Path", "")
                ExitLoop
            EndIf
        Next
    EndIf
    Return $sProfilePath
EndFunc   ;==>getDefaultFFProfile

And in FireFox, the control elements on that second page can no longer be found with _WD_FindElement(), unlike when the page is created with _WD_NewTab() without options.

Edited by CodeWriter
Didn't include code.
Link to comment
Share on other sites

@CodeWriterThanks for posting the code to demonstrate the issue. FYI, if you review the modified example I previously posted, you should see that it also shows the issue.

If you review the link posted in the header of _WD_NewTab, you'll find the following information --

Quote
windowFeatures Optional

A string containing a comma-separated list of window features in the form name=value — or for boolean features, just name. These features include options such as the window's default size and position, whether or not to open a minimal popup window, and so forth. The following options are supported:

popup

If this feature is enabled, it requests that a minimal popup window be used. The UI features included in the popup window will be automatically decided by the browser, generally including an address bar only.

If popup is not enabled, and there are no window features declared, the new browsing context will be a tab.

Note: Specifying any features in the windowFeatures parameter, other than noopener or noreferrer, also has the effect of requesting a popup.

So, it appears that you are getting a "popup" window, which exhibits the restrictions that you are encountering. Thanks for pointing this out, and I'll add a note about this to the function header.

You should be able to create a new window and resize it using _WD_Window. 🙂

Link to comment
Share on other sites

Would anyone know how to disable then later re-enable the right click (context) menu in Chrome of the current webpage:

I tried this which does disable the context menu

_WD_ExecuteScript($sSession, "document.addEventListener('contextmenu', event => event.preventDefault());")

but I can't get it re-enabled again without refreshing the page

Thanks for all suggestions

Edited by NassauSky
Link to comment
Share on other sites

Hi,

I have been using Webdriver a lot with Firefox and i'm trying to use it with Edge.

I want to open a WebSite that need IE mode to be opened but i can't find a way to get Edge on IE mode using WebDriver with Autoit.

Here is my code:

_WD_Option('Driver', @ScriptDir & '\include\' & (@Compiled ? '' : 'Exe_externe\') & 'msedgedriver.exe')
_WD_Option('DriverParams', '--log trace ')
_WD_Option('Port', 9515)
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"args": ["--ie-mode-force"]}}}}'
_WD_Startup()

Edge doesn't open on IE mode and i get an info bar showing on the Edge browser, saying  "Turn off remote debugging to open this site in internet explorer mode. Otherwise, it might not work as expected."

Is remote debbuging the real issue ? If so how could i turn it off using WebDriver with autoit script ?

Link to comment
Share on other sites

Thanks a lot for your answer @Danp2 !

IEDriver might be the driver to use to fix my problem but sadly, i can't find any example or documentation, showing this driver used into an Autoit script. Changing msgdgedriver by IEDriver in my code, doesn't seems enought to make it work.

Link to comment
Share on other sites

  • Danp2 changed the title to WebDriver UDF (W3C compliant version) - 07/29/2022
  • 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...