基本上,我有一个java程序,使用Selenium (Chromedriver)从网站获取数据
同样的java程序在windows上没有任何错误,也就是说我没有犯任何错误。在CentOS 7中,它可以工作,但会抛出一些错误。是奇怪。
错误:
Exception in thread "Thread-6" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. 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 C hrome has crashed.)
Host info: host: 'instance-20221128-2304', ip: '10.0.0.105'
Build info: version: '4.6.0', revision: '79f1c02ae20'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-1160.76.1.el7.x86_64', java.version: '1.8.0_352'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--headless], exten sions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [--headless], extensions: [ ]}}}]
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:146)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:101)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:156)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:541)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:242)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:157)
at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:101)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:81)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:70)
at org.hinoob.bot.BangerFM.fetch(BangerFM.java:55)
at org.hinoob.bot.BangerFM.lambda$startLoop$0(BangerFM.java:26)
at java.lang.Thread.run(Thread.java:750)`
我已经按照这个指南安装了这些东西:https://www.usessionbuddy.com/post/How-To-Install-Selenium-Chrome-On-Centos-7/
我的google chrome版本在CentOS:Google Chrome 107.0.5304.121
我什么都没尝试,因为我不知道该尝试什么。我希望它能像Windows一样工作。
Selenium 4.6.0发布!介绍selenium Manager现在你不需要添加驱动程序路径或任何东西。现在selenium自动更新驱动程序。
在你的情况下,我猜这个问题是chrome驱动程序兼容性问题。
更新你的selenium版本,如果它是一个maven项目,那么添加以下依赖项
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.6.0</version>
</dependency>
如果您手动添加了chromedriver路径,则删除系统属性,希望您的问题将得到解决。
另一个导致打开浏览器问题的检查点
- 确保chrome安装在server/pc
- 如果服务器是headless,那么添加参数——headless
- 添加禁用GPU参数
参考https://www.selenium.dev/blog/2022/introducing-selenium-manager/