Comments (7)
@ercanku Hi, the code of module example
can't be executed directly by java -jar
, because playwright-java
need to depend on the driver playwright-cli
, so maybe you can create a new maven project and according to the README.md
.
- Add the dependency to your maven
pom.xml
.
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>0.171.0</version>
</dependency>
- Copy the code from module
example
orREADME.md
to your project. - Run or Debug your project, it will work if there is no other issues.
Please notice that it will download and install browsers if you are running playwright-java
at the first time.
from playwright-java.
Hi @codeboyzhou,
It works with mvn compile exec:java -Dexec.mainClass=org.example.PageScreenshot
which is mentioned on examples but it is not appropriate for running on a server. It should be in a packaged form. So, do you have any advice or approach for that?
P.s.: I have installed playwright-cli separately and passed the path as a property like -Dplaywright.cli.dir
but still thown the same exception.
from playwright-java.
It works with
mvn compile exec:java -Dexec.mainClass=org.example.PageScreenshot
which is mentioned on examples but it is not appropriate for running on a server. It should be in a packaged form. So, do you have any advice or approach for that?
You can either add all required dependencies to the classpath when running from jar or you can use Maven to package all dependencies in a single jar. There is plenty of ways to do it with Maven, you can look at https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven for some pointers.
P.s.: I have installed playwright-cli separately and passed the path as a property like
-Dplaywright.cli.dir
but still thown the same exception.
That should work as long as playwright.cli.dir
contains playwright-cli
executable. Note that in this case you'll have to add jars produced by driver
and playwright
modules to the classpath. What's the command line you use to launch your test?
from playwright-java.
Hi @yury-s ,
It is not a missing dependency problem. I have inspected the code in detail.
During the playwright-cli installation, it tries to list the files under driver-bundle-0.171.0.jar!/driver
path via Files.list(Paths.get(uri))
on DriverJar -> extractDriverToTempDir . If you run the project from an IDE or via mvn compile exec:java
it works fine because the driver bundle path is a default .m2 folder related path which is normal file system path.
But if you run from a jar file, that path is nested jar path and Files.list(Paths.get(uri))
does not work for it. And it throws java.nio.file.NotDirectoryException
Caused by: java.lang.RuntimeException: Failed to find playwright-cli
at com.microsoft.playwright.impl.Driver.ensureDriverInstalled(Driver.java:46) ~[driver-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.PlaywrightImpl.create(PlaywrightImpl.java:38) ~[playwright-0.171.0.jar!/:na]
at com.microsoft.playwright.Playwright.create(Playwright.java:25) ~[playwright-0.171.0.jar!/:na]
at com.prisync.apps.browser.BrowserFactory.create(BrowserFactory.java:15) ~[classes!/:1.0-SNAPSHOT]
at com.prisync.apps.Crawler.<init>(Crawler.java:26) ~[classes!/:1.0-SNAPSHOT]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_241]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_241]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_241]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_241]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:204) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
... 29 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_241]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_241]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_241]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_241]
at com.microsoft.playwright.impl.Driver.createDriver(Driver.java:61) ~[driver-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.Driver.ensureDriverInstalled(Driver.java:44) ~[driver-0.171.0.jar!/:na]
... 38 common frames omitted
**Caused by: java.nio.file.NotDirectoryException: /BOOT-INF/lib/driver-bundle-0.171.0.jar!/driver/mac
at com.sun.nio.zipfs.ZipDirectoryStream.<init>(ZipDirectoryStream.java:73) ~[zipfs.jar:1.8.0_241]
at com.sun.nio.zipfs.ZipPath.newDirectoryStream(ZipPath.java:651) ~[zipfs.jar:1.8.0_241]
at com.sun.nio.zipfs.ZipFileSystemProvider.newDirectoryStream(ZipFileSystemProvider.java:262) ~[zipfs.jar:1.8.0_241]
at java.nio.file.Files.newDirectoryStream(Files.java:457) ~[na:1.8.0_241]
at java.nio.file.Files.list(Files.java:3451) ~[na:1.8.0_241]
at com.microsoft.playwright.impl.DriverJar.extractDriverToTempDir(DriverJar.java:53) ~[driver-bundle-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.DriverJar.<init>(DriverJar.java:32) ~[driver-bundle-0.171.0.jar!/:na]
... 44 common frames omitted**
from playwright-java.
During the playwright-cli installation, it tries to list the files under
driver-bundle-0.171.0.jar!/driver
path
Yes, this is expected. The examples project does the same. Local path will only be used if you build playwright locally.
How do you build the jar file you are trying to launch? Can you share you Maven project (or whatever build system you use) and the command line you use to launch it?
For now you can use playwright.cli.dir
as a workaround. Did you manage to download playwright-cli? This script is used by our build to download the driver.
from playwright-java.
I realized that the problem is Spring Boot related. It is about Spring boot packaging.
java -jar works fine with the example mentioned the playwright-java/example repository.
I have created an Spring Boot Playwright example application in here:
https://github.com/ercanku/spring-boot-playwright
Run commands:
mvn clean package
java -jar target/spring-boot-playwright-1.0-SNAPSHOT.jar
The exception is :
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) [spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779) [spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
at com.example.app.PlaywrightSpringApplication.main(PlaywrightSpringApplication.java:11) [classes!/:1.0-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_241]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_241]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_241]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_241]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) [spring-boot-playwright-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:109) [spring-boot-playwright-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) [spring-boot-playwright-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:466) [spring-boot-playwright-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
Caused by: java.lang.RuntimeException: Failed to find playwright-cli
at com.microsoft.playwright.impl.Driver.ensureDriverInstalled(Driver.java:46) ~[driver-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.PlaywrightImpl.create(PlaywrightImpl.java:38) ~[playwright-0.171.0.jar!/:na]
at com.microsoft.playwright.Playwright.create(Playwright.java:25) ~[playwright-0.171.0.jar!/:na]
at com.example.app.PageScreenshot.fetch(PageScreenshot.java:15) ~[classes!/:1.0-SNAPSHOT]
at com.example.app.PlaywrightSpringApplication.run(PlaywrightSpringApplication.java:17) [classes!/:1.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) [spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
... 13 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_241]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_241]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_241]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_241]
at com.microsoft.playwright.impl.Driver.createDriver(Driver.java:61) ~[driver-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.Driver.ensureDriverInstalled(Driver.java:44) ~[driver-0.171.0.jar!/:na]
... 18 common frames omitted
Caused by: java.nio.file.NotDirectoryException: /BOOT-INF/lib/driver-bundle-0.171.0.jar!/driver/mac
at com.sun.nio.zipfs.ZipDirectoryStream.<init>(ZipDirectoryStream.java:73) ~[zipfs.jar:1.8.0_241]
at com.sun.nio.zipfs.ZipPath.newDirectoryStream(ZipPath.java:651) ~[zipfs.jar:1.8.0_241]
at com.sun.nio.zipfs.ZipFileSystemProvider.newDirectoryStream(ZipFileSystemProvider.java:262) ~[zipfs.jar:1.8.0_241]
at java.nio.file.Files.newDirectoryStream(Files.java:457) ~[na:1.8.0_241]
at java.nio.file.Files.list(Files.java:3451) ~[na:1.8.0_241]
at com.microsoft.playwright.impl.DriverJar.extractDriverToTempDir(DriverJar.java:53) ~[driver-bundle-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.DriverJar.<init>(DriverJar.java:32) ~[driver-bundle-0.171.0.jar!/:na]
... 24 common frames omitted
And then I used the script you mentioned, and download the driver and install cli with "npm install playwright"
Run with driver parameter :
java -Dplaywright.cli.dir=/pw-java/driver/linux -jar spring-boot-playwright-1.0-SNAPSHOT.jar
And final exception is :
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
at com.example.app.PlaywrightSpringApplication.main(PlaywrightSpringApplication.java:11) ~[classes!/:1.0-SNAPSHOT]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[spring-boot-playwright-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:109) ~[spring-boot-playwright-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[spring-boot-playwright-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:466) ~[spring-boot-playwright-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
Caused by: com.microsoft.playwright.PlaywrightException: Error: Failed to launch firefox because executable doesn't exist at /root/.cache/ms-playwright/firefox-1224/firefox/firefox
Try re-installing playwright with "npm install playwright"
Note: use DEBUG=pw:api environment variable and rerun to capture Playwright logs.
at com.microsoft.playwright.impl.WaitableResult.get(WaitableResult.java:50) ~[playwright-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.ChannelOwner.lambda$toDeferred$0(ChannelOwner.java:97) ~[playwright-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.Connection.sendMessage(Connection.java:77) ~[playwright-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.ChannelOwner.sendMessage(ChannelOwner.java:80) ~[playwright-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.BrowserTypeImpl.launch(BrowserTypeImpl.java:39) ~[playwright-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.BrowserTypeImpl.launch(BrowserTypeImpl.java:28) ~[playwright-0.171.0.jar!/:na]
at com.microsoft.playwright.BrowserType.launch(BrowserType.java:594) ~[playwright-0.171.0.jar!/:na]
at com.example.app.PageScreenshot.fetch(PageScreenshot.java:24) ~[classes!/:1.0-SNAPSHOT]
at com.example.app.PlaywrightSpringApplication.run(PlaywrightSpringApplication.java:17) ~[classes!/:1.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) ~[spring-boot-2.3.2.RELEASE.jar!/:2.3.2.RELEASE]
... 13 common frames omitted
Caused by: com.microsoft.playwright.impl.ServerException: Error: Failed to launch firefox because executable doesn't exist at /root/.cache/ms-playwright/firefox-1224/firefox/firefox
Try re-installing playwright with "npm install playwright"
Note: use DEBUG=pw:api environment variable and rerun to capture Playwright logs.
at com.microsoft.playwright.impl.Connection.dispatch(Connection.java:146) ~[playwright-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.Connection.processOneMessage(Connection.java:130) ~[playwright-0.171.0.jar!/:na]
at com.microsoft.playwright.impl.ChannelOwner.lambda$toDeferred$0(ChannelOwner.java:95) ~[playwright-0.171.0.jar!/:na]
... 21 common frames omitted
It still cant find Firefox. It looks at /root/.cache/ms-playwright/firefox-1224/firefox/firefox
path. But the cli install firefox on
/root/.cache/ms-playwright/firefox-1221
. Directory suffix is 1221, not 1224.
So, how is this suffix managed among cli, driver and library?
from playwright-java.
Last update, I changed CLI_VERSION to 0.170.0 when I run driver download script. And now, everything is fine. 👍
from playwright-java.
Related Issues (20)
- [Ports]: Backport client side changes
- [Bug]: chrome set LaunchPersistentContextOptions return's browser is null HOT 2
- [Feature]: Add @Inherited to @UsePlaywright HOT 5
- [internal] Get rid of Driver wrapper
- [Feature]: Can I use proxy to connectOverCDP? HOT 1
- [Bug]: Initial execution of `Playwright.create()` is relatively slow HOT 1
- [Bug]: Run a playwright on an ARM-based machine Execution IO error HOT 3
- [Feature]: Make all playwright fixture's getOrCreateX methods public HOT 4
- [Feature]: Allow automatic saving of trace files HOT 7
- [Bug]: playwright cpu usage too high HOT 1
- [Ports]: Backport client side changes
- How to get chrom extension background page HOT 11
- [Regression]: playwright-1.42.0-javadoc.jar contains just the MANIFEST.MF HOT 5
- [Feature]: Allow user to pass in wsEndpoint and ConnectOptions to fixtures HOT 5
- [Docs]: trouble with the `mvn exec install webkit` command HOT 4
- ClassNotFoundException for Playwright CLI HOT 1
- [Bug]: Drag and drop in a canvas is not working in playwright HOT 4
- [Regression]: Maven could not create local repository HOT 1
- [Bug]: PLAYWRIGHT_NODEJS_PATH env variable is ignored in version 1.43.0 HOT 5
- [Feature]: ApiRequestContext: Add support for SSL Context HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from playwright-java.