Python webbrowser.open() to open Chrome browser
You can call get() with the path to Chrome. Below is an example - replace chrome_path with the correct path for your platform.
import webbrowser
url = 'http://docs.python.org/'
# MacOS
chrome_path = 'open -a /Applications/Google\ Chrome.app %s'
# Windows
# chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
# Linux
# chrome_path = '/usr/bin/google-chrome %s'
webbrowser.get(chrome_path).open(url)
How to open a website in google chrome using webbrowser module
Try this:
import webbrowser
url = "https://www.youtube.com/"
webbrowser.open(url)
I use https://www.youtube.com/
instead of youtube.com
and it will works :)
Python: how to open default browser using webbrowser module?
Try using get()
:
webbrowser.get('chrome').open('https://www.youtube.com')
EDIT
Make sure to use the full path to the site
How do I open google chrome in python
Use os.system('start chrome')
and os.system('start chrome {}'.format(site))
in Windows or os.system('google-chrome')
and os.system('google-chrome {}'.format(site))
in Linux, where site
is URL for the site you're looking for.
*** Solved *** Python - Displaying a web site in the same browser tab each time webbrowser.open is called
I'm unfamiliar with the webbrowser tool, but using selenium webdriver the following all stays in the same tab.
# import
from selenium import webdriver
from selenium.webdriver.common.by import By
# browser setup
browser = webdriver.Chrome()
browser.implicitly_wait(10)
# open url
browser.get("http://www.google.com")
# click element on page (same tab)
browser.find_element(By.XPATH,"/html/body/div[1]/div[1]/div/div/div/div[1]/div/div[2]/a").click()
# open new URL (same tab)
browser.get("http://www.stackoverflow.com")
Also, some other tools available in selenium are below. These are useful if you want new tabs but need to switch back and forth to "parent" tabs:
# assign tab variables (window_handle = tab apparently)
window = browser.current_window_handle
parent = browser.window_handles[0]
child = browser.window_handles[1]
# switch to new tab
browser.switch_to.window(parent)
I hope that at least some element of this is helpful.
Run a webbrowser and continue script without close the browser
You could create a new Thread and in that thread open the browser:
from threading import Timer
def open_browser():
webbrowser.open('https://www.google.com/')
Timer(1, open_browser).start()
print(“test”)
Everything that has to do with interaction of the browser should now be placed in def open_browser():
This code will set a timer for 1 millisecond and then execute the function within a seperate thread, so your code keeps executing.
How can I open a website in my web browser using Python?
The webbrowser
module looks promising: https://www.youtube.com/watch?v=jU3P7qz3ZrM
import webbrowser
webbrowser.open('http://google.co.kr', new=2)
Related Topics
Timeout for Python Requests.Get Entire Response
Multiple Variables in a 'With' Statement
How to Make a Selenium Script Undetectable Using Geckodriver and Firefox Through Python
How to Determine Whether a Year Is a Leap Year
How to Activate a Virtualenv Inside Pycharm's Terminal
How to Read and Write Ini File with Python3
How to Format a String Using a Dictionary in Python-3.X
Escape String Python for MySQL
Pandas Split Column into Multiple Columns by Comma
How to Override the [] Operator in Python
Get Lat/Long Given Current Point, Distance and Bearing
Find First Element in a Sequence That Matches a Predicate
Get Fully Qualified Class Name of an Object in Python
Cannot Find Vcvarsall.Bat When Running a Python Script
How to Change Index of a for Loop
What Is the G Object in This Flask Code