I know how to open a program from python, using the webbrowser module.
My question is this: how do I get it to open full screen?
At the moment I have this code:
import webbrowser
webbrowser.open("file.txt")
But it still opens Restored Down.
Please help!
Given the documentation, the webbrowser module does not have this capability at all.
This is not very surprising since you don't know what kind of device you are operating on, and it might not even have a graphical display!
What I would try is the following:
Use browser = webbrowser.get(SOME_BROWSER) to query which browsers are available.
Launch the browser using browser-specific command line arguments. You might be able to to do browser.args.append(FULLSCREEN_ARGUMENT) and then call browser.open_new(URL) instead of launching the browser manually using e.g.
the subprocess module.
Related
I have a program where I'm using Python's webbrowser module to open a browser and navigate to a page automatically. My code essentially looks like the following:
import webbrowser
chrome_path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
url = "stackoverflow.com"
webbrowser.get(chrome_path).open(url)
When doing it with a normal site it works exactly as expected. However, when I instead substitute in an internal Chrome site of the format chrome://<page> (e.g. chrome://dino or chrome://version) for the url, Chrome opens as expected, but it does not navigate anywhere but rather instead stays on my new tab page.
Why are normal urls (and even strings such as "hello world") working as expected, but only chrome-specific pages not? Is there any way to get around this?
(This is on Windows 10 & Python 3.6.8 by the way).
It indeed does not work, but it's not webbrowser's fault.
A small diving into the code shows that, at the end of the day, webbrowser simply calls subprocess.Popen(args) where args ending up being
'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe <url>'.
If you simply open a terminal window and execute
"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" chrome://dino
you will get the exact same behavior: Chrome opens and stays on the home page, so the problem is lying somewhere in Chrome's code (either a bug or a design choice).
It works with selenium since I assume it is using some black-OS-magic (ie interprocess communication) so it does not rely on Chrome's code. It just mimics a user.
I would need two things in vlc that I want to control from a Python script. First is to open a network stream, add the stream address as a url in it and play it. (This might be skippable)
The second thing is to take a snapshot at a specific time and use that picture. As I saw the different kind of libraries and modules, they can only things like play, pause, rewind a video.
Can anybody help me with this one?
Thanks in advance!
You can use os.chdir(path) and os.system(command)
Find where your vlc executable (.exe) is, and store the path in a variable.
Then you can use os.system to execute a given command.
Here you see a list of possible command-line options for VLC
Example code:
import os
vlc_path = "C:\path\to\vlc"
net_stream = "http://host[:port]/file" # You can use other protocols too
os.chdir(vlc_path)
os.system(f"vlc {net_stream}")
I'm currently writing a program which needs to check if a program is open before deciding to switch to it or open a new one. For example, a chrome short cut should check if chrome is already open. If it isn't then it should open a new chrome window but if it is then it should just show the existing chrome window/tab. Is there a way to write a function to do this?
Thanks in advance.
To check if a program is open I would suggest a library named psutil.
The switch of the window is more complicated and depends on the OS, for example in Windows you have to create (or get) an handle for that window and then call the OS API to change it, this is simply made using win32gui, as you can see here.
I'm writing a PyGTK application on Windows, and in a function, I'm trying to open a webpage with the webbrowser module. It should be the simplest thing in the world, but instead of opening in a browser, it prints the HTML source of the page to the console. Does anyone know why this would happen?
The code in question:
oauthURL = ("http://api.twitter.com/oauth/authorize?oauth_token=" + requestToken)
webbrowser.open(oauthURL, 2, True)
I tested it on my Arch Linux laptop just now, and it works fine, so this is a Windows-specific problem. Perhaps Python can't find a browser to use?
What it normally tries to do (on Windows) is using os.startfile(url), which launches the default application for http urls. You can check what this does by typing start http://www.example.com at the command prompt. If this has the same effect, you know what the problem is, and you should reconfigure your default browser in Windows.
This behaviour can be overridden by the user, through the BROWSER environment variable, so you might want to check that if the above doesn't help.
This code snippet can handle opening a web-page in a cross-platform way:
if sys.platform[:3] == 'win':
try:
os.startfile(oauthURL)
except WindowsError as why:
# your error handling code
print(why)
else:
webbrowser.open(oauthURL, 2, True)
It is based on IDLE's EditorWindow.py "python_docs" function.
I have a python program that opens several urls in seperate tabs in a new browser window, however when I run the program from the command line and open the browser using
webbrowser.open_new(url)
The stderr from firefox prints to bash. Looking at the docs I can't seem to find a way to redirect or suppress them
I have resorted to using
browserInstance = subprocess.Popen(['firefox'], stdout=log, stderr=log)
Where log is a tempfile & then opening the other tabs with webbrowser.open_new.
Is there a way to do this within the webbrowser module?
What is webbrowser.get() giving you?
If you do
webbrowser.get('firefox').open(url)
then you shouldn't see any output. The webbrowser module choses to leave stderr for some browsers - in particular the text browsers, and then ones where it isn't certain. For all UnixBrowsers that have set background to True, no output should be visible.
What about sending the output to /dev/null instead of a temporary file?
I think Martin is right about Unix systems, but it looks like things are different on Windows. Is this on a Windows system?
On Windows it looks like webbrowser.py is either going to give you a webbrowser.WindowsDefault browser, which opens the url using
os.startfile(url)
or if Firefox is present it's going to give you a webbrowser.BackgroundBrowser, which starts the browser on Windows using:
p = subprocess.Popen(cmdline)
It looks like only Unix browsers have the ability to redirect stderr in the webbrowser module. You should be able to find out what browser type you're getting by doing
>>> webbrowser.get('firefox')
In a Python interactive console.