2017-02-03 51 views
0

我的場景是:我不得不通過使用Selenium和Python 3.6編寫腳本來下載大量文件,現在,我必須使用相同的技術下載文件。如何使用硒更改chrome的默認下載路徑?

問題是這個腳本不會在我自己的電腦上執行。

是否有可能使用chrome webdriver獲取chrome的默認下載文件夾?

截至目前,我有這樣的代碼:

dlPth="C:\\Users\\genieelecpsim\\Downloads\\" 
nwPth="C:\\Users\\genieelecpsim\\Downloads\\Exports" 
     for file in os.listdir(dlPth): 
     if file.startswith("export") and file.endswith(".csv"): 
      print(str(years[i])+"-"+str(months[j])+"-"+str(days[k])) 
      newfile=os.path.join(nwPth,str(years[i]) +"-" +(str(months[j]) if months[j]>=10 else "0"+str(months[j]))+"-" +(str(days[k]) if days[k]>=10 else "0"+str(days[k])) +".csv") 
      shutil.move(os.path.join(dlPth,file),newfile) 
      print (newfile) 
       break 

我想在這裏做的是一樣的東西:

dlPth=# Chrome's default download directory 
nwPth=dlPth+"\\Export" 

這可能嗎?感謝您的答覆!

編輯:首先,感謝所有您的快速解答,似乎我的話題是重複的,但因爲我沒有使用與this one相同的配置,所以我想知道這種方法是否適用於py3 .6和Selenium 3.0.2 ...對不起,我不能直接評論你的答案,因爲我是新來的,但是要感謝大家!

+0

的可能的複製[在Python中設置Chrome首選項w/Selenium Webdriver](http://stackoverflow.com/questions/18026391/setting-chrome-preferences-w-selenium-webdriver-in-python) – JeffC

回答

0

答案並沒有真正幫助我,但無論如何感謝。我找到另一種方式使用OS庫這樣做:

# dlPth will be the path to the download directory of the current user (on the system) 
dlPth=os.path.join(os.getenv('USERPROFILE'), 'Downloads') 

# destPth will just be a directory where I'll put all my (renamed) files in. 
destPth=dlPth+"\\Exports\\" 

謝謝回答,我在這裏張貼的答案讓任何人都在尋找關於這個主題一定的幫助就能看到它

0

在啓動驅動程序時,您可以通過使用特定首選項更改下載文件夾。你應該設置這樣的:

("download.default_directory", yourWantedPath) 

不知道你是如何開始和配置驅動程序,以便幫不了你更多的代碼,但是這是你正在尋找的偏好。

你可以找到有用的東西here