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):
    # Example of setting custom user-agent

    # Open page and create screenshot with default User-Agent
    driver.navigate_to("http://www.xhaus.com/headers")
    e(driver, "css", "body").wait_until_visible(seconds=10)
    driver.save_screenshot("default.png")

    # Set custom user agent
    set_user_agent(driver, "Custom User Agent")

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

    driver.save_screenshot("changed.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(seconds=10)

    driver.save_screenshot("empty.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(seconds=10)

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