selenium.common.exceptions.WebDriverException:消息:使用Selenium和ChromeDriver以及通过Python通过Chrome浏览器的无效会话ID

时间:2019-06-06 18:45:50

标签: python selenium google-chrome webdriver selenium-chromedriver

我正在使用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 个答案:

答案 0 :(得分:0)

浏览器页面崩溃可能会导致 InvalidSessionIdException。 Selenium 对我们说:isnan()。当您收到错误消息时,请检查您的浏览器页面是否仍然存在。

这里有一个回溯的例子:

session deleted because of page crash

如果您需要一些技术细节,请查看 Chromium sources,您可以在其中找到字符串 [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

答案 1 :(得分:-1)

invalid session id

无效的会话ID 错误是当服务器无法识别唯一的会话标识符时发生的WebDriver error。如果会话已被删除会话ID无效

可以通过以下两种方式之一删除WebDriver会话:

  • Explicit session deletion:按如下所示显式调用quit()方法时,将显式删除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
      
  • Implicit session deletion:如下所示,当关闭最后一个调用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浏览器被检测并同时被阻止的原因多种多样。您可以在以下位置找到一些详细的讨论: