Closed picciama closed 3 years ago
I think that this issue is more to do with how the Firefox console and the page interact as I have tried to recreate this in the pure page or pure JS sense. see e9c738de8a587dc6bc86b55949c57b8574ec68bb
@whimboo do you know what could cause this issue?
I think that this issue is more to do with how the Firefox console and the page interact as I have tried to recreate this in the pure page or pure JS sense. see e9c738d
Does that mean you were able to reproduce it? I was able to reproduce it using your example. Maybe sth. specific related to Firefox on Debian? I'll come back to you after trying to reproduce it on a Windows machine.
I wasn't able to reproduce it... I wonder If it is because of an issue with that version of Firefox as it is really really old (release branch from Mozilla is on 88)
I can reproduce this with Windows 10 and Firefox 88.
I noticed though that we might have a misunderstanding here:
While you created a global const cheese
, you didn't call it as you created a local const within the scope of execute_script
: https://github.com/SeleniumHQ/selenium/commit/e9c738de8a587dc6bc86b55949c57b8574ec68bb#diff-6d75ecf37273e7973aeac782de0ca2876d6cfaa7b36971f811b385cc7dcb7a61R288
This is working as expected but it doesn't access the global const.
Your second attempt in which you call the global function makeMeA
is working too, as it is a globally defined function that's already in the document. If it isn't your website though, you annot just modify the served HTML of course an add this function.
What doesn't but should work is this: driver.execute_script("return cheese")
which should return the global const cheese
, I'm very sure you will be able to reproduce it.
I found a workaround though:
driver.execute_script("""
let scr = document.createElement('script');
scr.id='injected_function';
scr.innerHTML='function makeMeA(sandwich){return cheese + sandwich}';
document.head.appendChild(scr);
"""
)
driver.execute_script("return makeMeA('sandwich');")
This way, I can inject javascript by adding a child script to the document head. This is equivalent to your example in which this function is already defined in the HTML.
What remains though are the following questions:
driver.execute_script("return cheese")
work (and why does it in chromedriver)?execute_script
?As I see it, global consts cannot be accessed directly from within execute_script
at the moment, even with the newest Firefox and geckodriver versions.
So after speaking to the Mozilla team there appears to be https://github.com/mozilla/geckodriver/issues/1067 that is raised for this issue. There are links to Bugzilla from there so there is not much we can do from the Selenium side.
🐛 Bug Report
Global objects that are accessible via Firefox Console cannot be accessed with selenium. This only affects
const
it seems, i.e.var
are visible to selenium. When trying to return these values using theexecute_script
function, a JavascriptException is thrown. The issue is specific to using the geckodriver. It works as expected using the chromedriver.To Reproduce
In Python, create a driver instance and navigate to any website.
Add const via Firefox console:
In Python, execute the following:
The exception raised:
Implications:
Expected behavior
driver.execute_script("return test_val")
should return the consttest_val
. It should not result in an exception.Environment
OS: Debian Buster Browser: Firefox Browser version: 78.10.0esr (64-bit) Browser Driver version: GeckoDriver 0.29.0 Language Bindings version: Python 3.9.4, selenium 3.141.0 selenium-wire 4.2.4 Selenium Grid version (if applicable): -