简体   繁体   中英

How can I disable the webbrowser message in python?

In my python program, when I send the user to create a gmail account by use of the webbrowser module, python displays:

"Please enter your Gmail username: Created new window in existing browser session."

Is there any way to get rid of "created new window in existing browser session", as it takes up the space where the user types in their Gmail account.

The code for this is:

webbrowser.open('https://www.google.com/accounts/NewAccount?service=mail')  
gmail_user = raw_input('Please enter your Gmail username: ')

After trying out both of Alex Martelli's suggestions, the code is: http://pastebin.com/3uu9QS4A 在尝试了两个Alex Martelli的建议之后,代码是: http//pastebin.com/3uu9QS4A

I have decided just to tell users to go to the gmail registration page instead of actually sending them there, as that is much simpler to do and results in no (currently-unsolvable-by-me) errors. 我已经决定告诉用户转到gmail注册页面而不是实际发送它们,因为这样做更简单,并且导致没有(当前无法解决的)错误。

As S.Lott hints in a comment, you should probably do the raw_input first; however, that, per se, doesn't suppress the message from webbrowser , as you ask -- it just postpones it.

To actually suppress the message, you can temporarily redirect standard-output or standard-error -- whichever of the two your chosen browser uses to emit that message. It's probably no use to redirect them at Python level (via sys.stdout or sys.stderr ), since your browser is going to be doing its output directly; rather, you can do it at the operating-system level, eg, for standard output:

import os
gmail_user = raw_input('Please enter your Gmail username: ')
savout = os.dup(1)
os.close(1)
os.open(os.devnull, os.O_RDWR)
try:
   webbrowser.open(whatever)
finally:
   os.dup2(savout, 1)

(for standard error instead of standard output, use 2 instead of 1). This is pretty low-level programming, but since the webbrowser module does not give you "hooks" to control the way in which the browser gets opened, it's pretty much the only choice to (more or less) ensure suppression of that message.

There is an answer to another question that is relevant here.

You can use

webbrowser.get().open('https://www.google.com/accounts/NewAccount?service=mail')

I have xdg-open installed (Linux), which led to a message START /usr/lib/firefox/firefox for me when using webbrowser.open() . Using the method above this message is not displayed (and xdg-open is still used).

This supresses output to stdout. It doesn't suppress output to stderr for all setups though. I still have error messages in the terminal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM