Using Python webbrowser package I can open a new tab with a specified URL. Is there a way to close this tab? I referred the below official docs and nothing related to close action is mentioned.
Python webbrowser package doc: https://docs.python.org/3/library/webbrowser.html
You can use pyautogui to close the browser tab when your task is fulfilled.
import time,webbrowser, pyautogui
def open_close(url="https://www.python.org/"):
webbrowser.open(url)
time.sleep(20)
pyautogui.hotkey('ctrl', 'w')
print("tab closed")
No, webbrowser doesn't have methods to close tabs nor browser itself as you may see in its documentation page:
https://docs.python.org/2/library/webbrowser.html
You could
Use a hotkey using the pykeyboard library which you can read about at https://github.com/SavinaRoja/PyUserInput
or the keyboard library at https://github.com/boppreh/keyboard
Another (but probably worse) option is:
You may use a "taskkill" command like
import os
os.system("taskkill /im chrome.exe /f")
However, this will just kill all processes and close the chosen program (in this case chrome.exe)
(This also may delete data from the browser, f.eks. you lose all you're windows even tho you have chosen in settings to save them for next time you open your browser)
u can close the tab by driver.close if u are using selenium package
Related
Unable to close browser using python webbrowser.
I don't know how to close it.
Code is:
elif 'open chrome' in query:
Cpath = 'C:\\Users\\hi\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe'
os.startfile(Cpath)
You do not appear to using Python's webbrowser module to start one up, although even if you did it wouldn't help because the controller objects it creates don't support a close() method.
That's also true about the os.startfile() function you are using, and there's no option to wait for the application to close or to force it to do so.
Because of that, I suggest that you create an instance of the subprocess.Popen class to start the web-browser application, because if you did, you would then have the option of calling the terminate() (or kill()) method of the class instance to close it.
I want to open google chrome browser and go to facebook and terminate the python program but keep the google chrome window open until I manually close it. Please give me your own idea/Program with the above said as the aim.
I expect the chrome window to remain open after the program terminates, but it closes automatically after the program terminates.
Using os.system() should be avoided because it is platform dependent and because it isn't secure: if you use os.system('start chrome "%s"') % url where url is a string submitted by the user, someone can enter www.facebook.com" && shutdown /s /t "0 Facebook will open in a new Chrome window but then the computer will shut down.
The easiest way to open a new page in the browser is:
import webbrowser
webbrowser.open_new("www.facebook.com")
It remains open when the Python script terminates.
Try this, works on windows!
import os
os.system("start chrome \"www.facebook.com\"")
This shall open a chrome browser with the Facebook URL using cmd and it remains open even after the termination of the program.
I'm trying to open two websites in two tabs in my web browser. What actually happens is that two separate web browser windows are opened.
import webbrowser
webbrowser.open_new('https://www.msn.com')
webbrowser.open_new_tab('https://www.aol.com/')
The issue is likely that browser hasn't finished opening by the time you ask for a new tab. The docs do state that if no browser is open open_new_tab() acts as open_new(), which is why you are seeing two browsers.
I suggest putting a small delay between the calls:
import webbrowser
import time
webbrowser.open_new(url1)
time.sleep(1)
webbrowser.open_new_tab(url2)
Your other option is to poll the running processes and wait until the first instance of the browser appears before asking for a new tab.
I've a made a selenium test using python3 and selenium library.
I've also used Tkinter to make a GUI to put some input on (account, password..).
I've managed to hide the console window for python by saving to the .pyw extension; and when I make an executable with my code, the console doesn't show up even if it's saved with .py extension.
However, everytime the chromedriver starts, it also starts a console window, and when the driver exists, this window does not.
so in a loop, i'm left with many webdriver consoles.
Is there a work around this to prevent the driver from launching a console everytime it runs ?
I hated dealing with this in selenium until I remembered that this was an obvious use case for context managers just like the usage of open.
I did find out that selenium is about to add this officially to their package in this pull request
Until this is officially added, this snippet should give you the functionality you need to get things going :)
import contextlib
#contextlib.contextmanager
def Chrome(*args, **kwargs):
webdriver = webdriver.Chrome(*args, **kwargs)
try:
yield webdriver
finally:
webdriver.quit()
with Chrome() as driver:
# whatever you're planning on doing goes here
driver.close() and driver.quit() are two different methods for closing the browser session in Selenium WebDriver.
driver.close() - It closes the the browser window on which the focus is set.
driver.quit() – It basically calls driver.dispose method which in turn closes all the browser windows and ends the WebDriver session gracefully.
You should use driver.quit whenever you want to end the program. It will close all opened browser window and terminates the WebDriver session. If you do not use driver.quit at the end of program, WebDriver session will not close properly and files would not be cleared off memory. This may result in memory leak errors.
import webbrowser
br=webbrowser.open('http://www.google.com/')
if br==True:
print"Success"
print"need to close the browser"
You can't.
But you can use the subprocess module instead to open the browser and use the terminate or kill methods.