Jump to content

WebDriver UDF - Help & Support (II)


Danp2
 Share

Recommended Posts

@Danp2 Thank you, that works perfectly! Drat, I got so close, just had to wrap it in prefs, ugh. (And it changes that setting on chrome://settings/content/pdfDocuments that I wrote the ShadowRoot code for)

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

Let's say I have a js script file, like:

<script  type='text/javascript'>
    var links = document.evaluate("//div[@data-asin]//h2//a[contains(@href,'/dp/')]/@href", document, null, XPathResult.ANY_TYPE, null );
    ......
</script>

1) Is there a way to insert in the page Head, well-formatted string like in the file? I tried to insert like:

Global $scriptJS = FileRead("fileScriptJS.js")

 local $jqueryCommand = 'jQuery("head").append("' & $scriptJS & '")' ;
  _WD_ExecuteScript($sSession, $jqueryCommand )

but I get an error, I suppose because it does not interpret the multilines as strings,

2) so I tried to put all into one line and it works... like this:

local $jqueryCommand = 'myScript ="' & StringReplace($scriptJS,@LF, " ") & '";'   
    _WD_ExecuteScript($sSession, $jqueryCommand )

But, with these types of commands including " char I  get this error,

__WD_Post: URL=HTTP://127.0.0.1:9515/session/ddac395eff33214480a453b726a247eb/execute/sync; $sData={"script":"jQuery(\"head\").append(\"<script  type='text/javascript'>        var links = document.evaluate(\"//div[@data-asin]//h2//a[contains(@href,'/dp/')]/@href\", document, null, XPathResult.ANY_TYPE, null ); </script>\")", "args":[[]]}
__WD_Post: StatusCode=500; ResponseText={"value":{"error":"javascript error","message":"javascript error: missing ) after argument list\n

To understand what chrome got I put the $scriptJS into a var and that worked:

$sData={"script":"myScript =\"<script  type='text/javascript'>      var links = document.evaluate(\"//div[@data-asin]//h2//a[contains(@href,'/dp/')]/@href\", document, null, XPathResult.ANY_TYPE, null ); </script>\";", "args":[[]]}
__WD_Post: StatusCode=200; ResponseText={"value":null}

BUT the string is cut at " point on chrome-side.

"<script  type='text/javascript'>         var links = document.evaluate("

 

So:

is there an easy way to insert JS code from a file managing all these ' and " things, also in well-formatted multiline string?

 

script example:

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

OnAutoItExitRegister("_onExit")

Local $sDesiredCapabilities

$_WD_DEBUG =  True ; You could also use $_WD_DEBUG_Error
;~ You can also control the visibility of the console with the function _WD_ConsoleVisible.

_WD_Option('Driver', 'chromedriver.exe')
_WD_Option('Port', 9515)
_WD_Option('DriverParams', '--log-path=' & @ScriptDir & '\chrome.log')

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["--user-data-dir=C:\\Users\\' & @UserName & '\\AppData\\Local\\Google\\Chrome\\User Data\\", "--profile-directory=Default"]}}}}'

_WD_Startup()

$sSession = _WD_CreateSession($sDesiredCapabilities)

Global $scriptJS = FileRead("fileScriptJS.js")

If @error = $_WD_ERROR_Success Then
    _WD_Navigate($sSession, "https://www.amazon.com/dp/B07H71XXGY/")
    _WD_LoadWait($sSession)
    _WD_jQuerify($sSession)

     local $jqueryCommand = 'jQuery("head").append("' & StringReplace($scriptJS,@LF, " ") & '")' 
    _WD_ExecuteScript($sSession, $jqueryCommand )
    
    MsgBox(0,'',"")
EndIf

Func _onExit()
    _WD_DeleteSession($sSession)
    _WD_Shutdown()
EndFunc

 

 

Edited by frank10
Link to comment
Share on other sites

But jQuerify loads a src file into <script> using src property, linking a remote file on a server. I want to insert a local js file as a text into the <script> tag and add to head in the server page. So it's similar to insert a line string with a lot of ' and ", like:

local $jqueryCommand = "jQuery('head').append(""<script  type='text/javascript'> var links=document.evaluate(""//div[@data-asin]//h2//a[contains(@href,'/dp/')]/@href"", document, null, XPathResult.ANY_TYPE, null ); </script>"")"

