如果我使用以下代码:
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get(url)
driver.switch_to.window(driver.window_handles[0])
它会向我抛出一个错误。
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'name' must be a string
如果我在driver.switch_to.window(driver.window_handles[1])
我能做些什么来修复这个问题,因为它基本上会破坏整个代码,所以很烦人?我在用歌剧。
完整回溯:
Traceback (most recent call last):
File "C:UsersuserPycharmProjectswebsitemonitorcsshtmlswitchtab.py", line 11, i
n <module>
driver.switch_to.window(driver.window_handles[1])
File "C:UsersuserPycharmProjectsGiraffevenvlibsite-packagesseleniumwebdriv
erremoteswitch_to.py", line 134, in window
self._w3c_window(window_name)
File "C:UsersuserPycharmProjectsGiraffevenvlibsite-packagesseleniumwebdriv
erremoteswitch_to.py", line 143, in _w3c_window
send_handle(window_name)
File "C:UsersuserPycharmProjectsGiraffevenvlibsite-packagesseleniumwebdriv
erremoteswitch_to.py", line 139, in send_handle
self._driver.execute(Command.SWITCH_TO_WINDOW, {'handle': h})
File "C:UsersuserPycharmProjectsGiraffevenvlibsite-packagesseleniumwebdriv
erremotewebdriver.py", line 418, in execute
self.error_handler.check_response(response)
File "C:UsersuserPycharmProjectsGiraffevenvlibsite-packagesseleniumwebdriv
erremoteerrorhandler.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)
这是完整的代码:
from selenium import webdriver
from selenium.webdriver.opera.options import Options
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
DRIVER_PATH = 'C:Python27Scriptsoperadriver.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])
窗口切换
-
在尝试切换窗口之前,请存储父窗口句柄以备将来使用。
window_before = driver.current_window_handle
-
打开新窗口后,将窗口句柄存储在列表中。
windows_after = driver.window_handles
-
在切换窗口而不是使用索引(例如
window_handles[0]
、window_handles[1]
(时,使用列表理解来定位新窗口,如下所示:new_window = [x for x in windows_after if x != window_before][0]
溶液
完整的窗口切换代码块:
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()
您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
控制台输出:
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
这可能是因为您试图切换到没有足够时间创建的选项卡。要确认这一点,请在window.open
调用后打印可用window_handles
的列表,看看它是否具有您期望的计数。如果它只列出了一个id,而您正试图访问第二个元素,它会说"name"(即tab id(必须是字符串,而它为null。
解决方案是等待选项卡的数量增加(在您打开新选项卡中的某个内容之后(,然后在满足该条件后才切换。