Jump to content
Danp2

WebDriver UDF - Help & Support

Recommended Posts

50 minutes ago, Letraindusoir said:

Are there any actual examples where '$sStartElement' is not empty? I don't quite understand when $sStartElement is used

It's used when you want to perform an element search starting at a particular element. For example, if you want to get all the rows from a table, first you retrieve the element ID for the table. Then you use this as the starting point for your subsequent find operation, like this --

$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "/html/body/table")
$aRows = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//tbody/tr", $sElement, True)

 

Share this post


Link to post
Share on other sites
37 minutes ago, Danp2 said:

It's used when you want to perform an element search starting at a particular element. For example, if you want to get all the rows from a table, first you retrieve the element ID for the table. Then you use this as the starting point for your subsequent find operation, like this --

$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "/html/body/table")
$aRows = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//tbody/tr", $sElement, True)

 

Thank you, Dan. I basically understand....

Share this post


Link to post
Share on other sites
Posted (edited)

To stay logged in after first time logged in I'm saving a local profile for the future reuse. My code is as follows:

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["start-maximized", "disable-infobars","--user-data-dir=' & StringReplace(@ScriptDir, Chr(92), Chr(92) & Chr(92)) & '\\Profile 1"] }}}}'

So when I run script first time, I do some checks and then log in if needed (I have to do that manually first time, it's can't be automated). After that I can easily end/start my script and authentication is there ('coz of cookies). But my problem is error suggesting to restore my last session tabs.

Restore pages?
Chrome didn't shut down correctly.
[Restore]

How can I avoid this error?

I'm using this code on exit:

Func _QuitChrome()
    _WD_Window($sSession, 'Close')
    _WD_DeleteSession($sSession)
EndFunc

Func _Exit()
    _QuitChrome()
    _WD_Shutdown()
    Exit
EndFunc

UPD: I guess --disable-features=InfiniteSessionRestore should do the trick (will test it later)?..

Edited by MONaH-Rasta
Found possible sollution

Share this post


Link to post
Share on other sites

So... After some research and bunch of tests I finally have a solution to get rid of this annoying popup.

I'm using chrome version 74.0.3729.169 (current) latest stable.

Here is a code of function that will do the trick.

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["start-maximized", "disable-infobars", "--user-data-dir=' & StringReplace(@ScriptDir, Chr(92), Chr(92) & Chr(92)) & '\\Profile 1"] }}}}'

    _SetPreferences()
    _WD_Startup()

Func _SetPreferences()
Local $sFileName, $sFind, $sReplace, $iRetval
    $sFileName = @ScriptDir & Chr(92) & 'Profile 1' & Chr(92) & 'Local State'
    $sFind = '"exited_cleanly":false'
    $sReplace = '"exited_cleanly":true'

    $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)
    If $iRetval = -1 Then
        If TrayItemGetState($iShowNotify) = 65 Then _TrayTip('File ' & Chr(92) & 'Local State' & Chr(92) & ' was not edited! Error = ' & @error)
    EndIf

    $sFileName = @ScriptDir & Chr(92) & 'Profile 1' & Chr(92) & 'Default' & Chr(92) & 'Preferences'
    $sFind = '"exit_type":"Crashed"'
    $sReplace = '"exit_type":"None"'

    $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)
    If $iRetval = -1 Then
        If TrayItemGetState($iShowNotify) = 65 Then _TrayTip('File ' & Chr(92) & 'Local State' & Chr(92) & ' was not edited! Error = ' & @error)
    EndIf
EndFunc

If you will have those 2 options set like this before start new session, chrome will start normally regardless of was he actually crashed or not. For some reason in most cases webdriver causing chrome set those 2 options to values like after "crash", and of course my solution is not "correct", but at least I have no popups now 😎

Share this post


Link to post
Share on other sites

@Letraindusoir If you look at the underlying code for _WD_Startup, you'll see the Run command that launches the console app. The setting of $_WD_DEBUG controls whether the console is launched hidden or visible. So you have two options --

  • Set the value of $_WD_DEBUG to something other than $_WD_DEBUG_Info before calling _WD_Startup
  • Use the function _WD_ConsoleVisible to change the console's visibility

Share this post


Link to post
Share on other sites

Hi @Danp2, Thank you for the UDF. It works great !! I was trying to change the default download folder of chrome. I need to map a network drive for downloads everytime session is initiated. Could you please help.

Share this post


Link to post
Share on other sites

