Selenium Chromedriver未下载到指定目录

2024-06-16 14:57:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图使用seleniumFanGraphs Leaderboards下载一些数据。我是用Firefox来实现的,但是Chrome的速度快了一点,所以我试着切换到那个。在Firefox中,下载文件是可行的,但我在切换到Chrome时遇到了麻烦

设置Chrome

chrome_options = webdriver.ChromeOptions()
chrome_options.headless = False
os.makedirs("dist", exist_ok=True)
preferences = {
    "profile.default_content_settings.popups": 0,
    "download.default_directory": "dist/",
    "directory_upgrade": True
}
chrome_options.add_experimental_option(
    "prefs", preferences
)
self.browser = webdriver.Chrome(
    chrome_options=chrome_options
)

导出数据

while True:
    try:
        WebDriverWait(self.browser, 20).until(
            expected_conditions.element_to_be_clickable(
                (By.ID, "LeaderBoard1_cmdCSV")
            )
        ).click()
        break
    except exceptions.ElementClickInterceptedException:
        self.__close_ad()

每当我为模块运行测试时,CSV文件都会出现在C:/Users/UserDir/Downloads,而不是当前工作目录中的dist/文件夹中。我仔细检查了dist/文件夹是否存在,它确实存在

规格

  • Python v3.9
  • seleniumv3.141.0
  • 铬驱动v89.0.4389.23
  • 谷歌浏览器v88.0.4324.190

Tags: 文件数据selfbrowser文件夹truedefaultdist
1条回答
网友
1楼 · 发布于 2024-06-16 14:57:06

我也有同样的问题,我也解决了

option.add_experimental_option("prefs", {'download.default_directory': f"{download_path}",
                                             'download.prompt_for_download': False,
                                             'download.directory_upgrade': True})

目录\u升级可能会错过下载。之前

复制并粘贴这个

preferences = {
    "profile.default_content_settings.popups": 0,
    "download.default_directory": "dist/",
    "download.directory_upgrade": True
}

相关问题 更多 >