Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I tried to start web driver -> randomly time sleep -> close web dirver But it occured "invalid session id"

Does anyone know how to fix this problem?, plz

this is the following code

    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)

and this is the error

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

You are getting that error because you called driver.close() before calling driver.implicitly_wait(5). You cannot close the last/only browser window and then use commands with the driver. Either don't close the browser window, or open up a new window first.

To open up a new browser window, use:

driver.execute_script("window.open('');")
                You have a second driver.implicitly_wait(5) a few lines down from the first one. Try not closing the driver at all until the last line of the program.
– Michael Mintz
                Jan 19, 2022 at 2:13
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.