【问题标题】:Getting invalid session id error randomly [duplicate]随机获取无效会话ID错误[重复]
【发布时间】:2021-04-09 00:02:17
【问题描述】:

代码有时运行良好,但随机我收到以下错误消息

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

代码

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()

硒版本:3.141.0

Chrome驱动版本:86.0.4240.22

【问题讨论】:

  • 你能解决这个问题吗?

标签: python python-3.x selenium automation selenium-chromedriver


【解决方案1】:

我知道这篇文章可能已经过时了,但解决方案保持不变并且有很好的解释。 https://stackoverflow.com/a/56492149/14928211

如果这不是您问题的答案,请告诉我。然后我会继续寻找解决方案。

【讨论】:

  • 是的,我已经看过了,但似乎不合适
  • 我遇到了同样的问题,虽然这是由于处于无头模式。如果它是无头的,您应该始终确保设置用户代理,因为许多站点最终会阻止无头 chrome,并且它可能像发生在我身上的事情一样令人费解,占用了我 2 天的时间。在这里查看我的答案:stackoverflow.com/a/69464060/1871891
猜你喜欢
  • 2014-07-04
  • 2015-05-26
  • 2019-09-23
  • 1970-01-01
  • 2010-10-20
  • 1970-01-01
  • 2011-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多