2

The code is working fine sometimes, but randomly i am getting the following error message

selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id

THE CODE

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def login(email,password):
    driver.get("https://accounts.google.com")
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="identifierId"]'))
        )
        element.send_keys(email)
    except:
        driver.close()
    driver.find_element_by_xpath('//*[@id="identifierNext"]').click()

    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input'))
        )
        element.send_keys(password)
    except:
        driver.close()
    driver.find_element_by_xpath('//*[@id="passwordNext"]').click()
    driver.get("https://youtube.com")
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="action-button"]/yt-button-renderer/a'))
        )
        element.click()
    except:
        driver.close()

Selenium version: 3.141.0

Chromedriver version:86.0.4240.22

milmoi
  • 21
  • 1
  • 4

1 Answers1

-1

I know this post is maybe out of date, but the solution stays the same and it has a good explanation. https://stackoverflow.com/a/56492149/14928211

let me know if this is not the answer for your question. Then I will continue searching to the solution.

  • yea i saw it already, but it didn't seem to fit – milmoi Jan 02 '21 at 17:16
  • 1
    I got the same issue though it was due to being in headless mode. If it is headless, you should always make sure to set the user-agent as many sites end up blocking headless chrome, and it can be a real mind bender like what happened to me, carving up 2 days of my time. See my answer here: https://stackoverflow.com/a/69464060/1871891 – user1871891 Oct 06 '21 at 10:56