Python Forum

Full Version: Closing Web Browser
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi. I'm very new to Python coding. This is my code (attached).

import webbrowser
url = ""https://newholland.com"
webbrowser.open(url)

How do I add a simple 'wait' i.e. wait for 5 seconds, the 'close' that webpage?

Thanks
There is no close browser in webbrowser module,can only open in browser.
Can use PyAutoGUI to do this task.
import webbrowser
import pyautogui
import time

def open_close(url):
    webbrowser.open(url)
    time.sleep(20)
    pyautogui.hotkey('ctrl', 'w')
    print("tab closed")

url = "https://www.python.org/"
open_close(url)
(Jul-16-2024, 07:30 AM)rdwessex Wrote: [ -> ]Hi. I'm very new to Python coding. This is my code (attached).

import webbrowser
url = ""https://newholland.com"
webbrowser.open(url)

How do I add a simple 'wait' i.e. wait for 5 seconds, the 'close' that webpage?

Thanks
from selenium import webdriver
driver=webdriver.Chrome()
driver.get("https://newholland.com")
driver.implicitly_wait(5)
driver.close()
(Jul-16-2024, 02:31 PM)Thirupathamma Wrote: [ -> ]from selenium import webdriver
driver=webdriver.Chrome()
driver.get("https://newholland.com")
driver.implicitly_wait(5)
driver.close()

Great, thanks. Although I have another issue. I can't import Selenium. Everything I'm trying to do in cmd is throwing an error. See screenshots. I keep getting pip is not a recognized command, etc.
(Jul-16-2024, 04:57 PM)rdwessex Wrote: [ -> ]Great, thanks. Although I have another issue. I can't import Selenium. Everything I'm trying to do in cmd is throwing an error. See screenshots. I keep getting pip is not a recognized command, etc.
You do not type rdwes it just a dictionary,some basic understating is missing here.
Test python and pip like this in cmd.
C:\>python -V
Python 3.12.2

C:\>pip -V
pip 24.0 from C:\Python312\Lib\site-packages\pip (python 3.12)

# If this work and over don't,need to add pip to OS Path.
C:\>python -m pip -V
pip 24.0 from C:\python312\Lib\site-packages\pip (python 3.12)

C:\>
Can just install like this and add pip to path later
C:\>python -m pip install selenium --upgrade
Requirement already satisfied: selenium in c:\python312\lib\site-packages (4.18.1)
Collecting selenium
.....
Successfully installed selenium-4.22.0 websocket-client-1.8.0
Also if try to use Selenium most add chrome driver chromedriver.exe to a OS Path,or it will not find it.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

# Setup
# https://storage.googleapis.com/chrome-for-testing-public/125.0.6422.141/win64/chromedriver-win64.zip
options = Options()
#options.add_argument("--headless=new")
ser = Service(r"C:\cmder\bin\chromedriver.exe") # In my OS Environment Variables Path
browser = webdriver.Chrome(service=ser, options=options)
# Parse or automation
url = "https://www.python.org/"
browser.get(url)
time.sleep(10)
#browser.implicitly_wait(10) # Don't work for this task
browser.close()