简体   繁体   中英

Cannot run java selenium app inside docker on aws lambda - chrome/chromedriver problems

I want to run a little project like this:

System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
ChromeOptions options = new ChromeOptions();
//... optionsSetupHere
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get("https://www.wikipedia.org");
driver.quit();
return "done";

Inside docker container on an aws lambda serverless However no matter what I do I am still getting one of those:

1)

[1669291624.447][SEVERE]: bind() faiChromeDriverl ed: Cannot asswas started successfulign requested address (99)
Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: crashed.
  (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.) 
[1669276586.713][SEVERE]: CreatePlatformSocket() failed: Address family not supported by protocol (97)
ChromeDriver was started successfully.
[1669276589.300][SEVERE]: CreatePlatformSocket() failed: Address family not supported by protocol (97)
Could not start a new session. Response code 500. Message: unknown error: unable to discover open window in chrome

I have not a slightest idea what else might be wrong. I know a lot of people faced problems with chrome and chromedriver but none worked for me. So here I list all the chromedriver options I have already tried:

options.addArguments("--headless");
options.addArguments("--start-maximized");
options.addArguments("--no-sandbox");
options.addArguments("--test-type");
options.addArguments("--ignore-certificate-errors");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-default-apps");
options.addArguments("--disable-extensions-file-access-check");
options.addArguments("--incognito");
options.addArguments("--disable-infobars");
options.addArguments("--disable-gpu");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-notifications");
options.addArguments("--window-size=1980,1080");
options.addArguments("--single-process");
options.addArguments("--disable-extensions");
options.addArguments("--allow-running-insecure-content");
options.addArguments("--disable-web-security");
options.addArguments("--user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"");

My setup: Selenium: 4.6.0 Chromedriver: 107.0.5304.62 Chrome: 107.0.5304.87-1 jdk8

Base image I am using is markhobson/maven-chrome My dockerfile is essentially the above + Lambda runtime emulator if anyone finds it useful here is the link

FROM --platform=linux/amd64 markhobson/maven-chrome:jdk-8 as base
FROM base as build
WORKDIR /src
ADD pom.xml .
RUN mvn dependency:go-offline dependency:copy-dependencies
ADD . .
RUN mvn package

# Create final image
FROM base
WORKDIR /function

COPY --from=build /src/target/dependency/*.jar ./
COPY --from=build /src/target/*.jar ./

# Lambda env config
ENTRYPOINT [ "/usr/local/openjdk-8/bin/java", "-cp", "./*", "com.amazonaws.services.lambda.runtime.api.client.AWSLambda" ]
CMD [ "example.App::sayHello" ]

I can add that it works fine with headless chromium instead of chrome, but the whole point of me touching this is to switch from headless-chromium to chrome.

I've also tried putting both chrome and chromedriver in/tmp directory even though it shouldnt matter since it is in docker container

I can also add I have tired using this link and installing java in these image and swapping handler but it gave the same error (even though the python script from the repo works as intended)

It is also fair to point out that while I was still trying to do this on amazon base image for java docker lambdas ( link ) I could not get chrome installed via yum, and on there I got yet other error: could not find chrome binary at the path it was located. The chrome binary was jus extracted like here: link

Probably forgot to mention many other solutions I have tried but the post is long anyways.

If anyone could help I'd be forever in your debt

Try using WebDriverManager library instead of taking care of the webdriver manually. Add this dependency:

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.3.1</version>
</dependency>

Instead of:

System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");

Put this:

WebDriverManager.chromedriver().setup();

The library will automatically detect your OS and download the correct webdriver. Let me know what is the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM