khaled baccour
khaled baccour

Reputation: 67

How do I wait until a webpage is loaded before opening another tab

I've made this little python script to automate opening the websites I need in the morning, take a look `

Required Modules

import webbrowser

Open the websites

`webbrowser.get('firefox').open_new_tab('https://www.netflix.com')
webbrowser.get('firefox').open_new_tab('https://www.facebook.com')
webbrowser.get('firefox').open_new_tab('https://www.udemy.com') `

And I don't know how to wait until the webpage is loaded before opening the next one (in another tab), any help?

Upvotes: 0

Views: 1933

Answers (1)

Shreyas Sreenivas
Shreyas Sreenivas

Reputation: 351

You could take the approach as mentioned at How to wait for the page to fully load using webbrowser method? and check for a certain element in the page manually.

Another options would be to import time and call it after opening each tab time.sleep(5) which waits for 5 seconds before running the next line of code.

import webbrowser
from time import sleep

links = ['https://www.netflix.com', 'https://www.facebook.com', 'https://www.udemy.com']

for link in links:
    webbrowser.get('firefox').open_new_tab(link)
    sleep(5)

Selenium Implementation:

Note: This implemenetation opens your URL's in multiple windows rather than a single window and multiple tabs.

I will be using the chrome driver which you can install at https://chromedriver.chromium.org/downloads

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("detach", True) #this is just to keep the windows open even after the script is done running.

urls = ['https://www.netflix.com', 'https://www.facebook.com', 'https://www.udemy.com']

def open_url(url):
  driver = webdriver.Chrome(executable_path=os.path.abspath('chromedriver'), chrome_options=chrome_options)
  # I've assumed the chromedriver is installed in the same directory as the script. If not, mention the path to the chromedriver executable here.
  driver.get(url) 

for url in urls:
  open_url(url)

Upvotes: 1

Related Questions