I am running a basic script which picks up URL, Username and Password as command line parameters and initiates a web session on a RDS server as a remote application. But the session is always initiated with default Chrome settings and maps default downloads folder to the session. So I want to map the network drive as download location every time script is executed. I am really new to AutoIT so any help is much appreciated. I am really new to AutoIT so any suggestions are welcome. The code looks like:

Local $sDesiredCapabilities, $sSession

SetupChrome()

_WD_Startup()


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

If $CmdLine[0] <> 3 Then
   MsgBox($MB_OK, "Usage", "ps_palo <hostname> <username> <password>")
Else
   palo_login($CmdLine[1],$CmdLine[2],$CmdLine[3])
EndIf

Func palo_login($url,$username,$password)

   Local $string, $sButton
   $sSession = _WD_CreateSession($sDesiredCapabilities)
   $string= "https://" & $url
   _WD_Navigate($sSession,$string)

   ;Sleep(5000)
   ;_WD_LinkClickByText($sSession,"Continue to this website (not recommended).")


   _ChromeSetInputValueById($sSession,"user",$username)
   Sleep(1000)
   _ChromeSetInputValueById($sSession,"passwd",$password)
   Sleep(1000)
   $sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='submit']")
   _WD_ElementAction($sSession, $sButton, 'click')

EndFunc ;==>PaloAlto_login()


Func _ChromeSetInputValueById($sSession,$Id,$Value)
   Local $sElement
   $sElement=_WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@name='"&$Id&"']")
   _WD_ElementAction($sSession,$sElement,'value',$value)
EndFunc

;_WD_DeleteSession($sSession)
;_WD_Shutdown()

Share this post


Link to post
Share on other sites
Posted (edited)
21 hours ago, Danp2 said:

@Letraindusoir If you look at the underlying code for _WD_Startup, you'll see the Run command that launches the console app. The setting of $_WD_DEBUG controls whether the console is launched hidden or visible. So you have two options --

  • Set the value of $_WD_DEBUG to something other than $_WD_DEBUG_Info before calling _WD_Startup
  • Use the function _WD_ConsoleVisible to change the console's visibility

Thank You,Dan.I See.I didn't read the UDF carefully and clearly enough.

Edited by Letraindusoir

Share this post


Link to post
Share on other sites
Posted (edited)

How do I know I got multiple results from "_WD_FindElement", does it return an array (tried)?

$sList = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@class='listing-content']")
_WD_ElementAction($sSession, $sList, 'text')

In this example I know there are multiple classname "listing-content" but only see text from the first one.

Edited by Arlen

Share this post


Link to post
Share on other sites

Recently, I was thinking about a question: how can I move the mouse to the location of the element after getting an Element?

As shown like below:

$sElem = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "Xpath_sElem")
_WD_MoveMouseTo($sSession,$sElem)
or
_WD_SetFocus($sSession,$sElem)

 

Share this post


Link to post
Share on other sites
Posted (edited)
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

20180610155716143.png.8b4f1e61258abad77cb72299c62efbf5.png

As shown above, can this modal box pop-up window be controlled by a custom UDF similar to _ WD_Alert?

Edited by Letraindusoir

Share this post


Link to post
Share on other sites
Posted (edited)
1 hour ago, Danp2 said:

@Letraindusoir You can always write a custom function to serve a particular purpose. Have you tried using the standard webdriver UDF functions to interact with this dialog?

Yes, using standard functions and specific xpath,can interact with a  specific Modal_dialog_box. However, since the Modal_dialog_box is a characteristic pop-up interface, it may be more convenient and practical to encapsulate the operations For Modal_dialog_box into UDF, like _WD_Alert, can identify any Alert Pop-up.

Edited by Letraindusoir

Share this post


