【问题标题】:Python Selenium Error: invalid session idPython Selenium 错误:无效的会话 ID
【发布时间】:2022-01-19 01:31:47
【问题描述】:

我尝试启动网络驱动程序 -> 随机时间睡眠 -> 关闭网络驱动程序 但它发生了“无效的会话ID”

有人知道如何解决这个问题吗?,请

这是下面的代码

    from tkinter import *
    from tkinter import messagebox
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    import pyautogui
    import chromedriver_autoinstaller
    import datetime
    import time
    import pyperclip
    import csv, os
    import sys
    import random
    
    mobile_emulation = { "deviceName": "iPhone 6/7/8" }
    
    
    chrome_ver = chromedriver_autoinstaller.get_chrome_version().split('.')[0]  
    chromedriver_path = f'./{chrome_ver}/chromedriver.exe'
    
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    options.add_experimental_option("mobileEmulation", mobile_emulation)
    
    driver = webdriver.Chrome(chromedriver_path, chrome_options = options)
    
    url = 'https://google.com/'
    
    driver.get(url)
    
    rndNum = random.randint(350,700)
    
    now = time.localtime()
    nowTime = str(now.tm_hour)+'HOUR'+str(now.tm_min)+'MIN'+str(now.tm_sec)+'SEC'
    
    print('------------------> RANDOM TIME : ',rndNum//60,'MIN ', rndNum%60, 'SEC')
    print("DRIVER FINISH START", nowTime)
    
    
    time.sleep(rndNum)
    
    driver.close()
    
    driver.implicitly_wait(5)
    
    
    time.sleep(1)
        
    driver.switch_to.window(driver.window_handles[0])
    driver.implicitly_wait(5)
    
    isSearch = 1
    now = time.localtime()
    nowTime = str(now.tm_hour)+'HOUR'+str(now.tm_min)+'MIN'+str(now.tm_sec)+'SEC'
    
    print("DRIVER FINISH END", nowTime)

这是错误

Traceback (most recent call last):

  File "C:/Users/Desktop/testDriver.py", line 47, in <module>
    driver.implicitly_wait(5)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 911, in implicitly_wait
    self.execute(Command.SET_TIMEOUTS, {
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id

【问题讨论】:

    标签: python selenium-webdriver webdriver


    【解决方案1】:

    您收到该错误是因为您在调用driver.implicitly_wait(5) 之前调用了driver.close()。您不能关闭最后一个/唯一的浏览器窗口,然后使用带有driver 的命令。要么不要关闭浏览器窗口,要么先打开一个新窗口。

    要打开一个新的浏览器窗口,请使用:

    driver.execute_script("window.open('');")
    

    【讨论】:

    • 我先做了“driver.implicitly_wait(5)”,然后放了 driver.close() 但还是不行
    • 你有第二个driver.implicitly_wait(5),在第一个后面几行。尝试在程序的最后一行之前根本不关闭驱动程序。
    • 解决了!谢谢
    猜你喜欢
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2021-04-09
    • 2019-09-26
    • 1970-01-01
    • 2019-10-22
    相关资源
    最近更新 更多