In Selenium automation scripts, setting a specific user agent for a browser window is essential for simulating device behavior and ensuring website rendering as expected. In this case, we aim to modify the user agent in Google Chrome to that of Microsoft Edge Mobile.
Here's a succinct solution:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from fake_useragent import UserAgent ua = UserAgent() user_agent = ua.edge options = Options() options.add_argument(f'--user-agent={user_agent}') driver = webdriver.Chrome(chrome_options=options) driver.get("https://www.google.co.in")
This code employs Python's popular fake_useragent library to fetch a random Edge user agent. The add_argument method then injects the user agent into the Chrome options. When the script executes, Chrome will launch with the specified user agent, opening up Google with an Edge Mobile identity.
Using this approach, you can effortlessly set any desired user agent in your Selenium automation scenarios, facilitating website testing across various devices and browsers.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3