繁体   English   中英

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

[英]Default download directory Edge web driver - python

我正在尝试更改 Microsoft Edge 的 web 驱动程序的下载位置,但它似乎不起作用。

我已经尝试查看 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)

请问有人可以建议吗?

谢谢你。

看来你的代码有一些问题。 在使用 Selenium 自动化 Edge 时,您可以参考以下步骤更改下载路径:

  1. 这里下载正确版本的 Edge WebDriver。 确保 Edge WebDriver 版本与 Edge 浏览器版本相同。

  2. 使用以下命令安装 MS Edge Selenium 工具:

     pip install msedge-selenium-tools selenium==3.141
  3. 运行以下示例 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()

    注意:将代码中的路径更改为您自己的路径。

这不是一个完整的答案,即您需要进行一些实验,因为设置它时会产生影响。 另请注意,我必须输入这个,因为我不能复制和粘贴,因此直接复制和粘贴可能不起作用。 这些是建议:

  1. 如果我按如下方式更改下载文件夹,然后我发现我收到一个恼人的对话框,说我已登录 Edge 并且可以同步配置文件设置,并带有一个 OK 按钮(因此没有自动摆脱对话框的方法)。 除了私人浏览之外,我找不到摆脱这种情况的方法。 因此,如果我想使用 SSO,我不会更改下载文件夹来防止这种情况发生,如果我手动登录到 web 应用程序,我可以通过使用隐私浏览来更改下载文件夹以避免对话框。 我已经搜索和搜索,找不到设置首选项的答案并且没有这个对话框。

  2. 使用 Selenium 4. msedge-selenium-tools 已弃用,仅适用于 selenium 3.x。 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.

  3. 进口(还有更多有用的)

     from selenium import webdriver from selenium.webdriver.edge.options import Options as EdgeOptions from selenium.webdriver.edge.service import Service as EdgeService
  4. 其他有用的导入(参见文档)ActionChains、expected_conditions、WebDriverWait、Keys、By、desired_capabilities、staleness_of,我也使用 webelement

  5. 设置下载目录:

     edge_options = EdgeOptions() edge_options.add_experimental_option("prefs", {"download.default_directory":download_path})
  6. 您可以添加其他选项,例如:

     edge_options.add_experimental_option("prefs, { "download.default_directory":download_path, "download.prompt_for_download": False, "download.directory_upgrade": True, "safebrowsing.enabled": True })
  7. 对于某些事情,还有命令行 arguments,但这些似乎采用单个值。 (我也无法判断您是使用单破折号、双破折号还是不使用破折号,因为不同的帖子给出不同的答案。例如:

     options.add_argument("-inprivate") #private browsing with no windows sign in to edge
  8. 然后实例化驱动程序。 请注意,许多帖子使用现在已弃用的驱动程序运行方法,您应该使用 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.

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