簡體   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