当前位置: 首页 > 知识库问答 >
问题:

硒。常见的例外情况。WebDriverException:消息:使用Selenium和ChromeDriver并通过Python使用Chrome的会话id无效

金子平
2023-03-14

我正在使用Selenium编写一些代码,有一次我提出了7个请求,所有请求都指向不同的网站。对于第一个,这很好。但是,对于其他人,我得到一个会话ID错误。我认为我的浏览器配置正确,因为我确实从第一个网站获得了结果。我试着在两个请求之间放置一个WebDriverWait,但没有效果。我想这些网站可能会阻止我的请求。有人知道如何解决这个问题吗?

我很抱歉,如果这是愚蠢的事情,或者如果我做错了什么,我是新来的^^

提前谢谢!

Traceback (most recent call last):
  File "/home/cena/PycharmProjects/Frikandelbroodje/main.py", line 56, in <module>
    dirk_price = get_price(dirk_url, dirk_classname)
  File "/home/cena/PycharmProjects/Frikandelbroodje/main.py", line 44, in get_price
    browser.get(url)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid session id
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.15.0-50-generic x86_64)

共有2个答案

锺玺
2023-03-14

浏览器页面崩溃可能导致InvalidSessionIdeException。Selenium对我们说:由于页面崩溃而删除了会话。检查出现错误时浏览器页面是否仍然存在。

以下是本案例的回溯示例:

[2021-06-28 15:05:43,787: ERROR/ForkPoolWorker-2] Message: invalid session id
Traceback (most recent call last):
  ...
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: session deleted because of page crash
from tab crashed
  (Session info: chrome=83.0.4103.61)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  ...
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 580, in find_elements_by_class_name
    return self.find_elements(by=By.CLASS_NAME, value=name)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 1007, in find_elements
    'value': value})['value'] or []
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/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

如果您想了解一些技术细节,请查看Chromium源代码,其中可以找到由于页面崩溃而删除的字符串会话。

巢宏富
2023-03-14

无效会话ID错误是当服务器无法识别唯一会话标识符时发生的WebDriver错误。如果会话已被删除或会话ID无效,则会发生这种情况。

可以通过以下任一方式删除WebDriver会话:

>

>

  • 代码块:

    from selenium import webdriver
    from selenium.common.exceptions import InvalidSessionIdException
    
    driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    print("Current session is {}".format(driver.session_id))
    driver.quit()
    try:
        driver.get("https://www.google.com/")
    except Exception as e:
        print(e.message)
    

    控制台输出:

    Current session is a9272550-c4e5-450f-883d-553d337eed48
    No active session with ID a9272550-c4e5-450f-883d-553d337eed48
    

    隐式会话删除:当您关闭调用close()方法的最后一个窗口或选项卡时,WebDriver会话将被隐式删除,如下所示:

    >

  • 代码块:

    driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    print("Current session is {}".format(driver.session_id))
    # closes current window/tab
    driver.close()
    try:
        driver.get("https://www.google.com/")
    except Exception as e:
        print(e.message)
    

    控制台输出:

    Current session is a9272550-c4e5-450f-883d-553d337eed48
    No active session with ID a9272550-c4e5-450f-883d-553d337eed48
    

    由于第一个请求工作正常,但对于其他请求,您会收到会话ID错误,很可能是WebDriver控制的Web浏览器被检测到,从而阻止了下一个请求。

    WebDriver控制的Web浏览器被检测到并同时被阻止有不同的原因。您可以在以下内容中找到一些详细的讨论:

    • recaptcha 3如何知道我在使用硒/铬驱动程序

  •  类似资料: