Jump to content

WebDriver UDF - Help & Support (III)


Danp2
 Share

Recommended Posts

@Danp2 Not in this specific case, because at my day job, the browser is forced to delete all cookies after closing.

Okay, I'll try to automate it the old way using MouseClick. 

Update: 

_WD_Navigate($sSession, "https://www.gmx.net/pro_ssl/?origin=lpc")
_WD_LoadWait($sSession)
Sleep(1400)

MouseClick("Left", 678, 980)
Sleep(700)

 Sometimes, oldschool is still the easiest way 

Edited by Marc

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to comment
Share on other sites

@Marc  I like old school too.  Here my suggestion to be a bit more contemporary

_WD_Navigate($sSession, "https://www.gmx.net/pro_ssl/?origin=lpc")
_WD_LoadWait($sSession)
Sleep(2500)

Local $hWnd = WinGetHandle("GMX:")
ConsoleWrite($hWnd & @CRLF)
Local $hCtrl = ControlGetHandle($hWnd, "", "Chrome_RenderWidgetHostHWND1")
ConsoleWrite($hCtrl & @CRLF)

ControlClick($hWnd, "", $hCtrl, "left", 1, 760, 685)

;)

Link to comment
Share on other sites

_WD_ElementSelectAction with $sCommand = "options" seems to return the displayed value of an option as it uses option.label in JavaScript. I don't think this is wrong, just something to watch out for. For example, on a internal site, we have values that sometimes have leading spaces. They aren't displayed when the dropdown is visible, but they're visible in the HTML of the website.

<select id="CustomCode113ID">
  <option value="1"> Apples</option>
  <option value="2">  Bananas</option>
  <option value="3">Cookies</option>
</select>

I tried using "//select[@id='CustomCode113ID']/option[text()='Apples']", however it didn't find the element using this method. An alternate solution was using "[...]contains(text(),'Apples')", but that was a bit more vague than I wanted to be.

My solution was to use the value instead of text when looping through the options. The xPath looked like this: //select[@id="CustomCode113ID"]/option[@value=1]

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

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

19 minutes ago, seadoggie01 said:

_WD_ElementSelectAction with $sCommand = "options" seems to return the displayed value of an option as it uses option.label in JavaScript.

Not sure that I understand the issue. The function should return an array containing both the value and label text for each available option.

Can you further describe the actual problem or post a short reproducer script?

🙂

Link to comment
Share on other sites

Not so much of a problem as a, "I thought this was a problem, but instead here's how I fixed worked around it". There's nothing wrong with _WD_ElementSelectAction, it's just that using xPath, you can't say text()='Apples' because the text is actually ' Apples'.

; <WebDriver Setup Stuff>
; Find the select
Local $sSelectElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPATH, "//select[@id='CustomCode113ID']")
; Get the list of child options
Local $aOptions = _WD_ElementSelectAction($sSession, $sSelectElement, "options")
; For each option
For $i=0 To UBound($aOptions) - 1
    // This returns error, element not found because the text isn't an exact match
    _WD_FindElement($sSession, $_WD_LOCATOR_ByXPATH, "/options[text()='" & $aOptions[$i][0] & "']", $sSelectElement)
    
    // This works instead
    _WD_FindElement($sSession, $_WD_LOCATOR_ByXPATH, "/options[@value='" & $aOptions[$i][1] & "']", $sSelectElement)
    
    // This also works, but I don't like :)
    _WD_FindElement($sSession, $_WD_LOCATOR_ByXPATH, "/options[contains(text(),'" & $aOptions[$i][0] & "')]", $sSelectElement)
Next

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

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

@samibbIf you take a look at the code for _WD_SetElementValue, you'll see that it is just a wrapper for calling _WD_ElementAction with the 'value' option. To retrieve the value instead of setting it, you would use almost the exact same code --

$sResult = _WD_ElementAction($sSession, $sElement, 'value')

Note that I left off the last parameter, so _WD_ElementAction retrieves the current value instead of attempting to set it.

Link to comment
Share on other sites

@Danp2 The leading space is truncated. In JavaScript, document.getElementById("CustomCode113ID").children[0].label returns the text truncated as well, so I would say the functionality is correct

@samibb _WD_ElementAction($sSession, $sElement, $sCommand, $sOption = Default) and use "value" for $sCommand

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

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Right, that's why I didn't submit an issue, just posted my work-arounds :)

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

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

@Danp2 & @seadoggie01

i use $b1 and $bb1 and both works  thy gave IDNo.

i would say i am using   Cryptic Window

SEE THE ATTACHMEN 

while 1
send("lp/sv1020/1aug")

send("{enter}")
SLEEP(3000)

