我想运行这样一个小项目:
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";
在aws lambda serverless上的docker容器内部然而,无论我做什么,我仍然得到其中一个:
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
我一点也不知道还有什么可能是错的。我知道很多人在使用chrome和chromedriver时遇到了问题,但没有一个对我有效。因此,在这里我列出了我已经尝试过的所有chromedriver选项:
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"");
我的设置:硒:4.6.0Chromedriver: 107.0.5304.62铬:107.0.5304.87-1jdk8
我使用的基础图像是markhobson/maven-chrome我的dockerfile本质上是以上+ Lambda运行时模拟器,如果有人发现它有用,这里是链接
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" ]
我可以补充说,它可以很好地使用无头铬而不是铬,但我触摸这一点的全部意义是从无头铬切换到铬。
我也试过把chrome和chromedriver在/tmp目录,即使它不应该重要,因为它是在docker容器
我还可以添加我已经厌倦了使用这个链接并在这些图像中安装java并交换处理程序,但它给出了相同的错误(即使来自repo的python脚本按预期工作)
这也是公平的指出,虽然我仍然试图这样做在亚马逊的java docker lambdas(链接)的基础图像,我不能得到chrome安装通过yum,在那里我得到了另一个错误:无法找到chrome二进制在它所处的路径。chrome二进制文件被提取如下:link
可能忘记提到我尝试过的许多其他解决方案,但无论如何,这篇文章很长。
如果有人可以帮助我,我将永远欠你的债
尝试使用WebDriverManager库而不是手动处理webdriver。添加这个依赖项:
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.1</version>
</dependency>
代替:
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
把这个:
WebDriverManager.chromedriver().setup();
库将自动检测您的操作系统并下载正确的web驱动程序。让我知道结果如何。