pythonpython-3.xsubprocessmicrosoft-edgepython-os

Unable to open Edge Browser using custom user agent and profile using command arguments in subprocess.call()


I am trying to open Microsoft Edge using mobile agent and profile, but am unable to. The Microsoft Edge does open but still uses default string. I have tried various methods to do it but none works. I am able to open Edge with desired profile though.

These are what I tried:

Putting whole target into one line.

subprocess.call('C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --profile-directory="Profile 3" --user-agent="Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; SCH-I535 Build/KOT49H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"')

And by passing an array of executable and two arguments.,

subprocess.call(['C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe', '--profile-directory="Profile 3"', '--user-agent="Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; SCH-I535 Build/KOT49H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"'])

As well as passing variables storing the arguments:

edgeLink = 'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe'
profile3 = '--profile-directory="Profile 3"'
userString = '--user-agent="Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; SCH-I535 Build/KOT49H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"'

subprocess.call([edgeLink, profile3, userString])

But nothing seems to work for me. I have tried looking online but nothing seems to help my use case. I did find one article that said, Google chrome has remove user string, but I am not sure if that is what affecting me.


Solution

  • Actually, this line:

    subprocess.call('C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --profile-directory="Profile 3" --user-agent="Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; SCH-I535 Build/KOT49H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"')
    

    works fine to use custom user agent and profile. You just need to kill all the msedge.exe processes to activate the command line options. You can try the following command to kill them:

    taskkill /f /im msedge.exe
    

    And here's my user agent after running subprocess.call(): enter image description here