Set User-Agent

Set User-Agent #

def set_user_agent(driver: TestUIDriver, value: str or None = None) -> None

Changes User-Agent header for outgoing requests and navigator.userAgent.

To reset User-Agent to its default value, pass None as the new value.
Setting User-Agent to an empty string will actually update the User-Agent to empty string.
def test(driver: TestUIDriver):
    # Example of setting custom user-agent

    # Open page and create screenshot with default User-Agent
    driver.navigate_to("https://www.google.com/")
    e(driver, "css", "body").wait_until_visible()
    driver.save_screenshot("default_user_agent.png")

    # Set custom User-Agent value and reload browser
    set_user_agent(driver, "Custom User Agent")
    driver.get_driver().refresh()
    e(driver, "css", "body").wait_until_visible()

    # Take screenshot with custom User Agent
    driver.save_screenshot("custom_user_agent.png")

    # Set empty user agent header
    set_user_agent(driver, "")

    # Wait for page to load and create screenshot
    driver.get_driver().refresh()
    e(driver, "css", "body").wait_until_visible()

    driver.save_screenshot("empty_user_agent.png")

    # Reset user agent header
    set_user_agent(driver, None)

    # Wait for page to load and create screenshot
    driver.get_driver().refresh()
    e(driver, "css", "body").wait_until_visible()

    driver.save_screenshot("reset_user_agent.png")
User-Agent will revert to original value in browser built-in pages and WebRTC dump.