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.
Related
I am using webbroswer.open() in a loop to download multiple files at given intervals.
The issue I am having is that whenever the browser window opens, it becomes the primary window and thereby interrupts and disrupts my ability to use the computer. Downloading multiple files means this can last some time. The broswer continuously flashing open is obviously jarring.
Is there any way to instruct webbrowser to open the browser minimised by default or otherwise avoid this issue in some other ingenious way?
Much appreciated!
If you are open to using other modules I would urge you to look into selenium. This allows you to do many things, and one of them is to launch in headless mode (so as not to disturb you as it loads pages). The documentation is at:
https://selenium-python.readthedocs.io/
And you would be interested in the headless option
You would be advised though to make sure your script works without this enabled before you enable it though.
Sample code:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
my_options = Options()
my_options.headless = True # set to False for debugging!!
browser = webdriver.Chrome(options=my_options)
browser.get('http://www.google.com')
print('Done.')
You will need to download the proper drivers (just follow the instructions on the link I posted) for whatever browser you'd like. I picked Chrome, but they have Edge, Firefox, and Safari browsers as well!
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.
I have written a simple program to mimic logging into a web page, clicking on a few options and the final step is to click on a link to generate a report. Everything seems to be running fine on my PC and the report actually get downloaded into the default download directory(I am using Chrome). However, when my colleague tried it, the file download was cut off and the browser process terminates itself running the same code and I can't seem to replicate what he saw.
My code looks like this:
browser = splinter.Browser('chrome')
browser.visit('https://village-us.albourne.com/castle/')
browser.fill('username','xxx')
browser.fill('password','xxxxx')
browser.find_by_name('signinform')
button = browser.find_by_name('submit_0')
button.click()
browser.visit('https://village-us.albourne.com/castle/hf/listingoptions')
rptButton = browser.find_by_name('buildReport')
rptButton.click()
browser.find_by_name('checkbox_0').click()
browser.find_by_name('checkbox_1').click()
excelButton = browser.find_by_id('excelReport').first
excelButton.click()
So my questions are:
1. Is the excelButton.click() response supposed to be synchronized to the browser's response(per default timeout of course)?
2. Is there a way to change the default timeout period?
3. Since the browser is getting opened, if we run this process as a batch process, will this pose any issue when the screen lock is on? I have read about using the zope-testbrowser which seems like a good alternative for this purpose, but not sure if zope-testbrowser supports file downloads as well.
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.