Mobile Emulation from msedge driver (Selenium)

Luc Havelson 1 Reputation point
2021-08-26T09:04:04.89+00:00

I was reading this page : https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/capabilities-edge-options
about how to use MS Edge's web driver, and among the capabilities of the driver, there seems to be an option for mobile emulation.

I know this option is available during regular use of the browser via devtools, but I can't get it to work using the automated web driver with selenium.

The page above states that information about the device should be passed via a dictionary in EdgeOptions, either by specifying the name of the device, or its metrics (screen size) and user agent. I tired both of these and couldn't get a result.

Any idea of what I might have missed to get this to work ?

Here's my code in python if you are interested :

mobile_emulation_specs = {  
"deviceMetrics" : "425px",  
"userAgent" : "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Mobile Safari/537.36 Edg/92.0.902.78"  
}  
  
mobile_emulation_name = {  
"deviceName": "Pixel 2"  
}  
  
options = EdgeOptions()  
  
options.use_chromium = True  
options.set_capability('mobileEmulation', mobile_emulation_specs) #doesn't work  
options.set_capability('mobileEmulation', mobile_emulation_specs) #doesn't work either  
  
driver = Edge(options = options, executable_path = edge_path)  
driver.get(url)  

Thank you very much in advance.

Microsoft Edge
Microsoft Edge
A Microsoft cross-platform web browser that provides privacy, learning, and accessibility tools.
2,173 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XuDong Peng-MSFT 10,181 Reputation points Microsoft Vendor
    2021-08-27T05:31:10.983+00:00

    Hi @Luc Havelson ,

    According to your description, you can try to use add_experimental_option to achieve your requirements, here is a simple sample code(part of your code):

    from msedge.selenium_tools import Edge, EdgeOptions  
      
    options = EdgeOptions()  
    options.use_chromium = True  
      
    mobile_emulation = {  
        "deviceMetrics": { "width": 375, "height": 812, "pixelRatio": 3.0 },  
        "userAgent" : "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Mobile Safari/537.36 Edg/92.0.902.78"  
    }  
    options.add_experimental_option("mobileEmulation", mobile_emulation)  
    driver = Edge(executable_path = r"C:\Users\Administrator\Desktop\msedgedriver.exe",options = options)  
      
    driver.get('https://www.google.com')  
    

    Note: You need to replace the executable_path according to your own situation, and ensure that the mobile device parameters are correct.

    And this is result:
    Y0AQ9.png

    Best regards,
    Xudong Peng


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.