How to Open a Website in My Web Browser Using Python

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)

How to open a URL in python

with the webbrowser module

import webbrowser

webbrowser.open('http://example.com') # Go to example.com

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 :)

*** 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.

How can I open a website in my web browser using Python 3?

def __init__(self, username, password):
self.driver = webdriver.Safari()
self.driver.get("https://instagram.com")
sleep(2)

self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input')\
.send_keys(username)
sleep(3)
self.driver.find_element_by_xpath("//input[@name=\'password\']")\
.send_keys(password)
self.driver.find_element_by_xpath("//button[@type=\'submit\']")\
.click()
sleep(5)
self.driver.find_element_by_xpath("//*[@class='sqdOP yWX7d y3zKF ']").click()
sleep(4)


Related Topics



Leave a reply



Submit