问题:
解决:
用docker 启动 selenium hub & node , java 会用到 http://localhost:4444/wd/hub
1 |
$ docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome |
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>demo</groupId> <artifactId>selenuim1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-chrome-driver</artifactId> <version>3.9.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> </project> |
App.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package demo.selenuim1; import java.io.FileWriter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.io.IOUtils; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; public class App { public static void main(String[] argv) throws MalformedURLException, UnsupportedEncodingException, IOException { //ChromeDriver driver = new ChromeDriver(); final WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.chrome()); //driver.get("http://www.baidu.com"); driver.get("http://m.jd.com"); final String html = driver.getPageSource(); System.out.println(html); IOUtils.write(html, new FileWriter("a.html")); driver.close(); } } |
参考:
https://github.com/SeleniumHQ/docker-selenium
你好 这个我报缺少版本,请大神指导下
完整的错误信息有吗?