Using python to download a file after clicking the generate icon - python

I am working on a project. Each day I need to login to a website. Navigate to a report page. After entering the require information, and clicking on the "gererate report" icon, there's a pop up windown asking me to save the file. I want to do it automatically by python.
I search the internet for a couple of days, and now I can do it by Autoit, but can't find a way in python.
By using urllib2, I can process up to clicking the "generate report" icon. But how can I get the name and location of the file and download it?
I want to make clear that the "generate report" button is actuary the submit button of a form. There is no Href associate with the generate button. After submitting the form, a file-save dialog popup asking me where to save the file.
thanks in advance!

If it's a save file dialog from Fx it probably means that it directly links to a file. If you manage to fill all the information and click the generate_report icon, can't you check which href has the generate_button assigned to it?
import urlib
urlfile = "Href of the generate_button"
urllib.urlretrieve(urlfile,"filename")

Related

Python with Webbot, not sure how to click the button of the chrome PDF viewer

I have a Python app navigating through a website using Webbot. On the final page, it renders and streams a PDF to the browser (without an endpoint URL). This is displayed in the chrome PDF viewer but I need to download this.
I am unsure of how to go about activating the download here or obtaining this file through the normal method of request.get()
The URL is generic: www.website.com/generatePDF
chrome viewer showing download button
I can navigate to this page, I'm just not sure how to go about getting the actual PDF downloaded. Because everything uses scripting on the backend, I need to navigate button clicks (URL's are hidden).
Thoughts?
Have you consider sending CTRL+S command? It should send the save command and then click on SAVE button. Never tried but could do the job.

Avoid OS pop-up while uploading file with Selenium in Python

I am using selenium to upload a file. This is a problem because the upload button makes a file explorer window appear which I can't automate to my knowledge. How can I either automate file explorer or upload the file directly from selenium? It should select a specific file from a path to upload.
Any help is greatly appreciated.
This is my code:
upload1 = driver.find_element_by_xpath(upload1xpath)
upload1.send_keys('C:\Users\bodig\Downloads\image1.jpg')
To upload a file without clicking the upload button in most cases you can send your file directly to some element on that page.
This will be element with input tag name with attribute type='file'. Not the button user clicking but somewhere near it.
So you can try finding that element with this:
upload_input = driver.find_element_by_xpath('//input[#type="file"]')
upload.send_keys('C:\Users\bodig\Downloads\image1.jpg')
If you prefer using css_selector you can use this:
upload_input = driver.find_element_css_selector('input[type="file"]')
upload.send_keys('C:\Users\bodig\Downloads\image1.jpg')
To find specific element on your page I have to see the web page

How to navigate file upload window without knowing the elements using Selenium in Python

I have been trying to access the elements (input ID, button ID etc.) of a File Upload dialogue window using Selenium in Python, with no success. The File Upload window seems to be a Windows dialogue window. Have already tried the different solutions posted on this portal but nothing is working. Please assist.Attached is the screenshot of the File Upload window
A webdriver controls remote sessions on browsers. We cannot modify the windows system file dialog with a webdriver.
Thus, let me enlighten you with a possible solution (it may not be exactly what you want, in which case we will iterate on the solution):
To popup a file dialog to upload a file, you probably would've needed to click on the following html. In human readable terms, it's a file input field.
<input type="file">
But actually, there's no need to directly click on it. You can simply find the element, and send the path of the file to upload.
pathOfFile = "C:/Users/John/yourPotatoIsTwoPotatoes.png"
element = driver.find_element_by_css_selector("input[type='file']")
element.send_keys(pathOfFile)
I'm not sure if you'll like the idea of passing a path name to the file input, so tell me if I've answered your question enough!

What code can I use to download a csv file that requires some steps after logging in to the website?

After some research on my problem, it seems I should use either requests or urllib or both.
So basically, I am trying to learn the code I need to download a csv file from this url:
https://globalaccess.sustainalytics.com/#/tools/0
The way I manually download my files is as follows: first, I need to log in using username and password. Next I have to go to a tab called "Screening" that takes to me another page that has several buttons called "Generate". I click a specific generate button (it's always the same one) among the option to get the excel file. After that I have the option to save the file or open from a little window within the website.
My question is what code can I use on Python to download and save the file in a particular folder?
Use Selenium
https://selenium-python.readthedocs.io/
You'll need to download a 'chromedriver' to the same directory as your python script, then use the intro tutorial on the selenium docs site to drive the browser to type/click where you want.
If you use chrome you can right click on any given link/input box click inspect, then in the window that comes up right click the bit of highlighted code and 'copy xpath'. Use the find element by xpath function in Selenium to send keys or clicks to that element.

Python Selenium-webdriver close Open file windows pop-up

I'm using python and selenium chrome driver to click on an upload file button which opens an open file window as shown:
My code uploads the files without interacting with the window. What I'm trying to do is close this window but am not sure how to go about doing this.
I've read various other posts with the similar problem but none I could find really gave me what I was looking for. I understand selenium can't access the window, and I've read I need a different module to interact with it. What would be the best way to do this?
In the end I was able to upload the item directly into the page by finding the upload button and sending it the file path.
driver.find_element_by_xpath("//*[#id='upl-fileInp']").send_keys("C:/Users/user/folder/file.jpg")
Workaround
You need put inside element of upload the root file. Don't open the upload by button, just insert /root/of/file/file.pdf inside input upload value using Javascript. Remember, that's my workaround... haha

Categories