1

If I use the piece of code below:

driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get(url)
driver.switch_to.window(driver.window_handles[0])

It will throw back an error at me.

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'name' must be a string

The same thing happens if I use any code at all with driver.switch_to.window(driver.window_handles[1]) What can I do to fix this as it is quite annoying because it basically destroys the whole code? I'm using opera.

Full traceback:

Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\websitemonitorcsshtml\switchtab.py", line 11, i
n <module>
    driver.switch_to.window(driver.window_handles[1])
  File "C:\Users\user\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriv
er\remote\switch_to.py", line 134, in window
    self._w3c_window(window_name)
  File "C:\Users\user\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriv
er\remote\switch_to.py", line 143, in _w3c_window
    send_handle(window_name)
  File "C:\Users\user\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriv
er\remote\switch_to.py", line 139, in send_handle
    self._driver.execute(Command.SWITCH_TO_WINDOW, {'handle': h})
  File "C:\Users\user\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriv
er\remote\webdriver.py", line 418, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriv
er\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'name'
 must be a string
  (Session info: chrome=95.0.4638.69)
  (Driver info: operadriver=95.0.4638.54 (d31a821ec901f68d0d34ccdbaea45b4c86ce543e-ref
s/branch-heads/4638@{#871}),platform=Windows NT 10.0.19042 x86_64)

Here is the full code:

from selenium import webdriver 
from selenium.webdriver.opera.options import Options 

options = Options() 
options.add_experimental_option('excludeSwitches', ['enable-logging']) 
DRIVER_PATH = 'C:\Python27\Scripts\operadriver.exe' 
driver = webdriver.Opera(options=options, executable_path=DRIVER_PATH)
driver.get("stackoverflow.com/")
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get("github.com/")
driver.switch_to.window(driver.window_handles[0])
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Coder
  • 109
  • 2
  • 11
  • At which line do you see the error? Complete error stacktrace? – undetected Selenium Nov 22 '21 at 18:37
  • Full code is: from selenium import webdriver from selenium.webdriver.opera.options import Options options = Options() options.add_experimental_option('excludeSwitches', ['enable-logging']) DRIVER_PATH = 'C:\Python27\Scripts\operadriver.exe' driver = webdriver.Opera(options=options, executable_path=DRIVER_PATH) driver.get("https://stackoverflow.com/") driver.execute_script("window.open('');") driver.switch_to.window(driver.window_handles[1]) driver.get("https://github.com/") driver.switch_to.window(driver.window_handles[0]) The complete error is: https://pastebin.com/iHeKNYgu – Coder Nov 22 '21 at 18:41

2 Answers2

1

Window Switching

  1. Before attempting to switch the window, store the parent window handle for future use.

    window_before  = driver.current_window_handle
    
  2. After opening the new window store the window handles in a list.

    windows_after = driver.window_handles
    
  3. While switchin windows instead of using indexes e.g. window_handles[0], window_handles[1], use List Comprehension to locate the new window as follows:

    new_window = [x for x in windows_after if x != window_before][0]
    

Solution

Complete Window Switching code block:

driver.get("https://stackoverflow.com/")
window_before = driver.current_window_handle
print("First Window Handle is : %s" %window_before)
print("Page Title before Tab Switching is : %s" %driver.title)
driver.execute_script("window.open('','_blank');")
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
windows_after = driver.window_handles
new_window = [x for x in windows_after if x != window_before][0]
driver.switch_to.window(new_window)
driver.get("https://www.selenium.dev/")
print("Second Window Handle is : %s" %new_window)
print("Page Title after Tab Switching is : %s" %driver.title)
driver.switch_to.window(window_before)
print("Window Handle is : %s" %window_before)
print("Page Title after Reverse Tab Switching is : %s" %driver.title)
driver.quit()

You have to add the following imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Console Output:

First Window Handle is : CDwindow-9E723D48DFB59B9B6BCB992EFE3C6695
Page Title before Tab Switching is : Stack Overflow - Where Developers Learn, Share, & Build Careers
Second Window Handle is : CDwindow-9CCD4B7DD7F5916788A422F24D476BC8
Page Title after Tab Switching is : Selenium
Window Handle is : CDwindow-9E723D48DFB59B9B6BCB992EFE3C6695
Page Title after Reverse Tab Switching is : Stack Overflow - Where Developers Learn, Share, & Build Careers
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • It still gives me the same error even if I add this. The complete error is: https://pastebin.com/iHeKNYgu – Coder Nov 22 '21 at 18:43
  • In the [pastebin](https://pastebin.com/iHeKNYgu) error tracelog I still can see `driver.switch_to.window(driver.window_handles[1])` – undetected Selenium Nov 26 '21 at 14:29
  • I decided to change to chrome instead of opera and it's now almost completely bug free. I believe opera is not worked on as much as chrome is. Thank you for trying to help me though! :) – Coder Nov 26 '21 at 14:55
0

This may be caused by you trying to switch to tab that didn't have enough time to get created. To confirm this, print list of available window_handles after window.open call and see if it has the count you expect. If it has only one id listed and you are trying to access the second element, it will say that 'name' (i.e., tab id) must be string while it's null.

The solution would be to wait until the count of tabs increases (after you opened something in the new tab) and switch only after that condition was met.

sitanosik
  • 33
  • 5