Open multiple URLs in same browser with selenium python

Viewed 4537

How can I open 5 urls simultaneously on a single browser? In addition, the script has to browse one by one through these 5 urls by performing the following tasks:

  • adding information on a field
  • select a CTA button
  • then click on a Send button

1 url must have a tab, so in total it should have 5 tabs running one by one.

here's my code but it didn't work Thank you for your help

from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.delete_all_cookies()

urls = ["https://business.google.com/u/0/edit/l/10199720925622488243?hl=fr",
        "https://business.google.com/u/0/edit/l/13532588171385373346?hl=fr",
        "https://business.google.com/edit/l/18307083220547614220",
        "https://business.google.com/u/0/edit/l/08603059593698723407?hl=fr",
        "https://business.google.com/edit/l/00810825496818981035"]
for posts in urls:
    a = driver.execute_script("window.open('');")
    driver.get(a)
1 Answers
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

driver = webdriver.Chrome(executable_path=r"C:\Users\prave\Downloads\travelBA\chromedriver.exe")


driver.maximize_window()
driver.delete_all_cookies()

urls = ["https://business.google.com/u/0/edit/l/10199720925622488243?hl=fr",
        "https://business.google.com/u/0/edit/l/13532588171385373346?hl=fr",
        "https://business.google.com/edit/l/18307083220547614220",
        "https://business.google.com/u/0/edit/l/08603059593698723407?hl=fr",
        "https://business.google.com/edit/l/00810825496818981035"]
for posts in range(len(urls)):
    print(posts)
    driver.get(urls[posts])    
    if(posts!=len(urls)-1):
       driver.execute_script("window.open('');")
       chwd = driver.window_handles
       driver.switch_to.window(chwd[-1])

// you can move to specific handle    
chwd = driver.window_handles
print(chwd)

find the window handle and switch to it

Related