简体   繁体   English

默认下载目录 Edge web 驱动 - python

[英]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.我正在尝试更改 Microsoft Edge 的 web 驱动程序的下载位置,但它似乎不起作用。

I've tried looking at option for chrome and replicating it for Edge and this is what I got so far我已经尝试查看 chrome 的选项并将其复制到 Edge,这就是我到目前为止所得到的

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:在使用 Selenium 自动化 Edge 时,您可以参考以下步骤更改下载路径:

  1. Download the correct version of Edge WebDriver from here .这里下载正确版本的 Edge WebDriver。 Make sure that the Edge WebDriver version is the same as the Edge browser version.确保 Edge WebDriver 版本与 Edge 浏览器版本相同。

  2. Install the MS Edge Selenium tools using command below:使用以下命令安装 MS Edge Selenium 工具:

     pip install msedge-selenium-tools selenium==3.141
  3. Run the following sample python code to test:运行以下示例 Python 代码进行测试:

     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).如果我按如下方式更改下载文件夹,然后我发现我收到一个恼人的对话框,说我已登录 Edge 并且可以同步配置文件设置,并带有一个 OK 按钮(因此没有自动摆脱对话框的方法)。 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.因此,如果我想使用 SSO,我不会更改下载文件夹来防止这种情况发生,如果我手动登录到 web 应用程序,我可以通过使用隐私浏览来更改下载文件夹以避免对话框。 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. msedge-selenium-tools 已弃用,仅适用于 selenium 3.x。 selenium 4 supports the settings below. selenium 4 支持以下设置。 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. 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其他有用的导入(参见文档)ActionChains、expected_conditions、WebDriverWait、Keys、By、desired_capabilities、staleness_of,我也使用 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.对于某些事情,还有命令行 arguments,但这些似乎采用单个值。 (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):请注意,许多帖子使用现在已弃用的驱动程序运行方法,您应该使用 service 和 options 变量,然后(示例):

     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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python selenium 3 Edge web 驱动程序错误 - Error in python selenium 3 Edge web driver 使用 Python selenium 更改边缘的下载目录 - Changing download directory for edge using Python selenium 使用 python selenium 下载文件,使用 firefox 驱动程序正确下载目录 - Download file with python selenium, correct download directory with firefox driver Python Edge 驱动程序 Web 自动化帮助 - 找不到 Xpath - Python Edge Driver Web Automation Help - Cannot find Xpath 如何处理在 python 中的 selenium web 驱动程序中单击“下载键”按钮? - How to handle click on “Download key” button in selenium web driver in python? Python Dell驱动程序下载 - Python Dell driver download Selenium Python 在VPS中设置Chrome默认下载目录 - Selenium Python Set Chrome Default Download directory in VPS 带有 MS Edge 浏览器的 Python (Selenium):'Connection aborted.', ConnectionResetError(10054, ...)“Microsoft Web Driver 已停止工作” - Python (Selenium) with MS Edge browser: 'Connection aborted.', ConnectionResetError(10054, ...) "Microsoft Web Driver has stopped working" 如何使用Selenium Web驱动程序python单击网页中目录列表的不同链接 - how to click on different links of directory listing in a web page using selenium web-driver python 如何确定哪个 MS Edge 驱动程序与我的操作系统版本兼容,以启用 web 自动化和 Python 的 Selenium? - How do I determine which MS Edge Driver is compatible with my OS Version to enable web automation with Selenium for Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM