Reputation: 3144
Update/Solution
I decided to rework the code a little. I ended up using pandas read_csv instead to open the urls.csv and iterated over the df columns with iterrows(). Everything is working now. Below is the updated code snippet.
df = pd.read_csv(urls, header=0, encoding="utf8", index_col=False)
for index, row in df.iterrows():
report_type = row[0]
report_name = row[1]
file_name = row[2]
download_report(report_type, report_name, file_name)
----
I am working on automation some report downloads using Selenium. I wrote the original python script that was too repetitive so I decided to combine thing into a function. This function navigates to a specific place in the system, generates a report by matching a name, downloads the report and moves/renames it.
def download_report(q_type, report_name, file):
driver.get(q_type)
driver.find_element_by_xpath("//select[@name='SavedQueriesDropDownList']/option[text()='%s']" % report_name).click()
driver.implicitly_wait(3)
driver.find_element_by_xpath("//input[@type='submit' and @value='Run Query']").click()
driver.implicitly_wait(3)
driver.find_element_by_id('exportsLinksDiv').click()
driver.implicitly_wait(3)
driver.find_element_by_id('ListToolbarRAWEXCELExportLink').click()
time.sleep(5)
filename = max([path + "\\" + f for f in os.listdir(path)], key=os.path.getctime)
print(filename)
os.rename(filename, out_path + file)
I have all of my data that the function needs in a csv file that includes three columns: q_type which it the starting URL path, report_name which tells the driver which report to select and file which is a file name that I want the downloaded file to be renamed as.
I am passing the needed value to the function with the following:
with open(urls, encoding="utf8") as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
report_type = row[0]
report_name = row[1]
file_name = row[2]
download_report(report_type, report_name, file_name)
When I run the script I get an error on the first line of the function driver.get(q_type):
Traceback (most recent call last):
File "C:/nf4.py", line 52, in <module>
download_report(report_type, report_name, file_name)
File "C:/nf4.py", line 10, in download_report
driver.get(q_type)
File "C:\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "C:\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
(Session info: chrome=76.0.3809.100)
For testing I printed out the value of q_type from the function and can confirm that it pulls the url from the csv file and it pulls it as a string. Really not sure where the error is coming from.
I am using the following driver setup:
# Setup Chrome Driver
chrome_path = r'C:\drivers\chromedriver.exe'
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : r'C:\data-in\raw'}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_path, options=chrome_options)
Upvotes: 6
Views: 11469
Reputation: 1591
I suspect that your q_type does not have a leading http:// (or https://) in front of the URL. That would cause the error message you are seeing. Can you verify if this is the case?
Upvotes: 16