I tried with double "" but I get error:

$sData={"script":"jQuery('head').append(\"<script  type='text/javascript'> var links=document.evaluate(\"//div[@data-asin]//h2//a[contains(@href,'/dp/')]/@href\", document, null, XPathResult.ANY_TYPE, null ); </script>\")", "args":[[]]}
__WD_Post: StatusCode=500; ResponseText={"value":{"error":"javascript error","message":"javascript error: missing ) after argument list\n

How do you handle all this mixed ' " ?

Link to comment
Share on other sites

I get an error when using ver 0.2.0.8:
__WD_Post: StatusCode = 500; ResponseText = {"value": {"error": "unknown error", "message": "'value' must be a list (Session infochrome = 81.0.4044.138)", "stacktrace": "Backtrace: \ n \ tOrdinal0 [0x006D3CF0 + 867568] \ n \ tOrdinal0 [0x0067DB7D + 514941] \ n \ tOrdinal0 [0x0064B367 + 308071] \ n \ tOrdinal0 [0x00616871 + 92273] \ n \ tOrdinal0 [0x0062AE8D + 175757] \ n8 \ n \ tOrdinal0 [0x0062B111 + 176401] \ n \ tOrdinal0 [0x0063278D + 206733] \ n \ tOrdinal0 [0x0062AD2B + 175403] \ n \ tOrdinal0 [0x00613740 + 79680] \ n \ tOrdinal0 [0x00614CFC + 85244] \ 0x00614C50 + 85072] \ n \ tOrdinal0 [0x006DE62A + 910890] \ n \ tOrdinal0 [0x00688993 + 559507] \ n \ tOrdinal0 [0x00688BA3 + 560035] \ n \ tOrdinal0 [0x00688C8C + 560268] \ n \ t00 n \ tOrdinal0 [0x006887FF + 559103] \ n \ tOrdinal0 [0x0069306E + 602222] \ n \ tOrdinal0 [0x0069EA3B + 649787] \ n \ tOrdinal0 [0x0069EBA5 + 650149] \ n \ tOrdinal0 [0x0069DDA5 + 646587] +25] \ n \ tRtlGetAppContainerNamedObjectPath [0x772F7C24 + 228] \ n \ tRtlGetAppContainerNamedObjectPath [0x772F7BF4 + 180] \ n "}}

Previously my value was of {"id": "<id element>", "Text": "Text", "value": ["T", "e", "x", "t"]}

Link to comment
Share on other sites

19 hours ago, Danp2 said:

@JackER4565 It's not clear from your example, but I believe that you are targeting the wrong element. You have to target the input element, not the div element that represents the main datetime picker. The following appears to me to work correctly on the link you provided --

$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='datetimepicker1']//input[@class='form-control']")
_WD_ElementAction($sSession, $sElement, 'value', '07/05/2020')

Please try and let me know if this works for you.

Sorry I couln't explain myself. (I forgot to change the input id from my work page to the one in the example)

 

Yes you could write to the input the way you show, that way you create a value subtag and fill it.

BUT if you use the date picker itself it does not use the value subtag but something else.. So let say there is a submit button after that, it wont read the input value but the thing the datapicker uses.

 

datepkr.png.0a819c9317ece43cb4515e6b9aa9b471.png

Thanks for the reply!

Link to comment
Share on other sites

5 hours ago, JackER4565 said:

Yes you could write to the input the way you show, that way you create a value subtag and fill it. BUT if you use the date picker itself it does not use the value subtag but something else

Yes it does... it just isn't shown in the DevTools window. With the input element highlighted, try entering this in the browser console --

$0.value

For me, it returned the value currently displayed in the input element.

Suggest that you review this page and then try something like this --

_WD_ExecuteScript($sSessionID, '$("#datetimepicker1").data("DateTimePicker").date("07/07/2020")')

If that doesn't resolve your issue, then I don't know how to help further without being able to actually observe the issue.

Link to comment
Share on other sites

21 hours ago, Danp2 said:

Yes it does... it just isn't shown in the DevTools window. With the input element highlighted, try entering this in the browser console --

$0.value

For me, it returned the value currently displayed in the input element.

Suggest that you review this page and then try something like this --

_WD_ExecuteScript($sSessionID, '$("#datetimepicker1").data("DateTimePicker").date("07/07/2020")')

If that doesn't resolve your issue, then I don't know how to help further without being able to actually observe the issue.

Danp2:

 

Turns out, the input I was trying to find was inside of an 'Iframe' tag.. so I was never gonna find it:

 

Quote

_WD_Post: StatusCode=404; ResponseText={"value":{"error":"no such element","message":"no such element: Unable to locate element:

 

So I did this:

 

_WD_ExecuteScript($sSession, 'var x = document.getElementById("ctl00_mainFrame").getAttribute("src");window.location.href = x;')

It gets the iframe's src (by id) and then navigates to it, then I was able to find the input and fill it successfully!

 

Thanks a lot for the help.

Link to comment
Share on other sites

It seems there is a bug in escaping "\" chars with some combinations of chars:

original:

ASIN =  /dp\/([\w]+)\//.exec(node.textContent);


JSON sent:

ASIN =  /dp\\/([\\w]+)\\//.exec(node.textContent);


JS result:

ASIN =  /dp/([w]+)//.exec(node.textContent);

so JS believes the regExp is only "/dp/" and from the // char on, it's interpreted like a comment, instead of the correct RegExp.

Link to comment
Share on other sites

@Danp2 Hello.

I need to enter into the array the addresses of several pictures on the page.
Is it possible to pull out the addresses of images through the web driver udf?
I am stuck on this:

_WD_Navigate($sSession, "https://test.com/")
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//img src=[@width='180']")
_ArrayAdd($testarray, $sElement)

But unsuccessfully
And if possible, then how?

Link to comment
Share on other sites

@sarge Ok... so I got that you want to grab the source location for images. What isn't clear is which images that you want to target. Do you want all images on the page or only some subset?

Depending on your requirements, you may be able to grab all of the image elements using a single call to _WD_FindElement (set the $lMultiple parameter to True). That would give you an array of element IDs. Once you have those, you can use the following to retrieve the source attribute of each element --

For $sElement In $aElements
    $sSource  = _WD_ElementAction($sSession, $sElement, "Attribute", "src")
    ; Got it; now do something with it
Next

Modify the above as needed to fit your code.

Link to comment
Share on other sites

@Danp2Thank you!

I will clarify. I looked at the picture page again
On the page there are several dozen links of the form:

<a href="/office_online/"> office_online</a>

And I need to put the text part(office_online) of each link in the array
I do:

$aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//a[@href='']", True)

ArrayDisplay($aElements)

But the array is not showing.
What am I doing wrong?

P.S. I'm sorry if I asked a very stupid question...

Link to comment
Share on other sites

2 hours ago, sarge said:

$aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//a[@href='']", True)

When you run this code, what output do you see in the Scite console? This should help answer your question re: why the array doesn't exist.

2 hours ago, sarge said:

"//a[@href='']"

What exactly do you expect this xpath to retrieve? I never claimed to be an xpath expert, but I don't think this will give you the desired results.

Also, I'm confused because you initially asked a question about the source attributes of image elements and now you are looking text associated with links. Are these two separate issues or was the first post a "misdirection"? 😵

P.S. Remember that we can't read your thoughts. Try to be clear when communicating your goals; explain what you've already tried and show your code / results

Link to comment
Share on other sites

@Danp2I am sorry,
There are several images on the page with links, like this:

https://test.com/office_online, https://test.com/office_2, https://test.com/...etc...
I need to click on the random image on the page, to follow the link.
To do this, I want to pull the link(office_online, ..etc) from it, and put it in an array.

$ aElements  =  _ WD _ FindElement ( $ sSession ,  ..............????????............ ,  True )
ArrayDisplay ( $ aElements )

Next click on the element from the array:

$x = $aElements[Random(0,UBound($aElements)-1,1)]
_WD_Navigate($sSession, "https://test.com/'&$x&'")

Is it possible to do this (pull the link into an array) using the webdriver udf?

Or maybe it is implemented by other means?

P.S. When I run the code that I wrote in the previous message, nothing is displayed in the console at all ...

Link to comment
Share on other sites

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