0

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

ayrar
  • 111
  • 1
  • 5

1 Answers1

1

This is incorrect: System.setProperty("chromedriver", "/Users/XXX/Downloads");

It should be:

System.setProperty("webdriver.chrome.driver", "/Users/XXX/Downloads");

Having said that, I see you are using selenium v4.9.0, if you upgrade to the latest version of selenium which is v4.11.0, then you do not have to set the path of chromedriver.exe manually. Selenium will programatically download and manage drivers for you including the latest chrome version 116. You can simply get rid of System.setProperty line.

https://stackoverflow.com/a/76463081/7598774

Shawn
  • 4,064
  • 2
  • 11
  • 23
  • 1
    `~/.cache` go to this location and try to delete any existing `chromedriver.exe` folders. Then retry, see if that works. – Shawn Aug 18 '23 at 14:50
  • Unfortenately it still doesnt work. When i try normal mode for google chrome it shows the error message 500 as shown above. In headless it seems to start but it cannot work with the xpath arguments: "Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //*[contains(text(),'Einloggen')] (tried for 10 second(s) with 500 milliseconds interval)" – ayrar Aug 21 '23 at 11:33
  • 1
    If it works in `headless` mode, am pretty sure it should work in normal mode too. About the timeout exception you are getting, that's a different issue all together. I suggest you create a new question for that. – Shawn Aug 21 '23 at 13:31
  • SOLVED: thanks for all the help shawn, i ran the java code without sudo. apparently sudo has an effect on graphic based programs(environments?). plus your adjustments. – ayrar Aug 21 '23 at 15:14
  • Glad its solved. [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers#:~:text=To%20mark%20an%20answer%20as,the%20answer%2C%20at%20any%20time) – Shawn Aug 21 '23 at 16:32