背景:写了一个爬虫程序使用的docker部署,部署后在docker中安装PhantomJS 比较麻烦,于是想到了再另一个docker容器中部署一个PhantomJS ,通过远程调用的方式来使用它
1.启动一个PhantomJS docker实例
[root@Ieat1 ~]# docker run -d --name phantomjs -p 8910:8910 wernight/phantomjs phantomjs --webdriver=8910
2.通过程序远程调用
通过Java 调用:
WebDriver driver = new RemoteWebDriver(
new URL("http://127.0.0.1:8910"),
DesiredCapabilities.phantomjs());
通过python调用,先运行 pip install selenium安装selenium
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote(
command_executor='http://127.0.0.1:8910',
desired_capabilities=DesiredCapabilities.PHANTOMJS)
driver.get('http://example.com')
driver.find_element_by_css_selector('a[title="hello"]').click()
driver.quit()
3.完整的代码段:
public class RemotePhantomjsTest {
public static void main(String[] args) throws InterruptedException {
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
WebDriver webDriver = null;
try {
webDriver = new RemoteWebDriver(new URL("http://127.0.0.1:8910"), dc);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
webDriver.get("https://www.baidu.com/");
WebElement webElement = webDriver.findElement(By.xpath("/html"));
String content = webElement.getAttribute("outerHTML");
System.out.println(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
webDriver.close();
}
}
}
监控一下docker的日志,发现调用成功
[root@Ieat1 ~]# docker logs -f phantomjs
[INFO - 2018-08-12T12:03:20.392Z] Session [b47e2840-9e27-11e8-81d3-1d2649e3289e] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36","webSecurityEnabled":true}
[INFO - 2018-08-12T12:03:20.392Z] Session [b47e2840-9e27-11e8-81d3-1d2649e3289e] - page.customHeaders: - {}
[INFO - 2018-08-12T12:03:20.392Z] Session [b47e2840-9e27-11e8-81d3-1d2649e3289e] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"linux-unknown-64bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"},"phantomjs.page.settings.userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"}
[INFO - 2018-08-12T12:03:20.392Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: b47e2840-9e27-11e8-81d3-1d2649e3289e
参考: