2

I searched for this question,and I found an idea using driver.switch_to.window(),but it didn't work as expect:

from selenium import webdriver

driver1=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver1.get('https://www.google.com')


driver2=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver2.get('https://www.bing.com/')

driver1.switch_to.window(driver1.current_window_handle)

above code will first open a chrome window and go to google,then will open another chrome window and go to bing,then

driver1.switch_to.window(driver1.current_window_handle)

seems didn't work,the window showing bing still shows on top of the window showing google. Anyone have any idea?I think

driver1.switch_to.window(driver1.current_window_handle)

may have some BUG.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
bo wen
  • 33
  • 2
  • 9

2 Answers2

4

As you have used two WebDriver instances as driver1 and driver2 respectively to openthe urls https://www.google.com (e.g. windowA) and https://www.bing.com/ (e.g. windowB) it is worth to mention that the function switch_to.window() is a WebDriver method. So, driver1 can control only windowA and driver2 can control only windowB.

For Selenium to interact with any of the Browsing Window, Selenium needs focus. So to iterate among the different Browsing Windows you can shift the focus to the different Browsing Window using JavascriptExecutor as follows :

  • Python:

    driver1.execute_script("window.focus();")
    driver2.execute_script("window.focus();")
    
  • Java:

    ((JavascriptExecutor) driver1).executeScript("window.focus();");
    ((JavascriptExecutor) driver2).executeScript("window.focus();");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

I believe you have a different concept of "window" in driver.switch_to.window(). In chrome browser, it means "tab". It's not another chrome browser or browser window like what are you trying to do in your code.

If switch_to.window() what you really want, I'll give an example how to use it:

driver=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver.get('https://www.google.com')
# open a new tab with js
driver.execute_script("window.open('https://www.bing.com')")
driver.switch_to.window(driver.window_handles[-1])
# now your driver is pointed to the "tab" you just opened
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Aldo Suwandi
  • 382
  • 1
  • 6
  • 20
  • Thanks.I misunderstood what 'switch_to_window' mean,it will switch between tabs in the same webdriver.But what I really what is to switch between different webdriver window,do you have any suggestion? – bo wen Mar 11 '18 at 12:20
  • Is there a way to jump to different instances of chrome? In other words I have one instance of Chrome open as a particular user and another instance of Chrome running for another user and I want to be able to call either instance. I didn't see a mention of instances on https://pypi.org/project/selenium/. Thank you for any advice you can give. – Anthony Petrillo Aug 12 '23 at 11:24