Jump to content

Recommended Posts

Posted
  On 2/27/2023 at 5:18 PM, Danp2 said:

[...] was recently added to _WD_ElementActionEx by @SOLVE-SMART

Expand  

You are a very polite person @Danp2 😀 . Thanks for the credits - not necessary, but thanks 🤝 .

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server

  Reveal hidden contents
Posted

I hesitate to even to show my face in this topic since I am far from a programmer, and I basically learn by piecing together code from examples. However, I really would like to be able to automate Chrome, at least to a basic degree. I've been lurking for a while and have learned that the WebDriver udf is far above my pay grade of understanding, yet I keep coming back to it.

OK. here is what I would like to do.

Open Chrome with my own profile, go to any webpage and be able to extract the page's text to use in Regex....over and over again.

The script uses a loop and a middle-click trigger to do so. 

Hopefully, I am not too far out of my league with this. Thanks in advance.

AutoitQ.au3Fetching info...

Posted (edited)

Don't hesitate @fmendi 😀 , the most forum members are very helpful and polite.

Besides that your script is indeed incomplete, but not too bad. Can you please explain a bit further what do you mean by "Open Chrome with my own profile"? I saw your capabilities string with the user-data arg, but are you sure you want to do this?

Why? You described:

  On 2/27/2023 at 8:13 PM, fmendi said:

[...] go to any webpage and be able to extract the page's text [...]

Expand  

So a clean/default chromedriver instance could do the same thing. No need for your profile.

Best regards
Sven

Edited by SOLVE-SMART

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server

  Reveal hidden contents
Posted

Thanks for the quick reply, Sven.

The reason I need to run Chrome under my profile is that the script will eventually be pretty long with auto log- ins to several websites as well as the use of a couple of chrome extensions.  

Below is  where $sText comes up nul.  Thanks....

Func MClick()
   $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "/html/body")
   $sText = _WD_ElementAction($sSession, $sElement, "text")
   MsgBox(1, "",$sText)     ; *******ERROR***********
EndFunc

 

 

Opt("GUIOnEventMode", 1)
Opt("WinTitleMatchMode", 2)
Opt("TrayIconDebug", 1)

#include "wd_core.au3"
#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#Include <_SendEx.au3> ;prevent keys from sticking

dim $dll = DllOpen("user32.dll")
_Singleton(@Scriptname)

;====================================================
Local $sDesiredCapabilities, $sSession
$_WD_DEBUG = $_WD_Debug_Info
$_WD_ERROR_MSGBOX = False
$_WD_DEBUG = $_WD_DEBUG_None  ;Hides console at startup
SetupChrome()

If Not ProcessExists("chromedriver.exe") Then
    _WD_Startup()
EndIf
If @error <> $_WD_ERROR_Success Then
    Exit
 EndIf

; sets up chrome with Google and Yahoo tabs open
WinActivate("Chrome")
WinWaitActive("Chrome", "", 10)
_SendEx("+^m") ;opens my profile in a new Chrome Window
Sleep(200)
_SendEx("{enter}")
_SendEx("^t")  ;opens Yahoo tab
ClipPut("https://yahoo.com")
_SendEx("^v{enter}^1")
WinMove("Chrome", "", @DesktopWidth/15, 0, @DesktopWidth - 125, @DesktopHeight -35); moves chrome a touch away from right side of screen
Sleep(3000)
WinClose("New Tab") ;Closes first chrome window

;==========================================START LOOP========================================
While 1
    $_i1 = MouseGetPos()
    If _IsPressed('04', $dll) Then Call ("MClick"); Middle Click
    If Not WinExists("Chrome") Then exit
    Sleep(10)
 WEnd
;==========================================END LOOP==========================================



;==========================================FUNCTIONS======================================
Func MClick()
   $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "/html/body")
   $sText = _WD_ElementAction($sSession, $sElement, "text")
   MsgBox(1, "",$sText) ;  *******ERROR***********
EndFunc

Func SetupChrome()
    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--verbose --log-path="' & @ScriptDir & '\chrome.log"')
    $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["--user-data-dir=C:\\Users\\DOCMJ\\AppData\\Local\\Google\\Chrome\\User Data\\", "--profile-directory=Default"]}}}}'
    $sSession = _WD_CreateSession($sDesiredCapabilities)
    _WD_Option('DriverParams', '--marionette-port 2828')
EndFunc

 

Posted

I don't recommend sending data to the UDF with sendkeys. I also wouldn't recommend storing passwords in script files for any period of time, though that's a concern for later in the scripting process. When you want to fix that, try taking a look at KeePass. :)