_WD_FrameEnter($sSession, 4)
Local $Bb1 = _WD_GetElementById($sSession,"tpl1_shellbridge_shellWindow_top_left_modeString_currentCommand")
 Local $B1 =_WD_FindElement($sSession, "xpath","/html/body/div[1]/div[5]/div/div/div/div[3]/div[3]/div/div[2]/div/div[3]/div/div[4]/div[2]/div/div/span/span/div/div[1]/div/div[2]/div/div/span")
SLEEP(3000)
                   ;I HAVE GOT IDNo FOR BOTH
MsgBox(0,$B1,$Bb1)
Local $sResult = _WD_ElementAction($sSession, $Bb1, 'value')
MsgBox(0,$B1, $sResult) ;  No Data

WEnd

 

 

TEST.png

Link to comment
Share on other sites

@samibb

  1. I know that English isn't your native language, but you can always use Google Translate.
  2. You haven't explained what information you are trying to obtain from the website
  3. The elements that you are targeting don't have a "value" attribute, so that is likely why you aren't getting the desired result
  4. Did you check the console output for errors? This should be your first step when diagnosing your script.
  5. This information can also help us to assist you, so it's often a good idea to include the console output when you post your code
  6. Because you didn't show us the console output, we have no way of knowing that $B1 and $BB1 contain valid element IDs
  7. Can you explain why you are using Send() at the beginning of your script?
Link to comment
Share on other sites

@samibb That element doesn't have a named value, so it won't return anything. You'll want to get the xPath to the <code> element (below id="responseCommand"), then possibly get the "text" instead of the "value" using _WD_ElementAction. The key to learning about these things is to try a few different things. Mess with it, the worst that happens is you learn 5 ways not to complete your task ;)

Edit: This is silly, stop beating me to the punch, Danp2 :P

Edited by seadoggie01

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

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Dear @DAnp2

it works with me .:D

i get <code> Xpath

Local $B1 =_WD_FindElement($sSession, "xpath", "/html/body/div[1]/div[5]/div/div/div/div[3]/div[3]/div/div[2]/div/div[3]/div/div[4]/div[2]/div/div/span/span/div/div[1]/div/div[2]/div/div/span/div/div[2]/span/div/div/pre/code")

Then

Local $sResult = _WD_ElementAction($sSession, $B1, 'Text')

 

  • How I know the processing data is done and response data is show up?
  1.     Can you explain why you are using Send() at the beginning of your script?
  •        I am using Send() to make  "ENTER" because there is no function to make "ENTER" key and it         

       works.

is there another way?

   Thanks

Edited by samibb
Link to comment
Share on other sites

1 hour ago, samibb said:

How I know the processing data is done and response data is show up?

Not sure what you mean here. Are you asking at what point you can access the data being returned from _WD_ElementAction? If so, the answer is "whenever your script proceeds to the next line in your script"

1 hour ago, samibb said:

 I am using Send() to make  "ENTER" because there is no function to make "ENTER" key and it   

You are doing more than just sending Enter. Where is "lp/sv1020/1aug" being sent to? If an element in the browser, which one? Did you try using _WD_SetElementValue here?

<rant>
FWIW, It gets frustrating when people (not just you 😉) ask for help, but aren't voluntarily providing sufficient information so that we can offer solutions / recommendations. Sometimes it feels like I'm like I'm "pulling teeth" to get the necessary details.
</rant>

P.S. It is actually possible to send Enter to an element using Webdriver commands. I think this has come up previously on the forum (you can try searching on _WD_Action and the 'actions' command), but it's not for the faint of heart.

Link to comment
Share on other sites

Not sure anyone cares, but I (finally!) found a way to prevent tabs from being discarded. Theoretically, this should work, but currently doesn't

Spoiler

AutoIt function:

