我在 windows 上用 java 中的 selenium 做了一个小机器人,我试图在安装了 ubuntu 的 linux vps 上导出它。
(我从 Windows Chrome 配置文件(默认)导出到 Linux Vps 配置文件目录,但它不起作用)
但是,这个加载特定 chrome 配置文件的功能(在 Windows 上本地工作)对我不起作用。
这是我的代码:
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
public class App {
public static void main(String[] args) throws Exception {
// System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
//or
WebDriverManager.chromedriver().setup();// setup automatico e aggiornato dei driver di google chrome
WebDriver browser;
ChromeOptions options = new ChromeOptions();
// first line
options.addArguments("--user-data-dir=/home/ubuntu/.config/google-chrome/");
//second line
options.addArguments("--profile-directory=Default");
// without the above lines of code, it works (from here on down)
options.addArguments("--headless");
options.addArguments("--no-sandbox");
// options.addArguments("--disable-gpu"); //funziona anche se attivata
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-infobars");
options.addArguments("--remote-allow-origins=*");
browser = new ChromeDriver(options);
browser = new ChromeDriver(options);
browser.get("https://www.instagram.com");
//takeSnapShot(browser, "/www/wwwroot/lorenzo/SCREEEN/fanculo1.jpg");
Thread.sleep(3000);
System.out.println("Fatto ora chiudi");
browser.quit();
}
在我删除第一行和第二行注释掉代码的精确时刻,没有给出任何错误。
在其他论坛上搜索我已经验证安装的 chromedriver 和 google chrome 的版本匹配:
~$ chromedriver --version
ChromeDriver 111.0.5563.64 (c710e93d5b63b7095afe8c2c17df34408078439d-refs/branch-heads/5563@{#995})
~$ chromium --version
Chromium 111.0.5563.64 snap
~$ google-chrome --version
Google Chrome 111.0.5563.110
这是它给我的错误:
$ java -jar demo.jar
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 111.0.5563.64 (c710e93d5b63b7095afe8c2c17df34408078439d-refs/branch-heads/5563@{#995}) on port 11341
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session.
Response code 500. Message: unknown error: failed to write prefs file
Host info: host: 'vps-44b9f1aa', ip: '127.0.1.1'
Build info: version: '4.8.1', revision: '8ebccac989'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.0-67-generic', java.version: '19.0.2'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--user-data-dir=/home/ubunt..., --profile-directory=Profile_1, --headless, --no-sandbox, --disable-dev-shm-usage, --disable-infobars, --remote-allow-origins=*], extensions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [--user-data-dir=/home/ubunt..., --profile-directory=Profile_1, --headless, --no-sandbox, --disable-dev-shm-usage, --disable-infobars, --remote-allow-origins=*], extensions: []}}}]
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:148)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:106)
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:543)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:229)
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:82)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:71)
at com.example.App.main(App.java:38)
我靠你!感谢任何帮助我的人,对不起我糟糕的英语。
问题解决了!感谢@kaliiiiiiiii
你知道如何解决“警告”吗? 为什么有些重复?
$ java -jar demo.jar
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 111.0.5563.64 (c710e93d5b63b7095afe8c2c17df34408078439d-refs/branch-heads/5563@{#995}) on port 25982
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Mar 22, 2023 8:27:55 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
WARNING: Unable to find CDP implementation matching 111
Mar 22, 2023 8:27:55 PM org.openqa.selenium.chromium.ChromiumDriver lambda$new$3
WARNING: Unable to find version of CDP to use for . You may need to include a dependency on a specific version of the CDP using something similar to `org.seleniumhq.selenium:selenium-devtools-v86:4.8.1` where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's.
Starting ChromeDriver 111.0.5563.64 (c710e93d5b63b7095afe8c2c17df34408078439d-refs/branch-heads/5563@{#995}) on port 4625
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Mar 22, 2023 8:27:56 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
WARNING: Unable to find CDP implementation matching 111
Mar 22, 2023 8:27:56 PM org.openqa.selenium.chromium.ChromiumDriver lambda$new$3
WARNING: Unable to find version of CDP to use for . You may need to include a dependency on a specific version of the CDP using something similar to `org.seleniumhq.selenium:selenium-devtools-v86:4.8.1` where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's.