Home >Backend Development >Python Tutorial >How Can I Simulate Microsoft Edge Mobile in Google Chrome with Selenium?

How Can I Simulate Microsoft Edge Mobile in Google Chrome with Selenium?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-30 08:04:02646browse

How Can I Simulate Microsoft Edge Mobile in Google Chrome with Selenium?

Altering User Agent in Google Chrome using 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn