selenium.common.exceptions.WebDriverException:no such session

Traceback (most recent call last):
  File "******.py", line 74, in <module>
    browser.switch_to_window(mainHandle)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 568, in switch_to_window
    self._switch_to.window(window_name)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\switch_to.py", l
ine 112, in window
    self._driver.execute(Command.SWITCH_TO_WINDOW, data)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: no such session
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cf
d9),platform=Windows NT 6.1.7601 SP1 x86_64)

这是报错的详细信息,程序有时候执行了比较长的时间,如半小时甚至一小时才会报这个错误,有时候就是几分钟就报错了,不知道为什么,原因在哪里?然后附上我的部分代码

mainHandle=browser.current_window_handle
for i in range(1,21):
        file_href=browser.find_element_by_xpath("//*[@id='ctl00']/table/tbody/tr[2]/td/table/tbody/tr["+str(i)+"]/td[2]/a")    
        file_href.click()
        handles=browser.window_handles
        for handle in handles:
            if handle != mainHandle:
                browser.switch_to_window(handle)
                try:
                    pdf_element=browser.find_element_by_xpath("//*[@id='QK_nav']/ul/li[2]/a/b")
                    if pdf_element != None:
                        pdf_element.click()
                        print("*  ",end='')
                        time.sleep(5)
                        break
                except:
                    f=open("C:\\logs\\log","a")
                    traceback.print_exc(file=f)
                    f.flush()
                    f.close()
                    time.sleep(5)                    
        browser.close()
        browser.switch_to_window(mainHandle)
        browser.switch_to_frame("iframeResult")
        time.sleep(2)

补充说明:本来我是在ubuntu上面操作的,结果我的谷歌浏览器下载中文文件名乱码,一直没有解决,谷歌浏览器是当前最新版55,。也有火狐,因为火狐下载文件,会有弹窗,不知如何避免弹窗,最后实在没有其他办法,就用Windows系统了,怀疑是Windows不稳定,但是水平太菜,不知上面的报错如何解决。希望大神帮忙解决阿,远程调试也可以,比较急,所以希望很快解决

阅读 12k
1 个回答
browser.close()
browser.switch_to_window(mainHandle)
browser.switch_to_frame("iframeResult")

应该是browser对象关闭之后你又使用了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题