selenium firefox download - python

I am using python selenium and would like to download a pdf file, however it opens in my browser? How can I download it from my browser? Any way to click the following image,
Before, all i had to do was disable the firefox download box dialog, but now I am not able to request the download. Any ideas?
What should i do to request the download? I am also not able to find the file on the server.
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/vnd.csv")

There is no way do do it with selenium in a way you described. However you can tweak your browser, so it doesn't open pdf's but downloads them - and that can be done with selenium.
Here's (How to auto save files using custom Firefox profile )a good example how to do it.

With AppLoader you can click on any button, icon, or shortcut, just like a real user.

Add
profile.set_preference("pdfjs.disabled",True)
That will disable the default pdf viewer in firefox.
Also your save-to-disk preference seems incorrect. It should be
profile.set_preference("browser.helperApps.neverAsk.saveToDisk","application/pdf")
The last thing to keep in mind is that if you have multiple file types to download, you need to put them all under the same set_preference saveToDisk statement, comma separated, otherwise the later statement will overwrite the earlier settings.

Related

Scraping a js based website for a javascript generated .csv download from python

I try to scrape a ajax/js based website for a .csv download.
If I visit the site in a browser, I need to click a button that calls a js function "downloadChartCSV()". This function generates and downloads a .csv file. That the file I'm after.
How can I automate this?
I have tried:
requests-html, which can login and render the page, but I miss an option to call that onClick event. There seems to be an open issue about exactly this: Github Issue
Selenium which probably works, but I don't want a full browser that is flashing up.
If you want to have the functionality of Selenium, but don't want a full browser flashing up: Use the PhantomJS Driver.
https://realpython.com/headless-selenium-testing-with-python-and-phantomjs/
This is a very good Introduction into it.

Python and Firefox (Windows) - How to save a page as I see it

I am trying to implement automation in Python, because they changed the way we access data in my work. We used to log into an Unix based terminal, while now by using a simcard we open a browser page in Firefox that emulates the terminal.
Here is my problem: I wrote a script to save a .txt file with the data I am interested in. But when I tried to run it at work, it didn't run, because the page wouldn't let me open it (even if I was logged in).
So I decided to use a dumb solution: I saved the Firefox page as it was open and ran the script locally. But... I can't find how to save the page opened in Firefox without having to type the URL in Python beforehand. I want to automate this step too.
Is there a way to save the page opened like this one, acting like a Ctrl + S shortcut?
I appreciate any kind of help! Thanks!

Using Mechanize for python, need to be able to right click

My script logs in to my account, navigates the links it needs to, but I need to download an image. This seems to be easy enough to do using urlretrive. The problem is that the src attribute for the image contains a link which points it to the page which initiates a download prompt, and so my only foreseeable option is to right click and select 'save as'. I'm using mechanize and from what I can tell Mechanize doesn't have this functionality. My question is should I switch to something like Selenium?
Mechanize, last I checked, was pretty poorly maintained and documented. Selenium has a much more active community.
That being said: why do you need mechanize to do this? Why not just use urllib?
I would try to watch Chrome's network tab, and try to imitate the final request to get the image. If it turned out to be too difficult, then I would use selenium as you suggested.

Automate browser interaction

I need to upload hundreds of files via a html form, so that they end up on a server. I have no other options and must go via the form.
I have tried doing this problematically using python but I'm doing something wrong and the files are empty when I try to open them via the web interface. I also tried doing a replay via firefox's TamperData and the file is also uploaded incorrectly in this case.
So I'm interested in exploring the idea of uploading the files by automating my browser instead. All I need to do is:
for file in files:
open a page
click on the browse button
select a file
click the upload button
So what software/library can I use to do this? I don't necessarily need to do this using python as I will never need to do this again in the future. I just need to get the files up there by any means possible.
I have access to Windows 7, Mac Os X, and Suse Linux.
I also don't care which browser I use.
Splinter works well for this kind of thing:
https://github.com/cobrateam/splinter
You could do something like:
from splinter import Browser
with Browser('firefox') as browser:
browser.visit('http://yourwebsite.com')
browser.find_by_name('element_name').click()
do some other stuff...
You just need to find the name or ID of the element you want to interact with on the page
please check out selenium:
It's an automated browser tool (behaves like a real user)
http://selenium-python.readthedocs.org/en/latest/
You can also if you're more the techy type and understand html write your own python code to only send files to the server with httplib2
https://code.google.com/p/httplib2/
for an auto clicker try autopy (this emulates mouse and keyboard input)
http://www.autopy.org/

Advanced screen-scraping using curl

I need to create a script that will log into an authenticated page and download a pdf.
However, the pdf that I need to download is not at a URL, but it is generated upon clicking on a specific input button on the page. When I check the HTML source, it only gives me the url of the button graphic and some obscure name of the button input, and action=".".
In addition, both the url where the button is and the form name is obscured, for example:
url = /WebObjects/MyStore.woa/wo/5.2.0.5.7.3
input name = 0.0.5.7.1.1.11.19.1.13.13.1.1
How would I log into the page, 'click' that button, and download the pdf file within a script?
Maybe Mechanize module can help.
I think that url on clicking the button maybe generated using javascript.So, to run javascript code from python script take a look at Spidermonkey.
Try mechanize or twill. HttpFox or firebug can help you to build your queries. Remember you can also pickle cookies from browser and use it later with py libs. If the code is generated by javascript it could be possible to 'reverse engineer' it. If nof you can run some javascript interpret or use selenium or windmill to script a real browser.
You could observe what requests are made when you click the button (using Firebug in Firefox or Developer Tools in Chrome). You may then be able to request the PDF directly.
It's difficult to help without seeing the page in question.
As Acorn said, you should try monitoring the actual requests and see if you can spot a pattern.
If not, then your best bet is actually to automate a fully-featured browser, that will be able to run Javascript, so you'll exactly mimic what a regular user would do. Have a look at this page on the Python Wiki for ideas, check the section Python Wrappers around Web "Libraries" and Browser Technology.

Categories

Resources