As for how to use this UDF without copy/paste and sendkeys, I'd suggest getting really familiar these functions:
_WD_Navigate - Go to a URL
_WD_FindElement - Find a thing on a page, could be a username input in your case
_WD_ElementAction - Do a thing with that element you found, like setting the value to your username or clicking a button

When you get those figured out, you'll be able to do most of the basic stuff.

Your script is possibly breaking for a few reasons. I'm guessing though that you're not always creating the session since Chrome is already open. I suggest always creating a new instance and closing the old one when your script quits. Managing sessions is a pain IMHO

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

  Reveal hidden contents
Posted

Thanks for the suggestions, seadoggie! 

 

  Quote

Your script is possibly breaking for a few reasons. I'm guessing though that you're not always creating the session since Chrome is already open. I suggest always creating a new instance and closing the old one when your script quits. Managing sessions is a pain IMHO

Expand  

My plan was to start fresh by opening a chrome session under the umbrella of WebDriver and keeping it alive with the WEnd loop. I thought that the problem might be because of closing the original Chrome window  (WinClose("New Tab"), however, commenting that part out does not fix the problem. 

Posted

@Danp2FYI, I have tried to use the method listed in WebDriver Wiki to utilize an existing profile in MS Edge without any success, namely:

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"args": ["user-data-dir=C:\\Users\\' & @UserName & '\\AppData\\Local\\Microsoft\\Edge\\User Data\\", "profile-directory=Default"]}}}}'

This may be helpful for other users who have the same difficulty but after lots of experimentation, I found that the following capabilities string does work to launch MS Edge with an existing profile if you remove the "profile-directory" args and listing the chosen profile, e.g. "Profile 1" in this case:

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"excludeSwitches": [ "enable-automation"], "args": ["user-data-dir=C:\\Users\\' & @UserName & '\\AppData\\Local\\Microsoft\\Edge\\User Data\\Profile 1"]}}}}'

And the corresponding method using @mLipok wd_capabilities.au3

_WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'msedge')
    _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
    _WD_CapabilitiesAdd('args', 'user-data-dir=C:\\Users\\' & @UserName & '\\AppData\\Local\\Microsoft\\Edge\\User Data\\Profile 1')
    $sDesiredCapabilities = _WD_CapabilitiesGet()

If you agree, perhaps the Wiki suggested example could be amended.

Posted

@Danp2OK, but please include an explanation that dividing the profile path into 2 args: "user-data-dir=' and 'profile-directory=' will fail for some like me even when you replace "Default" with a specified profile "Profile 1".

Posted

@CodeWriterThanks for the clarification as I didn't detect that from your earlier post. To clarify, you are saying that this works --

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"args": ["user-data-dir=C:\\Users\\' & @UserName & '\\AppData\\Local\\Microsoft\\Edge\\User Data\\Profile 1"]}}}}'

but this doesn't work?

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"args": ["user-data-dir=C:\\Users\\' & @UserName & '\\AppData\\Local\\Microsoft\\Edge\\User Data\\", "profile-directory=Profile 1"]}}}}'

If that's accurate, then I wonder if the space in the profile name is causing issues. 🤔

Posted (edited)

Yes, the former works (but not with "Default"), and the latter does NOT work with any profile name ("Default" or otherwise). Not sure if the space is a factor because it is still present in the style that works.

Edited by CodeWriter
Posted

Hi Dan,

could you be so kind and declare    $sDriverCurrent and  $sDriverLatest from _WD_UpdateDriver() as global in the next release? I need these vars in my program.

Thanks a lot

Hans

Posted (edited)

Hi @HJL,

I think this isn't necessary. You can get the WebDriver version like in wd_helper.au3 by:

Global $sCurrentWebDriverVersion = _WD_GetWebDriverVersion($sInstallDir, $sDriverEXE)

Also for the latest WebDriver info, you can use this:

Global $sLatestWebDriverInfo = __WD_GetLatestWebdriverInfo($aBrowser, $sBrowserVersion, $bFlag64)[1]

Best regards
Sven

Edited by SOLVE-SMART

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server

  Reveal hidden contents
Posted (edited)
  On 3/2/2023 at 8:32 PM, Danp2 said:

@HJLThese are local variables to _WD_UpdateDriver, and they should remain local IMO. However, there are functions that allow you to retrieve these values yourself.

Expand  

If I would know how to do it I wouldn't ask 😉 But the solution is one thread above 😉

 

 

Edited by HJL
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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