; #FUNCTION# ====================================================================================================================
; Name ..........: _WDEx_DiscardableTab
; Description ...: Changes the Discardable status of a tab, use BEFORE a tab could be discarded
; Syntax ........: _WDEx_DiscardableTab($sSession[, $sHeaderDataSortKey = "tabUrl", $sData[, $sDiscardsPage = "chrome://discards"[, $bDiscardable = False[, $sJsFullFile = Default]]]])
; Parameters ....: $sSession            - Session ID from _WD_CreateSession.
;                  $sHeaderDataSortKey  - a string value. Default is "tabUrl".
;                  $sData               - a string value.
;                  $sDiscardsPage       - [optional] url to use, browser dependent. Default is "chrome://discards".
;                  $bDiscardable        - [optional] a boolean value. Default is False.
;                  $sJsFullFile         - [optional] a string value. Default is Default.
; Return values .: Success - True
;                  Failure - False and sets @error:
;                  |-1 - _WD_Window error getting current window handle, @extended is _WD_Window's error
;                  |-2 - _WD_NewTab error navigating to discards page, @extended is _WD_NewTab's error
;                  |-3 - _WD_Window error closing discards page, @extended is _WD_Window's error
;                  |-4 - _WD_Window error switching back to original webpage, @extended is _WD_Window's error
;                  |Other - _WD_ExecuteScript error and extended values
; Author ........: Seadoggie01
; Modified ......: July 25, 2021
; Remarks .......: $sHeaderDataSortKey is found by inspecting a header column, then getting the data-sort-key's value
;                  $sData is the innerHTML expected in the column. Note that the Tab Title column shouldn't be used as it includes an icon
;                  Requires isAutoDiscardable.js!!!
; Related .......: 
; Link ..........: 
; Example .......: No
; ===============================================================================================================================
Func _WDEx_DiscardableTab($sSession, $sHeaderDataSortKey, $sData, $sDiscardsPage = "chrome://discards", $bDiscardable = False, $sJsFullFile = Default)

    If IsKeyword($sJsFullFile) Then $sJsFullFile = @ScriptDir & "\isAutoDiscardable.js"
    If IsKeyword($sHeaderDataSortKey) Then $sHeaderDataSortKey = "tabUrl"
    If IsKeyword($bDiscardable) Then $bDiscardable = False
    If IsKeyword($sDiscardsPage) Then $sDiscardsPage = "chrome://discards"

    ; Get old window so we can switch back to it
    Local $sOldHandle = _WD_Window($sSession, "window")
    If @error Then Return SetError(-1, @error, False)
    
    ; Open a new tab to the discards page
    _WD_NewTab($sSession, True, Default, $sDiscardsPage)
    If @error Then Return SetError(-2, @error, False)

    ; Execute the script
    Local $vRet = _WD_ExecuteScript($sSession, FileRead($sJsFullFile), '{"headerDataSortKey": "' & $sHeaderDataSortKey & '", "data": "' & $sData & '", "discardable": "' & $bDiscardable & '"}')
    Local $iErr = @error, $iExt = @extended
    
    ; Close the discards page
    _WD_Window($sSession, "close")
    If @error Then Return SetError(-3, @error, False)
    
    ; Make sure we jump back to the old active window
    _WD_Window($sSession, "switch", '{"handle":"' & $sOldHandle & '"}')
    If @error Then Return SetError(-4, @error, False)

    Return SetError($iErr, $iExt, $vRet)

EndFunc

Required isAutoDiscardable.js

return isAutoDiscardable(arguments[0].headerDataSortKey, arguments[0].data, arguments[0].discardable);

function isAutoDiscardable(headerDataSortKey, data, discardable){
    // Get the base table (buried under two shadow roots)
    var discardsTable = document.querySelector('discards-main').shadowRoot.querySelector('iron-pages > discards-tab').shadowRoot.querySelector('#tab-discard-info-table');
    // Determine the column of the requested header
    var dataIndex = discardsTable.querySelector('thead > tr > th[data-sort-key="' + headerDataSortKey + '"]').cellIndex;
    // Determine the column of 'Auto Discardable'
    var columnIndex = discardsTable.querySelector('thead > tr > th[data-sort-key="isAutoDiscardable"]').cellIndex;
    // Get the list of rows
    var rowList = discardsTable.querySelectorAll('tbody > tr');
    // For each row
    for(var i=0; i<rowList.length; i++){
        // If the row has the correct data at the data column's index
        if(rowList[i].querySelectorAll('td')[dataIndex].innerText === data){
            // Check if discardable is NOT equal to the innerHTML (converted from checkmark/bold X in unicode to boolean)
            if(discardable != (rowList[i].querySelectorAll('td')[columnIndex].querySelector('div').innerHTML === String.fromCharCode(parseInt(2714,16)))){
                // Togle the discardablility
                rowList[i].querySelectorAll('td')[columnIndex].querySelector('div[is="action-link"]').click();
            }
            // the entry was found
            return true;
        }
    }
    // No matching entries were found
    return false;
}

Edit: Tested on Chrome and Edge, but it likely works on any Chromium browser

Edited by seadoggie01

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

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

I'm trying to export the text from a page but I don't know where to start.

I've tried

$Source = _WD_GetSource($sSession)
MsgBox (0, 'Source', $Source)

But I think I'm only getting the data from the 1st tab and because its framed I'm not getting all the text, I have 4 tabs ($tab1,$tab2...etc), and i wanted to get the text of each of those tabs.

"Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." - Terry Pratchett.

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...