Warm tip: This article is reproduced from serverfault.com, please click

Edge automation with Selenium

发布于 2020-12-07 15:03:56

I want to open e.g. Google page with Edge using Selenium and Python inside company network.

from selenium import webdriver
browser = webdriver.Edge(r"C:/_path_to_driver_/msedgedriver.exe")
browser.get("https://www.google.com")

Edge opens this site and asks me to insert my mail. enter image description here

After entering mail, it redirects me to this page: enter image description here

I have to click always manually on OK. Selenium is not able to click on that OK. Any idea how to perform a click? OR there is way how to store my mail address or certificate to not being always ask for it? This issue occurs only with the Edge controlled by selenium. Default Edge remembers all settings.

Questioner
kabarto
Viewed
0
Yu Zhou 2020-12-08 13:35:31

If we use selenium webdriver to automate Edge without any arguments, it will create a new profile every time instead of using the existing user data profile. That's why it asks for credential every time.

As a workaround, you can use user-data-dir and profile-directory to use specific profile to launch Edge using Selenium WebDriver. You can use the profile that stores the credential so that it won't ask for credential when you automate Edge. I think in your case it's the default Edge profile.

You need to install the MS Edge Selenium tools using command pip install msedge-selenium-tools selenium==3.141 then refer to the sample code below:

from msedge.selenium_tools import Edge, EdgeOptions

edge_options = EdgeOptions()
edge_options.use_chromium = True

#Here you set the path of the profile ending with User Data not the profile folder
edge_options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Microsoft\\Edge\\User Data"); 
#Here you specify the actual profile folder
edge_options.add_argument("profile-directory=Profile 2");

driver = Edge(options = edge_options, executable_path = r"C:\_path_to_driver_\msedgedriver.exe")
driver.get('https://www.google.com')

Note: Change the paths in the code to your owns.

If you don't know the path of the specific profile, you could check edge://version/ like below:

enter image description here