Execute existing function by Selenium-webdriver`executaeScript`method

Asked

Viewed 1,035 times

1

I am trying to run a function in the browser that is already set, but it is marked as undefined.

I ran the following code to reproduce the problem:

const webdriver = require("selenium-webdriver")
const driver = new webdriver.Builder().forBrowser("firefox").build()

driver.navigate().to("http://localhost/site")
driver.sleep(3000)

const code = `console.log(window.sayHello)`
const logCode = `console.log("${code}")`

driver.executeScript(logCode)
driver.executeScript(code)

When I tried to run the same code - console.log(window.sayHello) - directly in the browser still open, it returned the desired function, as in the image:

Log no navegador


EDITED

This seems to be a specific bug with the firefox driver as Chrome worked. I believe the variable window is being superscript, but I couldn’t rewrite it with the window original.

  • You’re encapsulating twice, that’s what you want? driver.executeScript(console.log("console.log(window.sayHello)"))? Won’t you want to: driver.executeScript(code); only?

  • @Sergio the first line only displays the code that will be executed

  • The . executeScript log content or return? tests console.log('foo');, gives foo or undefined?

  • @Sergio edited the question code to try to make it simpler. Basically, the first command executeScript only displays the code that will run on the second call.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.