SampoValo 2022-02-04 19:55 采纳率: 100%
浏览 975
已结题

Selenium关于invalid session id错误

请问一下,我使用Selenium跟Python做网页自动化但是随着几个帐号登入后常会遇到session id invalid的问题导致程序停止
我在执行中已经使用try except去handle所有的错误了但是还是无法解决这个问题

标识错误程序码片段

try:
            time.sleep(5)

            # Geetest Image
            self.waitGeetest.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[69]/div[2]/div[6]/div/div[1]/div[2]/div[2]')))
        
        except TimeoutException:
            print('Geetest验证视窗等待超时,验证失败重新启动')
            self.browser.close()
            self.login()
        except NoSuchElementException:
            print('Geetest验证视窗未载入,验证失败重新启动')
            self.browser.close()
            self.login()
        except Exception as e:
            print('Geetest验证视窗开启失败,重新启动...意外发生')
            with open('./data/Error_Record.txt', 'a+', encoding='utf-8') as f:
                f.write('Geetest未知错误: ' + Account + ' 使用代理: ' + self.ProxyString + '\n')
                f.close()
             # 截取当前超时页面画面存档
            self.browser.get_screenshot_as_file('./screenshot//' + self.UserID + '_LoginGeetestError.png')
            self.browser.close()
            self.login()  # 重新启动登入程序
        else:
            print('Geetest窗口已开启,进行自动验证中...')
            self.GeeTest('//*[@id="region-profile-menu"]/div/div[1]/div[1]/div[1]')  # Royalty Icon

错误信息

Traceback (most recent call last):
  File "d:\Programming\Python\StormBot\start.py", line 183, in login
    self.waitGeetest.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[69]/div[2]/div[6]/div/div[1]/div[2]/div[2]')))
  File "C:\Users\Ricky\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
Backtrace:
        Ordinal0 [0x00626903+2517251]
        Ordinal0 [0x005BF8E1+2095329]
        Ordinal0 [0x004C2848+1058888]
        Ordinal0 [0x004ED448+1233992]
        Ordinal0 [0x004ED63B+1234491]
        Ordinal0 [0x00517812+1406994]
        Ordinal0 [0x0050650A+1336586]
        Ordinal0 [0x00515BBF+1399743]
        Ordinal0 [0x0050639B+1336219]
        Ordinal0 [0x004E27A7+1189799]
        Ordinal0 [0x004E3609+1193481]
        GetHandleVerifier [0x007B5904+1577972]
        GetHandleVerifier [0x00860B97+2279047]
        GetHandleVerifier [0x006B6D09+534521]
        GetHandleVerifier [0x006B5DB9+530601]
        Ordinal0 [0x005C4FF9+2117625]
        Ordinal0 [0x005C98A8+2136232]
        Ordinal0 [0x005C99E2+2136546]
        Ordinal0 [0x005D3541+2176321]
        BaseThreadInitThunk [0x7595FA29+25]
        RtlGetAppContainerNamedObjectPath [0x77047A9E+286]
        RtlGetAppContainerNamedObjectPath [0x77047A6E+238]


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\Programming\Python\StormBot\start.py", line 114, in login
    self.browser.get(self.LoginUrl)
  File "C:\Users\Ricky\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 436, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\Ricky\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Ricky\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response       
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id

  • 写回答

2条回答 默认 最新

查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月16日
  • 已采纳回答 3月8日
  • 创建了问题 2月4日

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。