2018年3月19日 | 2 Comments 问题: 解决: 用docker 启动 selenium hub & node , java 会用到 http://localhost:4444/wd/hub $ docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome 1 $ docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome pom.xml <?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> 12345678910111213141516171819202122232425 <?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 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(); } } 12345678910111213141516171819202122232425 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