当前位置:网站首页>Phantomjs Usage Summary
Phantomjs Usage Summary
2022-06-23 00:49:00 【csdncjh】
download
maven rely on
<!--phantomjsdriver-->
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.2.1</version>
</dependency>Using encapsulation
PhantomJsUtils
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class PhantomJsUtils {
private static DesiredCapabilities dcaps = new DesiredCapabilities();
private static final String ua = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 baidu/3.2.1";
private static boolean isLinux = false;
static {
String os = System.getProperty("os.name");
String path = null;
if (os.toLowerCase().contains("windows")) {
path = "D:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe";
System.setProperty("webdriver.chrome.driver", path);
} else {
path = "./phantomjs/phantomjs";
System.setProperty("webdriver.chrome.driver", path);
isLinux = true;
}
// Necessary parameters
System.setProperty("phantomjs.binary.path", path);
// DesiredCapabilities dcaps = new DesiredCapabilities();
//ssl Certificate support
dcaps.setCapability("acceptSslCerts", true);
// Screenshots support
dcaps.setCapability("takesScreenshot", true);
//css Search support
dcaps.setCapability("cssSelectorsEnabled", true);
//js Support
dcaps.setJavascriptEnabled(true);
// Drive support
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
path);
//ua
dcaps.setCapability("phantomjs.page.settings.userAgent", ua);
dcaps.setCapability("phantomjs.page.customHeaders.User-Agent", ua);
// Set connection timeout
System.setProperty("sun.net.client.defaultConnectTimeout", "50");
System.setProperty("sun.net.client.defaultReadTimeout", "50");
// Optimize
List<String> cli = new ArrayList<>();
cli.add("--load-images=" + true);
// cli.add("--output-encoding=" + "utf-8");
cli.add("--disk-cache=yes");
cli.add("--ignore-ssl-errors=true");
cli.add("--debug=false");
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cli);
// agent
/* String ip = "192.168.6.1:1984";
// Enable disk caching ( Cache storage location in Desktop Services , The default is false). Also accept :[yes|no].
// --ignore-ssl-errors=[true|false] Ignore SSL error , For example, expired or self signed certificate error ( The default is false). Also accept :[yes|no].
// --proxy=address:port Specify the proxy server to use ( for example --proxy=192.168.1.42:8080).
String[] values = { "--disk-cache=yes", "--ignore-ssl-errors=true", "--proxy=" + ip };
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, values);*/
}
public static PhantomJSDriver initDriver() {
PhantomJSDriver driver = new PhantomJSDriver(dcaps);
// Set hidden wait ( Act on the whole )
driver.manage().timeouts()
.pageLoadTimeout(20, TimeUnit.SECONDS)
.implicitlyWait(20, TimeUnit.SECONDS);
return driver;
}
/**
* Using agents
* @param proxy ip:port
* @return
*/
public static PhantomJSDriver initDriver(String proxy) {
DesiredCapabilities dcaps = new DesiredCapabilities();
String os = System.getProperty("os.name");
String path = null;
if (os.toLowerCase().contains("windows")) {
path = "D:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe";
System.setProperty("webdriver.chrome.driver", path);
} else {
path = "./phantomjs/phantomjs";
System.setProperty("webdriver.chrome.driver", path);
isLinux = true;
}
// Necessary parameters
System.setProperty("phantomjs.binary.path", path);
// DesiredCapabilities dcaps = new DesiredCapabilities();
//ssl Certificate support
dcaps.setCapability("acceptSslCerts", true);
// Screenshots support
dcaps.setCapability("takesScreenshot", true);
//css Search support
dcaps.setCapability("cssSelectorsEnabled", true);
//js Support
dcaps.setJavascriptEnabled(true);
// Drive support
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
path);
//ua
dcaps.setCapability("phantomjs.page.settings.userAgent", ua);
dcaps.setCapability("phantomjs.page.customHeaders.User-Agent", ua);
// Set connection timeout
System.setProperty("sun.net.client.defaultConnectTimeout", "50");
System.setProperty("sun.net.client.defaultReadTimeout", "50");
// Optimize
List<String> cli = new ArrayList<>();
cli.add("--load-images=" + true);
// cli.add("--output-encoding=" + "utf-8");
cli.add("--disk-cache=yes");
cli.add("--ignore-ssl-errors=true");
cli.add("--debug=false");
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cli);
// agent
// String ip = "192.168.6.1:1984";
// Enable disk caching ( Cache storage location in Desktop Services , The default is false). Also accept :[yes|no].
// --ignore-ssl-errors=[true|false] Ignore SSL error , For example, expired or self signed certificate error ( The default is false). Also accept :[yes|no].
// --proxy=address:port Specify the proxy server to use ( for example --proxy=192.168.1.42:8080).
String[] values = { "--disk-cache=yes", "--ignore-ssl-errors=true", "--proxy=" + proxy };
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, values);
PhantomJSDriver driver = new PhantomJSDriver(dcaps);
// Set hidden wait ( Act on the whole )
driver.manage().timeouts()
.pageLoadTimeout(50, TimeUnit.SECONDS)
.implicitlyWait(50, TimeUnit.SECONDS);
return driver;
}
public static void close(WebDriver driver) {
if (driver != null) {
try {
driver.quit();
} catch (Exception e) {
System.out.println("close error");
}
}
}
/**
* Full page screenshot
*
* @param driver
* @param destPath
* @throws IOException
*/
public static void screenshot(WebDriver driver, String destPath) throws IOException {
File dfile = new File(destPath);
if(dfile.exists()){
dfile.delete();
}
File outFile = ((PhantomJSDriver) driver).getScreenshotAs(OutputType.FILE);
FileUtils.moveFile(outFile, new File(destPath));
}
public static String getCookieStr(WebDriver driver) {
// driver.navigate().refresh();
String cookieStr = null;
for (Cookie ck : driver.manage().getCookies()) {
cookieStr += (ck.getName() + "=" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck.isSecure() + ";");
// cookieStr += ck.getName() + "=" + ck.getValue()+";";
}
return cookieStr.substring(0, cookieStr.length() - 1).replaceFirst("null", "");
}
public static void main(String[] args) {
WebDriver driver = initDriver("192.168.6.1:1984");
String url = "https://en.wikipedia.org/wiki/China";
driver.get(url);
System.out.println(driver.getPageSource());
close(driver);
}
}
source
Selenium+Phantomjs do Java Reptiles _ Tomato, tomato blog -CSDN Blog _java phantomjs selenium
边栏推荐
- Node fetch download file
- 62. 不同路径
- Using geTx to build a more elegant structure of flutter pages
- How to set the power-off auto start of easycvr hardware box
- 數據庫中數據的儲存結構和方式是什麼?
- 贵金属现货白银如何呢?
- SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications
- Shell logs and printouts
- [initial launch] there are too many requests at once, and the database is in danger
- 关于测试/开发程序员技术的一些思考,水平很高超的,混不下去了......
猜你喜欢

