Search code examples
pythonseleniumwebdrivergeckodriverhttp-referer

What's the difference between `driver.execute_script("...")` and `driver.get("javascript: ..."` with geckodriver/Firefox?


I think, this question concerns the internal workings of Selenium. In another post Referer missing in HTTP header of Selenium request it becomes apparent that there is a difference between running

driver.execute_script("window.location.href = '{}';".format(url))

and

driver.get("javascript: window.location.href = '{}'".format(url))

The latter command will send Referer header with the request, the former will not.

Doesn't matter at this point if this is desired behavior or a bug and Referer should be sent by both commands. Also, window.location.href = ... is only an example.

Yet, obviously, there must be a difference between running JavaScript with command driver.execute_script("...") and driver.get("javascript: ..." if they don't produce the same result. So the question is more about the fact that both commands don't invoke the same Selenium code internally.

What is the difference between both commands?


Solution

  • The answer to your question depends on the browser your driver is running. Selenium itself does not implement these functionalities - it merely invokes the underlying driver's API.

    Take a look at the source of WebDriver.execute_script and WebDriver.get - they both just call self.execute, which performs a request to the webdriver.

    Chrome, for example, does not support 'javascript:' urls with WebDriver.get since 2013, as can bee seen in chromium's webdriver implementation.

    The actual difference between directly running a JS script and navigating to a 'javascript URL' is embedded deep in each browser's implementation, and might be not very straightforward. A possible reason for the difference you mentioned could be an implementation detail - maybe the browser (that was used when the results you mentioned were produced) only sends a Referer header when it is in the context of a high level navigation command (driver.get), and therefore did not include one on a plain javascript-triggered navigation.