【问题标题】:Can't download file through Selenium chrome webdriver in Python无法通过 Python 中的 Selenium chrome webdriver 下载文件
【发布时间】:2020-03-01 19:17:10
【问题描述】:

我正在使用 Selenium webdriver 抓取网站并将文件从网站下载到文件夹。由于某种原因,如果我通过ChromeOptions()更改chrome的默认下载路径,当我click()下载链接时,文件在窗口底部的下载栏中显示failed - download error。我确保 chrome 和 chrome 驱动程序是最新的,但它仍然无法正常工作。

这是我用来更改默认下载路径的代码。

from selenium import webdriver

download_path = select_directory()
options = webdriver.ChromeOptions()
preferences = {"download.default_directory": download_path}
options.add_experimental_option("prefs", preferences)
browser = webdriver.Chrome(chrome_options=options)

这是我用来下载文件本身的代码。

from selenium import webdriver

table = browser.find_element_by_id("tblDocs")
documents_info = table.find_elements_by_class_name("clsTableRowNormal")
for document in documents_info:
    str = document.find_element_by_class_name("clsTableCell").text
    if str.find("תשריט") > -1:
        document.find_element_by_tag_name("img").click()

有人知道如何将文件下载到特定目录吗? 我想也许将文件下载到默认文件夹并将文件从那里移动到我想要的路径,但我不知道如何通过 selenium 获取默认文件夹。 任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 用元素的相关 HTML 更新问题。
  • 试试这个希望它有帮助:stackoverflow.com/questions/18026391/…
  • downLoadFolder =os.path.join( os.getenv('USERPROFILE'), 'Downloads') 这将为您提供默认的下载文件夹路径。

标签: python-3.x selenium selenium-webdriver


【解决方案1】:

显然 selenium 无法下载到路径字符串中带有 / 的路径。 我不得不更改select_directory() 以仅使用\\ 返回文件路径字符串。 感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2016-03-27
    • 1970-01-01
    • 2017-08-12
    相关资源
    最近更新 更多