2

I've seen many example script on how to use selenium switch_to.window

Here is an example script regarding what I learned, doesn't work at all:

    from selenium import webdriver
    from pprint import pprint
    
    browser = webdriver.Firefox()
    
    
    script="""
        myWindow = window.open("", "ChildWindow", "width=200,height=100");
    """
    browser.execute_script(script)
    
    wHandles = browser.window_handles
    pprint(wHandles)
    
    handle = wHandles[1]
    print(handle, type(handle))
    
    browser.switch_to.window(handle)

I've got that error message:

InvalidArgumentException: Expected "handle" to be a string, got [object Undefined] undefined

Obviously i tired with normal web page with same result a as well.

Is there anyone with same problem?

StJoesy
  • 79
  • 2
  • 6

1 Answers1

2

Forgot about webdriver chrome/firefox - better use webdriver_manager which manages the latest browsers with your python version More about webdriver_manager can be found here

pip install webdriver_manager

here is the flawlessly code

from webdriver_manager.firefox import GeckoDriverManager
from selenium import webdriver

browser = webdriver.Firefox(executable_path=GeckoDriverManager().install())

script = """
        myWindow = window.open("", "ChildWindow", "width=200,height=100");
    """
browser.execute_script(script)

wHandles = browser.window_handles
print(wHandles)

handle = wHandles[1]
print(handle, type(handle))

browser.switch_to.window(handle)

browser.quit()
Alin Stelian
  • 861
  • 1
  • 6
  • 16
  • Seems to be stackovwrflow use a dumb algorithm what closed my question whilst your solution the real solution. Thank you. I could only give up a up triangle yet. I'm trying to contact to support team. – StJoesy Sep 19 '20 at 05:01
  • Accept it when you can, thanks and welcome – Alin Stelian Sep 19 '20 at 05:10