0

I must start by saying that I am relatively new to python so some of my questions might be very basic.

I deliberately made my question very similar to another question on stackoverflow since I got most of the information that lead me to get working code, from there. That thread did not solve all my problems and has some answers which seem to conflict with other answers I found on stackoverflow. I found a lot of threads here that seemed contradictory about how the selenium chromedriver should be imported and used. I assume that some of it is due to the age of the threads (some dating back to 2012) and others due to me not fully understanding how there are more than one way to do the same thing in python.

I now have code that does everything I want it to do but I do not really understand how all of it works or if it is the pythonic way to do it. I would appreciate if somebody could answer my questions so that I can improve my python skills.

I use tkinter for a user interface and selenium for my web browser.

from tkinter import Tk     # from tkinter import Tk for Python 3.x
from tkinter.filedialog import askdirectory
from tkinter import ttk
import webbrowser
from selenium import webdriver # https://selenium-python.readthedocs.io/api.html
from selenium.webdriver.chrome.options import Options

My python code starts by asking the user to select a folder where the file must be downloaded.

root = Tk()
init_folder = "C:\\temp"  # Folder where to open GUI
# Ask user to select a folder where to save all data
root.wm_attributes('-topmost', 1)
save_folder = askdirectory(initialdir = init_folder,
                    title = "Please Select Folder To Save Data In")

I later use this selected folder to set the default download path for selenium Chrome. I found that if I use this path, the downloads would fail every time even though I could see in the browser settings that the download path was set according to save_folder. After reading a warning on the chromium.org website about relative paths not always working in Windows, I added code to replace the / characters in the user selected path with \. After this coded was added my downloads succeeded every time. I found that the selenium chrome browser also failed to download the files when I hard coded the default browser download folder with / instead of \.

save_folder = save_folder.replace('/','\\')
print(save_folder)

I often create python code that asks the users to select a folder where to save files. Should I always replace the / character with \ to be sure that my python code will execute correctly?

My code next sets the selenium chrome options, such as the default download folder and the option to not ask the user before downloading the files. I tested a lot of code I found on the internet before I got to the following code that works.

options = webdriver.ChromeOptions()
options.add_experimental_option('prefs',{'download.prompt_for_download': False,
                                         'download.default_directory':save_folder})
driver = webdriver.Chrome(options=options)
# Print chrome options to check what is set
print(webdriver.chrome.options.Options.experimental_options.fget(options))

The code in this thread looks as follows:

chrome_options = Options()
chrome_options.add_experimental_option("download.prompt_for_download", "false")
chrome_options.add_experimental_option("download.default_directory", "/tmp")
driver = webdriver.Chrome(chrome_options=chrome_options) # chrome_options has been deprecated

The code that I use makes use of a dictionary to set the preferences while the code above looks like it does not. Is the code above just a different way to set the values inside the prefs dictionary of options?

I found several comments on forums where people stated that chrome_options has been deprecated.

I used the following print function to double check if my browser preferences were set correctly. print(webdriver.chrome.options.Options.experimental_options.fget(options))

My code then opens a web page and clicks on a download button. When I have \ characters in my browser preferences download path, the download fails. When I have \ characters, the download succeeds.

export_button = driver.find_element_by_name('CSVExport')
    if export_button != None:
        export_button.click()

0 Answers0