Using execute script in Selenium IDE to calcute a duration of time from two dates on the page

71 Views Asked by At

I've been using SIDE to automate some processes at work. I'm collecting some dates from the page using the store command and trying to use the execute script command to calculate a duration and then input it elsewhere on the page. Currently, I am getting NaN as the result of my script, through the echo command and seeing that it writes NaN in the box where the duration is supposed to go.

My Silenium IDE windows looks like this: Image showing variables as they are stored from page and the script

The script I'm passing with the execute script function is this:

const startDate = new Date(`${StartYear}`, `${StartMonth}` - 1, 1);
const endDate = new Date(`${EndYear}`, `${EndMonth}`, 1);     
const durationInMilliseconds = endDate.getTime() - startDate.getTime();    
const durationInYears = durationInMilliseconds / (1000 * 60 * 60 * 24 * 365.25);     
const roundedDuration = Math.round(durationInYears * 2) / 2;     
return `${roundedDuration}`;

I've used echo to check all of the inputs (endmonth, etc), they're returned the expected results. I've used echo to check scriptcito, it returns NaN.

I've tried using advice from ChatGPT but it doesn't seem to understand that I'm not creating python code myself and is having me input things into Selenium which break that machine.

1

There are 1 best solutions below

0
On

An example of how to pass things to the browser:

value = driver.execute_script('''
  let [startMonth, startYear, endMonth, endYear] = arguments
  const startDate = new Date(startYear, startMonth - 1, 1)
  //... more code goes here
  return something
''', startMonth, startYear, endMonth, endYear)