Jump to content

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)

 

Link to comment
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....
Link to comment
Share on other sites

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
Link to comment
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 😎

Link to comment
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
Link to comment
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()

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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)

 

Link to comment
Share on other sites

  • 3 weeks later...
<!-- 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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...