iam trying to use the Selenium Webdriver on Ubuntu 20.04 with Google Chrome 116.0.5845.96 and the matching chromedriver.
Some Java Code that is supposed to start the driver:
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class KASPwebcrawler {
public int getAverageProgressValue() throws InterruptedException {
// Für Headless
ChromeOptions options = new ChromeOptions();
options.addArguments("--log-level=DEBUG");
options.addArguments("--headless");
System.setProperty("chromedriver", "/Users/XXX/Downloads");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get("https://web.ankiapp.com/auth");
// Aufrufen Auth Tab
WebElement loginPage = driver.findElement(By.xpath("//*[text()='Einloggen']"));
loginPage.click();
Thread.sleep((long) (10000 * Math.random()));
// Eingeben der Login Info
WebElement username = driver.findElement(By.id("ion-input-3"));
WebElement password = driver.findElement(By.id("ion-input-4"));
username.sendKeys("XXX");
password.sendKeys("XXX");
// Login
List<WebElement> elements = driver.findElements(By.xpath("//*[text()='Einloggen']"));
WebElement login = elements.get(1);
login.click();
// Landen auf */home Website sicherstellen
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(webDriver -> webDriver.getCurrentUrl().contains("/home"));
// Sleep nach Login
Thread.sleep((long) (3000 * Math.random() + 1000));
// Herunterladen von Deck: Datenbanken
getDeck("Datenbanken", driver);
getDeck("Info II.3", driver);
getDeck("Methoden II", driver);
// Liste für spätere Berechnung der Werte
List<Integer> valueList = new ArrayList<Integer>();
// Progress bei Decks auslesen
for (WebElement element : getProgressValueList(driver)) {
valueList.add(getTwoDecimals(element.getText()));
}
System.out.println("Durchschnittswert: " + getAverage(valueList));
// Beendet den Webdriver
return getAverage(valueList);
}
public void getDeck(String pDeckName, WebDriver pDriver) throws InterruptedException {
String deckName = ("//*[text()='" + pDeckName + "']");
// Herunterladen von Deck: Datenbanken
WebElement deckButton = pDriver.findElement(By.xpath(deckName));
deckButton.click();
Thread.sleep((long) (3000 * Math.random() + 1000));
WebElement downloadButton = pDriver.findElement(By.xpath("//*[text()='Herunterladen']"));
downloadButton.click();
Thread.sleep((long) (3000 * Math.random() + 1000));
pDriver.navigate().back();
Thread.sleep((long) (1000 * Math.random() + 1000));
}
public int getTwoDecimals(String pProgressValueString) {
String result = "0";
if (!pProgressValueString.isBlank()) {
String[] splitProgressValueString = new String[2];
splitProgressValueString = pProgressValueString.split("%");
result = splitProgressValueString[0];
}
return Integer.parseInt(result);
}
public List<WebElement> getProgressValueList(WebDriver pDriver) {
List<WebElement> progressValues = pDriver.findElements(By.className("end-slot"));
return progressValues;
}
public int getAverage(List<Integer> pProgressValueList) {
int result = 0;
int countActualValues = 0;
for (Integer zahl : pProgressValueList) {
if (zahl != 0) {
countActualValues += 1;
result += zahl;
}
}
return result / countActualValues;
}
}
When i start the java code it first tells me that the driver was started sucessfully, but the chrome session can not be initiated.
Errors read like that:
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier.
SLF4J: Ignoring binding found at [jar:file:/home/gregor/my-app-all.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See https://www.slf4j.org/codes.html#ignoredBindings for an explanation.
Starting ChromeDriver 116.0.5845.96 (1a391816688002153ef791ffe60d9e899a71a037-refs/branch-heads/5845@{#1382}) on port 20445
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: 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.)
Host info: host: 'VMdrei', ip: '10.2.0.4'
Build info: version: '4.9.0', revision: 'd7057100a6'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.0-1042-azure', java.version: '17.0.8'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*, --log-level=DEBUG, --headless], extensions: []}}]}]
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:136)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:94)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:68)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:165)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:183)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:158)
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:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:84)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:73)
at KASPgradle.KASPwebcrawler.getAverageProgressValue(KASPwebcrawler.java:24)
at KASPgradle.KASPwebcrawlerRunner.run(KASPwebcrawlerRunner.java:7)
at KASPgradle.KASPwebcrawlerRunner.main(KASPwebcrawlerRunner.java:13)
I think i have narrowed it down to the chromedriver not being able to interact with chrome. Both the driver and and browser are properly installed. Headless tests with the browser on its own worked as well. Thanks for help in advance.
I wanted to webscrape a Site with selenium on a ubuntu 20.04 VM
2
Answers
I changed the code according to Shawns suggestions. Still the errors look like this:
SLF4J: No SLF4J providers were found. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details. SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier. SLF4J: Ignoring binding found at [jar:file:/home/gregor/my-app-all.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See https://www.slf4j.org/codes.html#ignoredBindings for an explanation. Exception in thread "main" 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 /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Host info: host: 'VMdrei', ip: '10.2.0.4' Build info: version: '4.11.0', revision: '040bc5406b' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.0-1042-azure', java.version: '17.0.8' Driver info: org.openqa.selenium.chrome.ChromeDriver Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*, --log-level=DEBUG, --headless=new], binary: /opt/google/chrome/google-c..., extensions: []}}]}] at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:140) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:96) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:68) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:196) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:171) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:232) at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:159) at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:108) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:88) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:83) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:72) at KASPgradle.KASPwebcrawler.getAverageProgressValue(KASPwebcrawler.java:24) at KASPgradle.KASPwebcrawlerRunner.run(KASPwebcrawlerRunner.java:7) at KASPgradle.KASPwebcrawlerRunner.main(KASPwebcrawlerRunner.java:13)
Could it be that iam Hardware capped? Iam Running this thing on an azure VM (Standard B2s (2 vcpus, 4 GiB RAM)
This is incorrect:
System.setProperty("chromedriver", "/Users/XXX/Downloads");
It should be:
Having said that, I see you are using selenium
v4.9.0
, if you upgrade to the latest version of selenium which isv4.11.0
, then you do not have to set the path ofchromedriver.exe
manually. Selenium will programatically download and manage drivers for you including the latest chrome version116
. You can simply get rid ofSystem.setProperty
line.https://stackoverflow.com/a/76463081/7598774