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

Mary-Kate Olsen
Release: 2024-10-30 08:04:02
Original
597 people have browsed it

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")
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!