Sagar
Sagar

Reputation: 9

Python: how to open new tab without waiting for current tab page loading in Python Selenium

I am opening 10 tabs (same URL) in chrome browser Successfully. but problem is that, my URL takes 1 minute to load page and i don't want to wait 1 minute at each tab.

i need to let it load and want to open another tab and i know final tab compulsory take one minute to load but no problem but i don't want to wait 1 minute for each tab.

what can i do to achieve it?

i have used time.sleep(), WebDriverWait, driver.switch_to.window(x) but no use.

Thanks in Advance

This is my Code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common import window
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
url = 'http://my_url/Index'
driver.get(url)
for _ in range(10):
    driver.get(url)
    driver.switch_to.new_window(window.WindowTypes.TAB) 

Upvotes: 0

Views: 1041

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193308

The fastest way to open a/multiple new tab(s) without waiting for current tab page loading using Python Selenium would be to use execute_script() method as follows:

  • Code Block:

    driver = webdriver.Chrome(service=s, options=options)
    driver.get('https://stackoverflow.com/')
    for _ in range(10):
        driver.execute_script("window.open('','_blank');")
    
    # switch to opened tab i-th eg 0,1,2
    driver.switch_to.window(driver.window_handles[1]) 
    
  • Browser Snapshot:

fast_tabs

Upvotes: 1

Prophet
Prophet

Reputation: 33361

To get rid from waiting for the opened tab to be loaded you need to apply NONE PageLoadStrategy. By default NORMAL PageLoadStrategy is used. By definition in this case WebDriver should wait for the document readiness state to be "complete" after navigation while you want WebDriver should not wait on the document readiness state after a navigation event strategy.
To apply this PageLoadStrategy to your code this should be added to your code:

caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "none"

This is how looks the entire code on my side. It works as desired

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.service import Service

options = Options()
options.add_argument("--start-maximized")

caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "none"
s = Service('C:\webdrivers\chromedriver.exe')

driver = webdriver.Chrome(options=options, desired_capabilities=caps, service=s)

Upvotes: 0

originn
originn

Reputation: 13

Looks likley that you will need to create a thread for each time you open a URL

import threading

def main(url)
  # use a different driver for each thread
  options = webdriver.ChromeOptions()
  options.add_experimental_option("detach", True)
  options.add_argument("start-maximized")
  driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
  driver.get(url)
  rand_function1
  return

url = "http://my_url/Index"
for t in range(10):
   t = threading.Thread(target=main, args=[url])
   t.start()

Upvotes: 0

Related Questions