Recommand · October 4, 2021 0

geckodriver – cannot create a js variable then read it?

I’m using geckodriver.exe for some Selenium automation with Firefox. There are times when I need to make a global variable using ExecuteScript, then read that variable later on:

driver.ExecuteScript("  var x = 'test'; ");
var result = driver.ExecuteScript("  return x; ");

In ChromeDriver I get a result back and it is "test" as expected.

With GeckoDriver I get null on that second call. Is there something I am doing wrong? I really do intend to create a global variable here!