本来在本地运行好好的scrapy,结果移植到docker中就报错,完整报错是: selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id

大家可以在控制台搜索一下是否同时有这个报错: selenium.common.exceptions.WebDriverException: Message: unknown error: session deleted because of page crash

在启动 selenium 时添加选项:

option.add_argument('--no-sandbox')
option.add_argument('--disable-dev-shm-usage')

完整的启动selenium的示例如下:

from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('--disable-gpu')
option.add_argument('lang=zh_CN.UTF-8')
option.add_argument('headless')  # 无头浏览器
option.add_argument('--no-sandbox') # 必要!!
option.add_argument('--disable-dev-shm-usage') # 必要!!
prefs = {
    "profile.managed_default_content_settings.images": 2,  # 禁止加载图片
    # 'permissions.default.stylesheet': 2,  # 禁止加载css
option.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=option)
driver.get("https://www.baidu.com")
print(driver.page_source)

其实这个报错是可见的报错,在这之前还有一个不容易被察觉的报错:

selenium.common.exceptions.WebDriverException: Message: unknown error: session deleted because of page crash
from unknown error: cannot determine loading status
from tab crashed
  (Session info: headless chrome=94.0.4606.54)

而问题的根源就是这个报错:selenium.common.exceptions.WebDriverException: Message: unknown error: session deleted because of page crash

  1. 报错原因是:session deleted because of page crash,可知是page crash
  2. 页面崩溃导致chrome虽然启动了,但是却意外退出了,
  3. 这时,当程序使用selenium调用一个崩溃了的chrome时,就会有报错:selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id

这里大家可以做一个实验:
本机启动selenium的driver后,使用driver.quit(),手动让driver退出,再执行driver.get(“https://www.baidu.com”)
这时就会得到同样的报错:selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id
原因就是因为driver在得到页面前driver就 退出了/崩溃了/逃跑了/到异次元了

因此报错根本原因是页面崩溃。而导致页面崩溃的原因是本机的docker的共享内存默认只有64M,而且直接从docker hub上拉取的selenium,使用本机的docker打开后,共享内存依旧是默认的64M,可以使用df -h查看。
在这里插入图片描述

当chrome使用内存保存下载的页面时,第一个页面就足以让chrome崩掉了(而火狐却不会,这就是为什么网上有人改为火狐,不改代码就解决问题了,结果在chrome上提交这个bug???),因此解决问题的思路有两个,最简便的就是解决方法这里提到的:

option.add_argument('--no-sandbox')
option.add_argument('--disable-dev-shm-usage')

这个含义是使用硬盘来存储获取的内容,而不是使用内存,所以会稍稍降低分布式爬取时爬虫的速率。

还有一个解决的思路是增大设定的shm默认值,方法是运行时设置:

docker run -it --shm-size="1g" ubuntu

修改已启动的容器的shm请自行查找咯

解决docker中运行scrapy使用chrome selenium报错InvalidSessionIdException: Message: invalid session id 本来在本地运行好好的scrapy,结果移植到docker中就报错,完整报错是:selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id大家可以在控制台搜索一下是否同时有这个报错:selenium.common.exceptions.WebDriverException: Message: unknown error: session deleted because of page crash解决方法
selenium.common.exceptions.WebDriverException: Message: invalid session id 原因是在使用webdriver之前调用了 driver.close() 将webdriver关闭了,则webdriver就失效了。 原文链接:https://blog.csdn.net/djshichaoren/article/details/899...
将webdriver关闭了,则webdriver就失效了。 在使用selenium进行自动获取信息时,报错InvalidSessionIdException: Message: invalid session id 因为我需要获取多个页面的数据,让他自动进入下一页获取信息,但是我却在让他在第一页完了后就关闭了,因此会报错
使用selenium进行自动获取信息时,报错InvalidSessionIdException: Message: invalid session id 因为我需要获取多个页面的数据,让他自动进入下一页获取信息,但是我却在让他在第一页完了后就关闭了,因此会报错 去掉关闭就行了
options.add_argument(‘headless’) # 无头模式 options.add_argument(‘window-size={}x{}’.format(width, height)) # 直接配置大小和set_window_size一样 options.add_argument(‘disable-gpu’) # 禁用GPU加速 options.add_argument(‘proxy-server={}’.format(self.proxy_server)) # 配置代理 option
self.driver.find_element_by_xpath('//*[@id="tag"]/div/div[2]/div/div[5]/span').click() selenium.common.exceptions.WebDriverException: Message: unknown error: Element <span data-v-5e7cbfab=""...