ROS1Noetic在Win11中安装记录

层次选择器

New paradigm of semantic segmentation! Structtoken: Rethinking the per pixel classification paradigm
声网多人视频录制与合成支持掉线再录制 | 掘金技术征文

Sélecteur de hiérarchie

How to refine permissions to buttons?

黄金etf持仓量如何算

Typecho imitation of Lu Songsong's blog theme template / Technology Information blog theme template

Cadence spb17.4 - Chinese UI settings

每日刷题记录 (一)
随机推荐
启牛学堂属于证券公司吗?开户安全吗?
Mysql8.0 easily completes gtid master-slave replication
JS image resolution compression
Does qiniu school belong to a securities company? Is it safe to open an account?
graphite statsd接口数据格式说明
权限想要细化到按钮,怎么做?
Typecho仿盧松松博客主題模板/科技資訊博客主題模板
Introduction to the use of opencvsharp (C openCV) wechat QRcode decoding function (with source code attached)
手机上券商开户哪个券商平台更好更安全,如果需要佣金低的怎么办
SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications
Flink synchronizes MySQL data to es
层次选择器
LINQ query
OpenCvSharp (C# OpenCV) 微信QRCode解码功能使用介绍(附源码)
Kunlundb backup and recovery
Ansible 学习总结(8)—— Ansible 控制提权相关知识总结
SAP MM 事务代码VL04为STO创建外向交货单
Binary tree to string and string to binary tree
“听觉”营销价值凸显,喜马拉雅迎来新局点
SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications