Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
Why aren't my polyfills being loaded properly in webdriver Firefox?

I have this polyfill in JS for my site:

if (!Date.prototype.toSQLDate) {   
    console.log('Adding my custom polyfill');                                                                                                                                                                                                       
    Date.prototype.toSQLDate = function () {                                                                                                                                                                                 
        // relevant code snipped, but assume:
        // return some string like '2000-01-01';
    }                                                                                                                                                                                                                                         
}

When I load the website manually, I see the console log and I can do:

var x = new Date(2000, 0, 1);
console.log(x.toSQLDate()) => '2000-01-01'

This works both in Chrome/Chromium(96) and Firefox(95+)

However, when I run my automated tests against this website, in Firefox(95) only (Chrome works fine) I get this when attempting to run the same code via webdriver.execute_script

from selenium import webdriver
wd = webdriver.Firefox()
wd.get(...myurl)
wd.execute_script('var x = new Date(2000, 0, 1); return x.toSQLDate()')
>>>>
selenium.common.exceptions.JavascriptException: Message: TypeError: x.toSQLDate is not a function

What? If I watch the automated test with the console open, I can see my console.log telling me that it successfully added the polyfill. I can interact with the browser in the console and do the exact same logic and get a valid string back. But when webdriver executes it, it's not in the same js context? And only in Firefox.

This behavior is present in both Python2.7/selenium3 and Python3.8/selenium4

What is going on here? Do I need to enable some sort of sandbox js escape in Firefox?

Additional Information:

It appears that there may be some "quirk" in the way firefox/geckodriver treats globals. Consider this simple script:

from selenium import webdriver
ff = webdriver.Firefox()
ff.get('http://www.google.com')
ff.execute_script('return Date === window.Date') => False
ff.execute_script('console.log(Date === window.Date)') => false

# But this same script shows true for chrome.
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda