Home > Article > Backend Development > How Can I Simulate Microsoft Edge Mobile in Google Chrome with Selenium?
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.
The above is the detailed content of How Can I Simulate Microsoft Edge Mobile in Google Chrome with Selenium?. For more information, please follow other related articles on the PHP Chinese website!