0

Iā€™m trying to automate web browsing with Python (2.7) / Selenium / Chromedriver but having a peculiar behavior when opening more than one site (browser window) in succession. As a new browser window opens, a previously-opened one closes for some reason. Is this a garbage collection issue? Is there a way to force all browser windows to stay open? Here is a sample:

import sys
from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://google.com")
driver.get("http://amazon.com")
driver.get("http://ebay.com")
user1526973
  • 45
  • 1
  • 5

1 Answers1

1

Issue here is you are hitting multiple URLs in same tab. Its like clicking a new link inside a page which open the new page in same tab.What you need is to open seperate tab for each url.

driver = webdriver.Chrome()
driver.get("http://google.com")

#Open 2nd Tab
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') 
driver.get("http://amazon.com")

#Open 3rd Tab
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') 
driver.get("http://ebay.com")

Or if you want to open separate windows then you can create 3 instances of chrome driver and open separate urls in all of them.

driver1 = webdriver.Chrome()
driver2 = webdriver.Chrome()
driver3= webdriver.Chrome()

driver1.get("http://google.com")
driver2.get("http://amazon.com")
driver3.get("http://ebay.com")

Note :

I believe your next question will be how to work with different tabs. Follow below link: How to switch to new window in Selenium for Python?

rahul rai
  • 2,260
  • 1
  • 7
  • 17
  • Thanks, very helpful....one question though...is there a way to assign a window handler to each tab as you open it? I know you can "get" a window handler with "driver.window_handles[index number here]" but the tricky part is that the "current" handler remains with the original (first) window opened unless you deliberately switch it with "driver.switch_to.window." Assigning each window handler a variable name as it is opened would make it easy to keep track of which window is which. ā€“ user1526973 Aug 26 '20 at 17:08
  • You can store a reference to window handle using statement win1= driver.window_handles[0], win2 = driver.window_handles[1], win3 = driver.window_handles[2]. Now you can use these references to switch to any window like driver.switch_to_window(win2) . Also please accept the answer if it helps you solve your issue. ā€“ rahul rai Aug 26 '20 at 17:18