webdriver.close 报错问题 解决方案

在使用Appium和WebDriver进行自动化测试时,遇到在关闭窗口时出现'Method is not implemented ..'的错误。解决方案是正确使用close()和quit()方法。当有主窗口和子窗口时,应先使用close()关闭子窗口,再通过switchTo()切换回主窗口,最后使用quit()关闭所有关联窗口。注意quit()会结束整个浏览器会话,而close()仅关闭当前窗口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决方案转载自:

 http://blog.csdn.net/aerchi/article/details/43985099

使用appium wd.close()时遇见报错:

Method is not implemented ..




在使用webdriver操作时,点击主窗口的链接,会弹出子窗口。此时就会有两个窗口,如主窗口为mainWindow,子窗口为detailWindow。

1、使用detailWindow = mainWindow.switchTo().window(detailWindowHandle); 切换并定位到子窗口中。

2、然后关闭子窗口,调用detailWindow.quit(),

3、mainWindow.switchTo().window(mainWindowHandle); 切换回到父窗口

4、mainWindow.quit();关闭父窗口

程序执行到第四步时,出错了,提示类似“no a session ……”错误信息,

查了一下网上的webdreiver 的 api,才发现,

quit()详细信息:  “Quits this driver, closing every associated window”,执行这个方法后,driver会关闭所有关联窗口。

close()详细信息:Close the current window, quitting the browser if it's the last window currently open  ,此方法是关闭当前窗口,或最后打开的窗口。


部分代码如下:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. //   author: aerchi, aerchi@gmail.com  
  2.                      Set<String> handles = mainWindow.getWindowHandles();  
  3.                      Out.println(handles);  
  4.                               
  5.                      Out.println("Change to detail page");  
  6. //                   detailWindow=WindowUtil.getPopupWindow(mainWindow);  
  7.                      Iterator<String> itWin = handles.iterator();  
  8.                           while(itWin.hasNext()) {  
  9.                             String key = itWin.next();  
  10.                             if (mainWindowHandle.equals(key)) {  
  11.                                 continue;  
  12.                             }  
  13.                             detailWindowHandle = key;  
  14.                          }  
  15. //   author: aerchi, aerchi@gmail.com  
  16.                      detailWindow = mainWindow.switchTo().window(detailWindowHandle);                      
  17. //                   detailWindowHandle = WindowUtil.getWindowHandle(detailWindow);   
  18.                       
  19.                      detailWindow.close();  
  20.                      handles.remove(detailWindowHandle);  
  21.                        
  22.                      //切换到主窗口  
  23.                      mainWindow = mainWindow.switchTo().window(mainWindowHandle);  
  24. //   author: aerchi, aerchi@gmail.com  

 
 



1. webDriver.Close() - Close the browser window that the driver has focus of //关闭当前焦点所在的窗口
2. webDriver.Quit() - Calls dispose //调用dispose方法
3. webDriver.Dispose() Closes all browser windows and safely ends the session 关闭所有窗口,并且安全关闭session


弄明白了,这两个方法的不同,以上错误信息就能迎刃而解了。

大功造成,特此记录一下。


C:\Users\czadmin\AppData\Local\Programs\Python\Python313\python.exe C:\Users\czadmin\PyCharmMiscProject\script.py Traceback (most recent call last): File "C:\Users\czadmin\PyCharmMiscProject\script.py", line 4, in <module> driver = webdriver.Chrome() File "C:\Users\czadmin\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__ RemoteWebDriver.__init__( ~~~~~~~~~~~~~~~~~~~~~~~~^ self, ^^^^^ ...<2 lines>... keep_alive=keep_alive), ^^^^^^^^^^^^^^^^^^^^^^^ desired_capabilities=desired_capabilities) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\czadmin\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__ self.start_session(capabilities, browser_profile) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\czadmin\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Users\czadmin\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute response = self.command_executor.execute(driver_command, params) File "C:\Users\czadmin\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute return self._request(command_info[0], url, body=data) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\czadmin\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 397, in _request resp = self._conn.request(method, url, body=body, headers=headers) File "C:\Users\czadmin\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\_request_methods.py", line 143, in request ret
最新发布
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值