How to pass variables to JavaScript?

Hello,
In my current workflow I create a table and then create a loop and pass variables to FindElements selenium node.
I have tried to replace the FindElements and SendKeys nodes with Javascript node. However the node does not seem to get access to my variables.
In the code below I want to replace “?” with variables passed from a table I created through the loop to the Javascript node. Somehow, I am not able to see the same variables in Javascript node that I see in the FindElements node.
Any suggestions how to access my variables in Javascript node?

Please ignore! I forgot to connect to the variable port. Sorry!

1 Like

OK, so I see the variables now but I don’t know how to use them in Javascript. This is what I wrote (sorry, I am new to it)

public class Form {
public static void main(String args) {

      // Get the WebElement corresponding to the HST Number		
    //WebElement HSTNumber = driver.findElements(By.name("businessNumber"));							

    // Get the WebElement corresponding to the Business Name		
    //WebElement businessName = driver.findElements(By.name("businessName"));							

    // Get the WebElement corresponding to the Request Date	
    //WebElement requestDate = driver.findElements(By.name("requestDate"));	
		
     
    // Find the submit button		
    //WebElement Search = driver.findElement(By.name("reg.label.submit"));							
                		
		
    //using submit method to submit the form. Submit used on password field		
    driver.findElements(By.name("businessNumber")).sendKeys([[Flow] HST]);							
    driver.findElements(By.name("businessName")).sendKeys([[Flow] Name]);
    driver.findElements(By.name("requestDate")).sendKeys([[Flow] Date]);					
    driver.findElements(By.name("reg.label.submit")).submit();					
	
}		

}

It does not work. Can someone help create the correct script? The variables are shown in the screenshot.

I’ve never used the nodes, but the documentation for them states:

Select columns from the input table or flow variables, which provide arguments for the executed code snippet. The selected entries are available in code in the listed order via the arguments array. Enable the last entry [asyncCallbackMethod] in case you’re running an asynchronous script to be able to signal end of execution.

I wish I knew how to transform what I have into arguments. If anyone can help, I would really appeciate it (I am not a developer, so struggling here)

I believe it is saying that the array in your main’s method signature String[] args will reflect what you have selected on the left, in the order listed.

So in your screen shot, args[0] would be the Name flow variable… if you checked the SearchNow column, that value would be in args[0] because it is listed before Name in that UI pane.

… so, for example:

driver.findElements(By.name("businessName")).sendKeys(args[0]);

I believe I actually understood what you are saying and change the scripts. Now I am getting "Execute failed: Expected ‘;’ "
Did I miss a syntax somewhere?

without seeing any of your code, as you haven’t pasted in the new version: yes; yes you did.

You are funny…:slight_smile: Here it is:

public class Form {
public static void main(String args) {

      // Get the WebElement corresponding to the HST Number		
    //WebElement HSTNumber = driver.findElements(By.name("businessNumber"));							

    // Get the WebElement corresponding to the Business Name		
    //WebElement businessName = driver.findElements(By.name("businessName"));							

    // Get the WebElement corresponding to the Request Date	
    //WebElement requestDate = driver.findElements(By.name("requestDate"));	
		
     
    // Find the submit button		
    //WebElement Search = driver.findElement(By.name("reg.label.submit"));							
                		
		
    //using submit method to submit the form. Submit used on password field		
    driver.findElements(By.name("businessNumber")).sendKeys(args[1]);							
    driver.findElements(By.name("businessName")).sendKeys(args[0]);
    driver.findElements(By.name("requestDate")).sendKeys(args[2]);					
    driver.findElements(By.name("reg.label.submit")).submit();					
	
}		

}

I can’t see anything missing there, unfortunately. If you export your workflow and attach it to the thread, i’ll install the nodes and take a look later this evening.

oh… now that i’ve installed the Selenium nodes (can’t use them as i haven’t gotten a temporary license email yet) i see all of that Java class code above is something you’ve added - not what comes up automatically in the Execute Javascript configuration panel (which makes a lot more sense as to why it says “Add your Javascript code below:”)

So, you’ll want to be using only JavaScript - no Java. It’s unclear whether you’d have something jQuery available to you (maybe it the web page has it imported you’d have it?) - which would make finding the DOM elements easier, and acting on them en masse much easier.

At any rate, the JavaScript variable you want is apparently called arguments - so the flow variables or column variables would appear, ordered as seen in that left pane, as arguments[0] etc…

Assuming you had jQuery at your disposal and assuming that all things with the attribute/value pair of name/businessName were input elements, you could do something like:

$("[name=businessName]").val(arguments[0]);

At the end of the day, though, this is going to involve writing JavaScript code on your part.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.