2014-03-29 42 views
3

要明確界定的webdriver我們用下面的代碼中的硒之前定義下載目錄:Python中硒的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) 

我想了一些文件,每個下載到不同(新創建)目錄。定義驅動程序後可以更改下載目錄嗎?

+0

可能重複[如何webdriver的同時運行更改默認的下載文件夾?(http://stackoverflow.com/questions/23896625/how-to-change-default-download-文件夾,而webdriver正在運行) –

回答

-1
driver.set_preference("download.default_directory", "path/") 

試試這個變種。

+0

試過這個,並得到這個錯誤:AttributeError:'WebDriver'對象沒有屬性'set_preference' – user3294195

+0

對不起,沒有回頭... – Wolkodav

+0

然後你不能。 – Wolkodav

0

我一直無法弄清楚如何做到這一點,並使用瞭解決方法。下面的解決方案不是直接更改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