seadoggie01 Posted May 8, 2020 Posted May 8, 2020 @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 functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types
frank10 Posted May 8, 2020 Posted May 8, 2020 (edited) 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: expandcollapse popup#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 May 8, 2020 by frank10
Danp2 Posted May 8, 2020 Author Posted May 8, 2020 @frank10 Take a look at the functionality of _WD_jQuerify and how it loads jQuery into an existing webpage. Depending on your needs, you may be able to use a dumbed down version of it to load a JS file. Latest Webdriver UDF Release Webdriver Wiki FAQs
frank10 Posted May 8, 2020 Posted May 8, 2020 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 ' " ?
Danp2 Posted May 8, 2020 Author Posted May 8, 2020 48 minutes ago, frank10 said: I want to insert a local js file That might have been useful to include in your initial post. 🙄 P.S. I recall that we ran into issues with attempting to load a local copy of jQuery, but thinking this is different. Latest Webdriver UDF Release Webdriver Wiki FAQs
hoangvu0905 Posted May 8, 2020 Posted May 8, 2020 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"]}
hoangvu0905 Posted May 8, 2020 Posted May 8, 2020 (edited) I did it. I use "text": "\ uE007", "value": ["\ uE007"] I am very thankful for your enthusiasm for helping. Edited May 8, 2020 by hoangvu0905
JackER4565 Posted May 9, 2020 Posted May 9, 2020 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. Thanks for the reply!
Danp2 Posted May 9, 2020 Author Posted May 9, 2020 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. Latest Webdriver UDF Release Webdriver Wiki FAQs
JackER4565 Posted May 10, 2020 Posted May 10, 2020 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.
Danp2 Posted May 10, 2020 Author Posted May 10, 2020 @JackER4565 Did you happen to try the existing functions in wd_helper.au3 for dealing with frames (ie: _WD_FrameEnter & _WD_FrameLeave)? Latest Webdriver UDF Release Webdriver Wiki FAQs
frank10 Posted May 10, 2020 Posted May 10, 2020 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.
Danp2 Posted May 10, 2020 Author Posted May 10, 2020 @frank10 You should be able to prevent the characters from being escaped by temporarily adjusting the value of $_WD_ESCAPE_CHARS. This is normally equal to '\\"', but you can change it in your script to suit your needs. Latest Webdriver UDF Release Webdriver Wiki FAQs
sarge Posted May 10, 2020 Posted May 10, 2020 @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?
Danp2 Posted May 10, 2020 Author Posted May 10, 2020 @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. Latest Webdriver UDF Release Webdriver Wiki FAQs
sarge Posted May 11, 2020 Posted May 11, 2020 @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...
Danp2 Posted May 11, 2020 Author Posted May 11, 2020 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 Latest Webdriver UDF Release Webdriver Wiki FAQs
hoangvu0905 Posted May 12, 2020 Posted May 12, 2020 Sorry to bother you again. I use firefox to reopen the existing profile. However this firefox does not save my previous used browser history. Can you help me Thank you very much.
sarge Posted May 12, 2020 Posted May 12, 2020 @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 ...
Danp2 Posted May 12, 2020 Author Posted May 12, 2020 @sarge It sounds like you've set $_WD_DEBUG = $_WD_DEBUG_None. For debugging purposes, suggest that you leave it at $_WD_DEBUG_Info so that you get the maximum level of output. P.S. You've left out the $sStartElement parameter in your call to _WD_FindElement, so that's why your array is empty 😀 Latest Webdriver UDF Release Webdriver Wiki FAQs
Recommended Posts