作者:mobiledu2502890657 | 来源:互联网 | 2023-09-05 21:17
问题:selenium.common.exceptions.WebDriverException:Message:unknownerror:Chromefailedtostart:
问题:selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn‘t exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.
解决方法:设置为不用打开图形界面运行浏览器,代码如下:
#使用谷歌浏览器
def getChromeDriver(self):
chrome_options = webdriver.ChromeOptions() #为驱动加入无界面配置
chrome_options.add_argument(‘--headless‘) #–headless”参数是不用打开图形界面
chrome_options.add_argument(‘--no-sandbox‘) #“–no - sandbox”参数是让Chrome在root权限下跑
# chrome_options.add_argument(‘--disable-dev-shm-usage‘) #不加载图片, 提升速度
# chrome_options.add_argument(‘blink-settings=imagesEnabled=false‘)
# chrome_options.add_argument(‘--disable-gpu‘) # 谷歌文档提到需要加上这个属性来规避bug
# chromedriver = webdriver.Chrome(chrome_optiOns=chrome_options)
# chromedriver = webdriver.Chrome() # 需要把驱动所在路径配置到系统环境变量里
path = r"%s/driver/chromedriver"% str(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) ) #配置驱动路径
print("path:%s" % path)
# path = r"D:\Users\Administrator\PycharmProjects\webtestdata\TestCaseFunction\driver\chromedriver.exe" #配置驱动路径
# option = webdriver.ChromeOptions()
# option.add_argument(‘--user-data-dir=C:\\Users\\Administrator\\Local\\Google\\Chrome\\User Data\\Default‘) # 设置成用户自己的数据目录
# #浏览器输入chrome://version 下个人资料路径就是自己的数据目录
# chromedriver = webdriver.Chrome(chrome_optiOns=chrome_options)
chromedriver = webdriver.Chrome(executable_path=path,chrome_optiOns=chrome_options)
# chromedriver = webdriver.Chrome(executable_path=path)
# chromedriver = webdriver.Chrome(executable_path=path)
chromedriver.maximize_window() #窗口最大化
self.delayTime(5)
return chromedriver
其中“chrome_options.add_argument(‘--headless‘) #–headless”参数是不用打开图形界面”这行是关键,意思是设置为不用打开图形界面运行,
其中chromedriver = webdriver.Chrome(executable_path=path,chrome_optiOns=chrome_options)这行中的“chrome_optiOns=chrome_options”是将上面设置的参数添加到驱动中,
否则 “不用打开图形界面运行”的设置不会生效,就是一直报"Chrome has crashed"的错。
centos7在命令界面使用命令可以执行,但在jenkins中输入命令报Chrome has crashed.