Retrieving file name while it's being downloaded - python

For reasons i'm not going to get into i need to get the name of the file that is being downloaded in the instance of that browser.
Is there a way using selenium to get the name of the file being downloaded? Not talking about waiting for it to download and get the name after
I am using selenium with chrome and python.

So i ended up going in another direction with this, i am simply creating folders for each browser instance i open and each instance only downloads in that folder and retrieve the name from there.

Related

get dropbox file content using dropbox URL

I have dropbox filepicker in my project, once user select file i am receiving URL of the dropbox, now i want a content of that file using that URL in python.
here is the link which i received from dropbox picker : https://www.dropbox.com/s/ocissavtfvvdh2g/images.png?dl=0
I have checked this link https://sodocumentation.net/dropbox-api/topic/408/downloading-a-file, but it will ask for the path, but i don't have path of the file, i just have URL of the file
I just have to use 1 in dl querystring.
https://www.dropbox.com/s/ocissavtfvvdh2g/images.png?dl=1 like this
I would suggest you to use Dropbox API for your project. I made a similar project but using Google Drive API.
Check bellow
https://www.dropbox.com/developers/documentation/python#install
It sounds like you're using the Dropbox Chooser. If you just want direct access to the data of the selected file(s), you should use the "direct" link type.
Based on your sample link, I see that you're currently using the "preview" link type, which doesn't link directly to the file data, but rather a preview page.
Also, the sample code you linked to is for accessing files via the Dropbox API with an access token, not using a link returned by the Chooser, so it isn't relevant.
If you switch to the "direct" link type, you can use the returned link to directly access the file data just with an HTTP GET request, for four hours.
Alternatively, if you need access for longer than that, you can keep using the "preview" link type but modify the link as documented here.

How to use requests to download files with original names?

I made a little script to automate some downloads, but I have a little issue. For example, the link is www.link.com/29292292.pdf but If I press the download button in my browser the name of the file is file with a good name.pdf.
I don't know to tell to requests to download the file but with 'file with a good name.pdf'.

How to access Google Chrome Extensions (Session Buddy) via Python?

I want to access Session Buddy using Python.
In my case "access" means to get all currently opened URLs from Chrome.
Session Buddy allows you to save all opened URLs into a .csv file.
To do so you need to "setup" a few things (simplified: press buttons) and then all URLs are downloaded to Chromes /Downloads directory.
I would like to fully automate this process though. This means python needs to access Session Buddy, initiate the download and then save the file to the directory you want it to.
I can't use requests or something though since an extension won't work using an URL. This is what the extension calls: chrome-extension://edacconmaakjimmfgnblocblbcdcpbko/main.html
In general, I don't necessarily want to use Session Buddy to get all the URLs, it just seems to be the easiest way..
So, in summary, I just want to ask: How can I automatically use Python to fetch all currently opened URLs in my Chrome Browser (using Session Buddy)?
I'm thankful for any kind of help.
You can use selenium webdriver and load .crx(extension) file to automate
chrome_options = Options()
chrome_options.add_extension('path_to_extension')
driver = webdriver.Chrome(executable_path=executable_path,chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()

Download chrome extension for selenium test

I'm working on a selenium test and I need to get a chrome extension from the chrome app store to use in the test. Right now it's a manual process to update to a newer version of the extension.
Current Flow:
1. Manual download extension through a chrome extension downloader.
2. Store the .crx file in a location visible to the selenium test.
3. Execute test with that extension.
I was hoping that google had an API that could be hit in order to download the extension but I've been unable to find anything to that effect. Has anyone ran into a situation like this and been able to solve it?
Basically you just have to capture the redirect url and then request that.
In python:
pluginId = id at end of url on plugin page. option 2 on here explains it well
blah=requests.get(url,params{'prodversion':'57.0','x':"id=pluginId",'response':'redirect'},verify=False,stream=True)
blahFile = requests.get(blah.url)
extension = open("yourExtension.crx", 'wb')
extension.write(blahFile.content)
extension.close()

Python webrowser open url with bookmarks like www.something.com/file.html#top

I am using a hmtl file as a help document for my program, and would really like to be able to open the file at a specific point. i assumed i would be able to do this using the built in webbrowser module by specifying a url with a bookmark.
this is my html file name: help.html
i assumed that i would be able to use: help.html#top
this is the code i am using to open the file, this works fine:
webbrowser.open("Files\help.html")
and this is the code i have been trying to use to open at a specific point which ie9 apparently cant display (not sure why it is trying to load in ie9 as chrome is my default browser, and the working one above loads in chrome):
webbrowser.open("Files\help.html#2.1.0")
any ideas guys?
webbrowser.open() calls the browser from the command line. So you might try to do that yourself first. If that doesn't work, it's likely that your browser just doesn't support that for local files or something.
With Ubuntu+Firefox for example, webbrowser.open() does what you ask. (but - as Dave Webb said in his answer - you do have to provide a file: url, not just a filename).
(not on Windows at the moment, so haven't checked there)
As for why it doesn't load chrome but ie9: (you can look in the webbrowser.py code yourself if you want) I think it does try to use your default webbrowser, by doing os.startfile(url). What happens when you doubleclick your help.html file, of when you just type help.html (adjust path as needed) at the command line? It should do the same.
EDIT:
It seems that it doesn't always use the command line. On Windows, when trying to use the default browser, it uses os.startfile() which in turn uses the win32 ShellExecute api. ShellExecute can be used to perform certain actions on a file, folder or URL, like "open", "edit" or "print" with its default application. In this case, ShellExecute is asked to "open" the URL.
It seems however, that ShellExecute ignores the fragment identifier (the part after #) when opening file: urls. Strangely enough, this is not the case with http:urls. Presumably, a file: url is just converted to a plain filename first.
There seems to be little you can do about this except:
write something that "does the right thing" yourself (and register it as a browser controller for the webbrowsermodule, and use webbrowser.get() to get your controller, see docs)
as many applications do: configure the browser you want to use (or make it possible for your users to do so). The easiest way would be to set the BROWSER environment variable (see webbrowser module docs)
serve the file via a localhost http server, and open the http url, which would then be something like "http://. "http://localhost:8000/help.html#2.1.0". (The SimpleHttpServer module might come in handy)
Or, the easiest way: as you seem to be on windows: just try to open internet explorer specifically:
try:
browser = webbrowser.get('c:\\Program Files\\Internet Explorer\\IEXPLORE.EXE')
except Webbrowser.Error:
browser = webbrowser.get()
browser.open(url)
(This will fall back to using the default, so your code would still work on other platforms)
I think webbrowser is expecting a URL, so have you tried something like:
webbrowser.open("file://c:/path/to/files/html.html#2.1.0")

Categories

Resources