Webdriver: Change file name before it's saved to the folder - python

When I use webdriver to browse the website, it will automatically download the file to my folder, but the file name is "Bike.gz", is it possible to change its name to "{current time}.txt" before save it to my folders? For example, as "2019-03-07-11-46.txt".
Code:
options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {
"download.default_directory": r"\\xxx.xx.xxx.xx\bike_test",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
driver_main = webdriver.Chrome(chrome_options=options)
driver_main.get("http://data.xxxx/xxxx")
Also, "\xxx.xx.xxx.xx\bike_test" is my NAS path, if I run it on AWS, how can I download the file and save it directly to my NAS folder bike_test? Or do I have to save it in my AWS folder first and transform it after?

Related

How to change the download path of a selenium docker

I have a selenium docker on a UNRAID server, I tried to change the download path like this
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("prefs", {
"download.default_directory": config.path,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
chrome_options.add_argument('disable-popup-blocking')
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument('disable-dev-shm-usage')
chrome_options.add_argument('disable-browser-side-navigation')
driver = webdriver.Remote
command_executor="http://IP:4444"
options=chrome_options
)
Unfortunately, nothing changes, the download directory is the default directory.
Did I make something wrong?

Using Selenium with Python in Chrome to click "download" button and download PDF

I am trying to download a PDF from the following url,https://sec.report/Document/0001670254-20-001152/
There is a download button embedded in the html. I am using the following code to click the button and send the download to my desktop as defined in my path. The program runs without any errors but the PDF does not show up in the desktop. I have tried changing the location to different places, ie Downloads. I have also toggled the preferences in google chrome to download PDF files instead of automatically opening them in Chrome. Any ideas?
from selenium import webdriver
download_dir = "C:\\Users\\andrewlittle\\Desktop"
options = webdriver.ChromeOptions()
profile = {"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}],
"download.default_directory": download_dir , "download.extensions_to_open": "applications/pdf"}
options.add_experimental_option("prefs", profile)
chromedriver_path = os.getcwd() + '/chromedriver'
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://sec.report/Document/0001670254-20-001152/document_1.pdf')
driver.close()
Thanks in advance!
See the answer below:
import time
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
download_dir = "/Users/test/Documents/"
options = Options()
options.add_experimental_option('prefs', {
"download.default_directory": download_dir,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True
}
)
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://sec.report/Document/0001670254-20-001152/document_1.pdf')
time.sleep(3)
driver.quit()
I put the time.sleep in for some security in case the file takes a little longer to download. However, it is not necessary.
I also used the newer, Service and Options objects for Selenium.
The key to the code is the use of,
"download.default_directory": download_dir,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True
These allow for Chrome to download the PDF without prompt to the directory of your choice.

Python Selenium Chromedriver how to download pdf without opening file explorer?

I'm trying to figure out a way to download this file at a click of a button (right now it just opens file explorer). I'm looking for a way to make it immediately download into a folder. Here is my code, any help is appreciated. Thank you!
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
"download.default_directory": r"C:\Users\jhak\downloads", #Change default directory for downloads
"download.prompt_for_download": False, #To auto download the file
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True,
"profile.default_content_setting_values.notifications":2,
"download.extensions_to_open": "applications/pdf"
#It will not show PDF directly in chrome
})
options.add_argument("--disable-notifications")
global chrome_browser
chrome_browser = webdriver.Chrome(r'C:\Users\rlaceste\Desktop\chromedriver.exe', options = options)
chrome_browser.maximize_window()
#element that holds download link
chrome_browser.find_element_by_id('element here').click()

Python selenium chrome doesn't download with driver.get(url)

This code is supposed to download the sample pdf file but it only displays.
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {
"download.default_directory": r"/Users/ugur/Downloads/",
"download.prompt_for_download": True,
"download.directory_upgrade": False,
"safebrowsing.enabled": True
})
driver = webdriver.Chrome(executable_path="/Users/ugur/Downloads/chromedriver",chrome_options=options)
driver.get('http://www.africau.edu/images/default/sample.pdf')
This is a demonstration, real website is different and it requires authentication so after running the initial part of the code I manually enter username and password and then run a for.
On your computer, open Chrome.
Navigate to chrome://settings
Go to advance settings.
Under “Privacy”, click Content settings.
Under “PDF Documents," check the box next to "Download PDF files instead of automatically opening.”

However to convert string value to raw string as download path for selenium chrome driver

Anyone know how to transfer string variable to raw string? I try to write small program to click download button to download from web by using selenium drive. I need to use variable for "download.default_directory" value
if i set download.default_directory value as '/User/xxxx' I am able to see correct download path on chrome driver and I can see the download file from path. However
if I change value to
path = '/User/xxxx'
"download.default_directory": repr(path)
download path in chrome become '/User/xxxx' the incorrect path
from pago.driver import WebDriver
import os
from selenium import webdriver
options = webdriver.ChromeOptions()
path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
path = '/User/ycc/'
options.add_experimental_option("prefs", {
"download.default_directory": '/User/xxx',
#"download.default_directory": repr('/User/xxx'), -> failed
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
driver = WebDriver(desired_capabilities={'browserName': 'chrome'}, options=options)
driver.get('https://www.docker.com/get-started')
locator = '//a[.="Download for Mac"]'
time.sleep(3)
button = driver.find_element_by_xpath(locator)
button.click()

Categories