Python Selenium Webdriver - 动态更改下载目录

标签 python selenium

要在定义 selenium webdriver 之前明确定义下载目录,我们使用以下代码:

chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "C:/data/cline"}
chromeOptions.add_experimental_option("prefs",prefs)
chromePath = "path to chromedriver"

driver = selenium.webdriver.chrome.webdriver.WebDriver(executable_path=chromePath, port=0,    chrome_options=chromeOptions, service_args=None, desired_capabilities=None,   service_log_path=None)

我想下载一些文件,每个文件到不同的(新创建的)目录。是否可以在定义驱动后更改下载目录?

最佳答案

我一直无法弄清楚如何做到这一点,并使用了变通方法。下面的解决方案只是移动您下载的文件,而不是即时更改 webDriver 下载目录。

ExperimentsWithCode Gave the Answer Here. Below is part of his solution

def move_to_download_folder(downloadPath, newFileName, fileExtension):
    got_file = False   
    ## Grab current file name.
    while got_file = False:
        try: 
            currentFile = glob.glob(DOWNLOAD_PATH+"*"+fileExtension)
            got_file = True

        except:
            print "File has not finished downloading"
            time.sleep(20)

    ## Create new file name
    fileDestination = downloadPath+newFileName+fileExtension

    os.rename(currentFile, fileDestination)

    return

关于Python Selenium Webdriver - 动态更改下载目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734399/

相关文章:

python - Python3 中字典的空间复杂度

python - headless 无尽滚动 Selenium

python - Selenium Webscraper Click 在 Morningstar 上不起作用

java - 如何使用 Selenium 在 Indeed 上单击“立即申请”按钮?

selenium - ChromeOptions 和 DesiredCapability 之间有什么区别

javascript - 查找多个元素

python - Python 的二分法如何工作?

php - XAMPP-python脚本执行

Python 2.7 从utf-8文件读写 "éèàçê"

python:数据清理 - 检测欺诈性电子邮件地址的模式