1

I need to change the clock of the Playwright Python browser. I know this can be done in JavaScript, but is there a way to do this with the Python Playwright library? I've tried converting the JavaScript code to Python but it doesn't work.

hardkoded
  • 18,915
  • 3
  • 52
  • 64
KiwiNFLFan
  • 59
  • 5

1 Answers1

1

You can use the evaluate-method to execute Javascript on the page:

import playwright

def execute_js(page):
  page.evaluate(
    """
    page.evaluate(() => {
      Date.now = () => {
        return 1609477200000; //Jan 1, 2021
      };
    });
    """
  )

with playwright.create_browser().new_page() as page:
  execute_js(page)
  print(page.evaluate("Date.now()"))

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37