简体   繁体   中英

Default download directory Edge web driver - python

I'm trying to change the download location of my web driver for Microsoft Edge but it does not seem to work.

I've tried looking at option for chrome and replicating it for Edge and this is what I got so far

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from msedge.selenium_tools import Edge, EdgeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time
from datetime import datetime

PATH =r"C:\Users\Username\Desktop\test\msedgedriver.exe"

options = EdgeOptions()
options.add_argument(r"download.default_directory=C:\Users\username\Desktop\test")

driver = Edge(PATH)

Please can someone advise?

Thank you.

It seems that your code has some issues. You can refer to the steps below to change the download path when automating Edge with Selenium:

  1. Download the correct version of Edge WebDriver from here . Make sure that the Edge WebDriver version is the same as the Edge browser version.

  2. Install the MS Edge Selenium tools using command below:

     pip install msedge-selenium-tools selenium==3.141
  3. Run the following sample python code to test:

     from msedge.selenium_tools import Edge, EdgeOptions options = EdgeOptions() options.use_chromium = True options.add_experimental_option("prefs", { "download.default_directory": r"D:\\Downloads" }) driver = Edge(executable_path=r"D:\\webdriver\\msedgedriver.exe", options=options) driver.get("https://www.seleniumhq.org/download/"); m = driver.find_element_by_link_text("32 bit Windows IE") m.click()

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

This is not a complete answer ie you will need to experiment a little because there are effects when setting this. Please also note I have had to type this, as I can't copy and paste, therefore a direct copy and paste from this may not work. These are suggestions:

  1. If I change the download folder as below then I find I get an annoying dialog box saying I am signed into Edge and can sync profile settings, with an OK button (and therefore no automatic way to get rid of the dialog). I can find no way of getting rid of this apart from private browsing. So if I want to use SSO, I don't change the download folder to prevent this, if I am manually logging into a webapp I can change the download folder by using private browsing to avoid the dialog. I have searched and searched and cannot find an answer of setting prefs AND NOT having this dialog.

  2. use Selenium 4. msedge-selenium-tools is deprecated and only for selenium 3.x. selenium 4 supports the settings below. In anaconda be careful that by default conda install selenium will give you selenium 3 by default (unlike pip) so check the selenium docs for the conda command to get 4.

  3. imports (there are many more that are useful)

     from selenium import webdriver from selenium.webdriver.edge.options import Options as EdgeOptions from selenium.webdriver.edge.service import Service as EdgeService
  4. Other useful imports (see docs) ActionChains, expected_conditions, WebDriverWait, Keys, By, desired_capabilities,staleness_of and I also use webelement

  5. To set download directory:

     edge_options = EdgeOptions() edge_options.add_experimental_option("prefs", {"download.default_directory":download_path})
  6. You can add other options eg:

     edge_options.add_experimental_option("prefs, { "download.default_directory":download_path, "download.prompt_for_download": False, "download.directory_upgrade": True, "safebrowsing.enabled": True })
  7. There are also command line arguments for certain things, but these seem to take a single value. (I also can't yet tell whether you use a single dash, double dash or no dash as different posts give different answers. Example:

     options.add_argument("-inprivate") #private browsing with no windows sign in to edge
  8. Then instantiate the driver. Note that many posts use a now deprecated method of running the driver, you should use service and the options variable and then (example):

     myService = EdgeService(executable_path = 'path to driver') myDriver = webdriver.Edge(service=myService, options=edge_options) element = myDriver.find_element(By.ID, 'foo') #perform selenium functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM