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

Time:11-30

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) 

CodePudding user response:

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

CodePudding user response:

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)
  • Related