I am trying to fill all the text fields in Safari where the name ends with _comment1 with a number obtained from a dialog query.
The source code of the text field looks like:
<input type="text" maxlength="6" size="6" name="233941_142117_comment1" value="" onKeyUp="return autoTab (this, 6, event);" onChange="disableButtons();">
It doesn't fill the fields though. Any ideas?
My applescript is:
set query to text returned of (display dialog "Enter Query" default answer "" buttons {"Input", "Cancel"} default button 1)
log "2"
activate application "Safari"
log "3"
tell application "Safari"
log "4"
do JavaScript "
let x = document.querySelectorAll('input[name$=_comment1]');
var i;
for (i = 0; i < x.length; i++) {
x[i].value = query;
}
"
end tell
I outlined the problems with your script in the comment I left below your question. To summarise:
① Your query variable is defined as an AppleScript variable, but goes unused. You then make a reference to a variable in JavaScript called query, which is undefined and has no value.
② Your do JavaScript command has no target within Safari, which needs to perform the action on a specific tab or document.
Solution:
Without seeing the specific site you're working with, I cannot test my solution, but I'm pretty confident that it should rectify these two explicit errors that are most certainly preventing your script from working. Whether or not any other unrelated issues crop up depends on the specifics of the website.
So you've defined your AppleScript variable query, which I'm just going to do lazily here:
set query to "blah blah blah"
The core bit of your script needing amending is here:
tell application "Safari" to tell ¬
the front document to do JavaScript ¬
"x=document.querySelectorAll(\"input[name$='_comment1']\"); " & ¬
"for(let i of x){ i.value=" & quoted form of query & "; }"
Your JavaScript itself is totally fine (although I slightly modified it to my own preference, which I perhaps ought not to have done). The main points here are that I have told Safari that it will be performing the JavaScript in the front document; and that the JavaScript now makes use of the AppleScript query variable with which it uses to set the value of the <input> fields.
Having tested a variant of this on some arbitrary website I found, it appeared successful.
Let me know how it turns out.
Related
I’m currently using AppleScript to automate some tasks in Safari. I have below code, which is supposed to set the value of the text box ‘ownerValue’ to the item from the loop. Whenever I execute the code, nothing happens.
set countryList to {"BR", "CN"}
repeat with country in countryList
tell application "Safari"
activate
tell document 1
do JavaScript "document.getElementById('ownerValue').value = country"
end tell
end tell
end repeat
When I replace the country in the loop to the actual country value, e.g. “BR”, it then inserts the text into the text field.
tell application "Safari"
activate
tell document 1
do JavaScript "document.getElementById('ownerValue').value = ‘BR'"
end tell
end tell
It also seems that AppleScript doesn’t recognise country as an item from the loop, since country is not in green.
Any ideas on how I can fix this so I can let AppleScript loop through the values in countryList?
Thanks!
This will resolve the issue of the variable not being treated as a variable:
set countryList to {"BR", "CN"}
repeat with country in countryList
tell application "Safari"
activate
tell document 1
do JavaScript "document.getElementById('ownerValue').value = '" & country & "';"
end tell
end tell
end repeat
However, you may have another issue here, in that you are looping while inputing the value of a new variable into the same input field. In other words the JavaScript command as written is going to first input BR and immediately overwrite it with CN.
Note that the color of country in the code above is not showing as green, however, here is a clipped screenshot from Script Editor and you see it's green.
To address the comment:
Could you please explain why it was previously not treated as a variable but now is?
When mixing two languages, i.e. AppleScript and JavaScript in this use case, and passing an AppleScript variable to JavaScript, you need to build out the JavaScript command using concatenation so the value of the variable is expanded.
In your code it was being treated as a fixed value as JavaScript has no way of knowing it was an AppleScript variable, hence concatenation, i.e. & ... &. In AppleScript, & is the concatenation character.
I'm using Selenium 3.17.0.
I want to type a text from a javascript, but it doesn't work!
I have tried this (this types the text "undefined")
and this (this types the entire script as text)
but nothing works! maybe I'm doing wrong the javascript but I don't know, I'm new in all of this, please help!
btw this is my first post, sorry if I'm doing it wrong.
If I understand your question correctly, you are trying to generate a random string using javascript and then store the value in a variable. Then enter that variable value using type.
you have to use execute script command to run the javascript and target (your javascript) should be return "free text" + Math.floor(Math.random()*100) as shown in the below screenshot.
How can I loop this code to repeat and set a different variable for every occurrence of the class 'auto-date-time' ?
tell application "Safari"
set myValue to do JavaScript "document.getElementsByClassName('auto-date-time')[0].innerHTML;" in current tab of window 1
end tell
e.g : myValue1 document.getElementsByClassName('auto-date-time')[1]
MyValue2 document.getElementsByClassName('auto-date-time')[2] MyValue3
document.getElementsByClassName('auto-date-time')[3
I know this work fine
tell application "Safari"
set myValue to do JavaScript "var outPut=[]; var arr=document.getElementsByClassName('sortable fraudScoringTransactionCCSummary');for (var i in arr) {outPut.push(arr[i].innerHTML)};outPut;" in current tab of window 1
end tell
But I'm trying to do it different, can that be done?
I tried this which is working but obviously seems pretty bad + that wouldn't fix the issue of the variable
repeat with counter from 1 to 10
tell application "Safari"
set myValue to do JavaScript "document.getElementsByClassName('auto-date-time')[" & counter & "].innerHTML;" in current tab of window 1
end tell
if myValue = missing value then
exit repeat
end if
end repeat
I would say that the cleanest and most efficient way to obtain an AppleScript list of innerHTML property values for a collection of HTML elements is this:
tell application "Safari" to tell ¬
the front document to set ¬
myList to do JavaScript ¬
"Array.from(
document.getElementsByClassName('auto-date-time'),
x => x.innerHTML
);"
EDIT (2019-04-02):
Addressing a comment below where you reported no return value, and in lieu of you providing the console return value for the JavaScript above, here's an alternative method that you can try:
tell application "Safari" to tell document 1 to set myList to ¬
do JavaScript "[...document.getElementsByClassName('auto-date-time')]
.map( x => x.innerText );"
The two differences are the use of property innerText instead of innerHTML (which you can also apply to the first JavaScript), and method used to construct the array (although, assuming you're running the latest version of Safari, shouldn't make a difference).
A note on error-catching
Try to get out of the habit of using try blocks in the manner that you tend to do. They shouldn't be used if its purpose is to prevent a script from terminating through an error that you weren't expecting to occur or don't understand why the error occurs. They are used to catch errors you predict will arise in specific situations, knowing why it happens, and allowing you to use it to your advantage. Otherwise, all you are doing is stopping an unexpected error from alerting you to a flaw in your script, and where/when it's arising.
Find it :
I need to use repeat / plus list, although pretty sure that's still not the right way to do it.
set myList to {}
try
repeat with counter from 1 to 1000
tell application "Safari"
set myValue to do JavaScript "document.getElementsByClassName('auto-date-time')[" & counter & "].innerHTML;" in current tab of window 1
end tell
if myValue = missing value then
exit repeat
else if myValue is not equal to "" then
set the myList to the myList & myValue
end if
end repeat
on error
--
end try
return myList
I have to explain how a specific Javascript code validates a web form, but I am stuck with what some of the features do, most specifically this section of the code. I understand that the first line defines that the rest of the section should only run if the field Field1 of the form ExampleForm is left empty, but I do not know what purpose the rest of the code serves. All I know is that msg is a variable created earlier in the document with an empty default value, and that result is another variable with a default value of true. Can anyone help me out by explaining what each line does?
if (document.ExampleForm.Field1.value=="") {
msg+="You must enter your name \n";
document.ExampleForm.name.focus();
document.getElementById('Field1').style.color="red";
result = false;
}
In plain english:
If the document form field value is equal to an empty string, set the error message to msg, then focus on the element, and give is a red color so the user knows it's an error, and set the result to false, for whatever you're going to use that for later in your code/function.
So this would in part depend on what other code is on the page. For example document.ExampleForm is not part of the DOM and seems to be something someone kludged onto your page.
Overall I would say this is pretty bad code that makes a ton of assumptions that won't necessarily hold up written by someone who doesn't understand in-browser javascript very well, but let's go with it
//if the value in this variable is falsy (false, empty, or 0)
if (document.ExampleForm.Field1.value=="") {
//Append this to the msg string. Note \n is usually used
//to indicate "new line" but wont' do anything on the web since that's not how line breaks
//work on the web
msg+=”You must enter your name \n”;
//Invoke the `focus` field on this variable. From the context I assume this is
//a DOM node so you are basically focusing it in the browser
document.ExampleForm.name.focus();
//Set the font color of '#Field1' to red
document.getElementById('Field1').style.color=”red”;
//Presumably result is something that tells you true/false did the validation succeed.
//set it to false to indicate failure.
result = false;
}
My guess about what document.ExampleForm is that it depends on some undocumented behavior of an old browser to add anything with id=ExampleForm to the document element. But really I have no idea. Maybe you have some code elsewhere that creates that variable. Either way its a horrible idea and should get someone yelled at.
I have the user entering a post-number in a form-field, and then I want to display the town with that post-number.
I have the server-side function set up, it takes a variable from the URL and returns a plain string.
As I see it, I need to get the variable from the form-field after the user has written it and the focus has left the form-field, use that number in an XMLHttpRequest call to the server and display the resulted string.
But the problem is, that I've never written any JavaScript, more complex than a Hello World. So I would like some help with writing the JavaScript for that page, any help would be appreciated.
Since you are new to JavaScript I won't recommend using any JS framework for this.
To get the value from an input box you can use
document.getElementById("txt1").value;
where txt1 is the id of the input element.
Then you can append the element value to the query string and call the server-side function. And if the response text is a plain string put that inside a div or span
document.getElementById("divTown").innerText = "response string"; // for IE
document.getElementById("divTown").textContent = "response string"; // for FF
You can get a basic understanding of AJAX and JavaScript read these
AJAX Introduction
JavaScript Tutorial