python - selenium.common.exceptions.WebDriverException : Message: invalid session id using Selenium with ChromeDriver and Chrome through Python

标签 python selenium google-chrome webdriver selenium-chromedriver

我正在使用 Selenium 编写一些代码,有一次我向不同的网站发出了 7 个请求。对于第一个,这效果很好。但是,对于其他人,我收到 session 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)

最佳答案

invalid session id

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

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

  • Explicit session deletion :显式调用 quit() 方法时会显式删除 WebDriver session ,如下所示:

    • 代码块:

      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 session 将隐式删除,如下所示:

    • 代码块:

      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
      
<小时/>

结论

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

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

关于python - selenium.common.exceptions.WebDriverException : Message: invalid session id using Selenium with ChromeDriver and Chrome through Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60038349/

相关文章:

java - 如何提高递归函数性能?

jquery - Firefox 和 Chrome CSS 绝对定位问题

Python Matplotlib 点颜色

python - 如何在 Python 中导入没有类的模块?

python - 如何在 matplotlib 中为数据帧中的多个组添加误差线?

python - 关于 Google python 类日的解决方案的问题

python - 使用 Selenium 和 BeautifulSoup 提取 iFrame 内容

linux - 未知错误:在ubuntu上执行Selenium UI测试用例时,DevToolsActivePort文件不存在错误

javascript - Chrome 扩展程序将变量从 popup.js 传递到内容脚本

javascript - 如何创建 chrome(扩展)webRequest 监听器?