Link to post
Share on other sites

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

  • Similar Content

    • By Danp2
      Introduction
      This UDF will allow you to interact with any browser that supports the W3C WebDriver specifications. Supporting multiple browsers via the same code base is now possible with just a few configuration settings.
      Requirements
      JSON UDF https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn
      WinHTTP UDF https://www.autoitscript.com/forum/topic/84133-winhttp-functions/
      WebDriver for desired browser
      Chrome WebDriver https://sites.google.com/a/chromium.org/chromedriver/downloads FireFox WebDriver https://github.com/mozilla/geckodriver/releases Function List
      Core Functions
      _WD_CreateSession($sDesiredCapabilities='{}') _WD_DeleteSession($sSession) _WD_Status() _WD_Timeouts($sSession, $sTimeouts = '') _WD_Navigate($sSession, $sURL) _WD_Action($sSession, $sCommand) _WD_Window($sSession, $sCommand, $sOption = '') _WD_FindElement($sSession, $sStrategy, $sSelector, $sStartElement = "", $lMultiple = False) _WD_ElementAction($sSession, $sElement, $sCommand, $sOption='') _WD_ExecuteScript($sSession, $sScript, $sArguments="[]") _WD_Alert($sSession, $sCommand, $sOption = '') _WD_GetSource($sSession) _WD_Cookies($sSession, $sCommand, $sOption = '') _WD_Option($sOption, $vValue = "") _WD_Startup() _WD_Shutdown() Helper Functions
      _WD_NewTab($sSession, $lSwitch = True, $iTimeout = -1, $sURL = "", $sFeatures = "") _WD_Attach($sSession, $sString, $sMode = 'title') _WD_LinkClickByText($sSession, $sText, $lPartial = True) _WD_WaitElement($sSession, $sElement, $sStrategy, $sSelector[, $iDelay = 0[, $iTimeout = -1]]) _WD_GetMouseElement($sSession) _WD_GetElementFromPoint($sSession, $iX, $iY) _WD_LastHTTPResult() _WD_GetFrameCount() _WD_IsWindowTop() _WD_FrameEnter($sIndexOrID) _WD_FrameLeave() _WD_HighlightElement($sSession, $sElement[, $iMethod = 1]) _WD_HighlightElements($sSession, $aElements[, $iMethod = 1]) _WD_jQuerify($sSession) WD_ElementOptionSelect($sSession, $sStrategy, $sSelector, $sStartElement = "") _WD_ConsoleVisible($lVisible = False) Source Code
      You will always be able to find the latest version in the GitHub Repo 
      Help / Support
      Please post any questions, suggestions or errors in the GH&S thread.
      https://www.autoitscript.com/forum/topic/192730-webdriver-udf-help-support/
    • By Davidowicza
      Hey guys,
      I am creating my very first script that automates a browser (I usually work with program installs and database automation) and have come across an issue that I am totally stumped on. I need to click an element that gives me a drop down list box but the Xpath to the element changes with every instance of chrome I start...
      I have tried selecting the class, rect, and path but no luck. It never finds the element. (I could be doing this wrong since I am not good at HTML)
      Element I need to select:
      This is the Xpath for the last 3 instances of chrome I have run the script with:
      //*[@id="highcharts-5bp9crq-8"]/svg/g[6]/g/rect //*[@id="highcharts-fiw9szv-8"]/svg/g[6]/g/rect //*[@id="highcharts-5szkmx8-8"]/svg/g[6]/g/rect As you can see the path changes every time.
      How I am trying to select the element:
      ;Check for box element _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//*[@id='highcharts-5bp9crq-8']/svg/g[6]/g") MsgBox(0, "", "check for timeout") $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[@id='highcharts-5bp9crq-8']/svg/g[6]/g") _WD_ElementAction($sSession, $sElement, 'click') Maybe someone has come across this before and found a work around without using mouseclick()
      Thanks guys, hopefully I am just very overlooking something simple and can be pointed to the right solution.
      Side Note: I wish I could share the webpage entirety, but it has sensitive information with my agency that I cannot share. If you need more, just let me know and I will try and post as much as I can.
    • By daluu
      This is kind of already posted here:
      '?do=embed' frameborder='0' data-embedContent>>
      but wanted to note this in this forum of example scripts in case some people only look in this forum for examples and not the other forums with respect to AutoIt integration or relation to Selenium WebDriver.
      So, the example in summary is: you can use Selenium WebDriver API to do AutoIt GUI automation (the ControlXyz() and WinXyz() functons, along with mouse and "send keys" functions). Although I suppose that might be opposite or counter to what most examples here are about - using AutoIt to do or control other stuff rather than using other stuff to control/run AutoIt. But regardless, it is an example implementation of a different use case for AutoIt beyond the general AutoIt community, like those who also use Selenium WebDriver and/or need to integrate Selenium with AutoIt for remote deployment.
      https://github.com/daluu/AutoItDriverServer
      with specific demo examples here:
      https://github.com/daluu/AutoItDriverServer/blob/master/sample-code/CalculatorTest.java
      https://github.com/daluu/AutoItDriverServer/blob/master/sample-code/calculator.py
      https://github.com/daluu/AutoItDriverServer/blob/master/sample-code/SeleniumIntegrationTest.java
      https://github.com/daluu/AutoItDriverServer/blob/master/sample-code/SeleniumIntegrationWithAutoItDriver.py
×
×
  • Create New...