java Selenium设置Chrome代理
Posted 南京张超凡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Selenium设置Chrome代理相关的知识,希望对你有一定的参考价值。
Selenium+Chrome认证代理不能通过options处理。只能换个方法使用扩展解决
网上只找到用python实现google代理扩展的,有兴趣的同学可以试一下用java实现插件,这里介绍用jython实现java传参调用python来实现google代理插件的创建。
python test1.py脚本如下:
# -*- coding: utf-8 -*-
# 生成google代理插件zip包
# -*- coding: utf-8 -*-
import string
import zipfile
def create_proxyauth_extension(proxy_host, proxy_port,
proxy_username, proxy_password,
scheme='http', plugin_path=None):
if plugin_path is None:
plugin_path = 'chrome_proxyauth_plugin.zip'
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""
background_js = string.Template(
"""
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "${scheme}",
host: "${host}",
port: parseInt(${port})
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"},
function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "${username}",
password: "${password}"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
"""
).substitute(
host=proxy_host,
port=proxy_port,
username=proxy_username,
password=proxy_password,
scheme=scheme,
)
with zipfile.ZipFile(plugin_path, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return plugin_path
参考java jython工具类 https://blog.csdn.net/cafebar123/article/details/79394431 用的是execFuncToString2改的execFuncToString4方法
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.poi.ss.formula.functions.T;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
public class JythonUtils {
/**
* @Title: jythonInit
* @Description: TODO(初始化jython)
* @param: @return
* @return: PythonInterpreter
* @throws
*/
public static PythonInterpreter jythonInit(){
//初始化site 配置
Properties props = new Properties();
props.put("python.home", ""); //python Lib 或 jython Lib,
//根据系统中该文件目录路径
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
//创建PythonInterpreter 对象
PythonInterpreter interp = new PythonInterpreter();
return interp;
}
/**
* @Title: loadPythonFile
* @Description: TODO(加载python 源码文件,)
* @param: @param interp
* @param: @param filePath ,比如:F:\\jpython_jar\\jpythonTest\\pythonTest.py
或/testpython/test.py
* @param: @return
* @return: PythonInterpreter
* @throws
*/
public static PythonInterpreter loadPythonFile(PythonInterpreter interp,
String filePath)
{
interp.execfile(filePath);
return interp;
}
/**
* @Title: loadPythonFunc
* @Description: TODO(加载python 源码文件中的某个方法)
* @param: @param interp
* @param: @param functionName
* @param: @return
* @return: PyFunction
* @throws
*/
public static PyFunction loadPythonFunc(PythonInterpreter interp,
String functionName){
//加载方法
PyFunction func = (PyFunction) interp.get(functionName,PyFunction.class);
return func;
}
/**
* @Title: execFunc
* @Description: TODO(执行无参方法,返回PyObject)
* @param: @param func
* @return: PyObject
* @throws
*/
public static PyObject execFunc(PyFunction func){
PyObject pyobj = func.__call__();
return pyobj;
}
/**
* @Title: execFuncToString
* @Description: TODO(执行无参方法,返回一个字符串)
* @param: @param func
* @param: @return
* @return: String
* @throws
*/
public static String execFuncToString(PyFunction func){
PyObject pyobj = execFunc(func);
return (String) pyobj.__tojava__(String.class);
}
/**
* @Title: execFuncToString
* @Description: TODO(执行有参方法,返回一个字符串)
* @param: @param func
* @param: @param paramName ,参数名
* @param: @return
* @return: String
* @throws
*/
public static String execFuncToString2(PyFunction func, String paramName){
PyObject pyobj = func.__call__(new PyString(paramName));
return (String) pyobj.__tojava__(String.class);
}
/**
* @throws
* @Title: execFuncToString
* @Description: TODO(执行有参方法, 返回一个字符串)
* @param: @param func
* @param: @param ip
* @param: @param port
* @param: @param userName
* @param: @param pwd
* @param: @return
* @return: String
*/
public static String execFuncToString4(PyFunction func, String ip,
int port,String userName,String pwd) {
PyObject pyobj = func.__call__(new PyString(ip),new PyInteger(port),
new PyString(userName),new PyString(pwd));
return (String) pyobj.__tojava__(String.class);
}
/**
* @Title: execFuncToInteger
* @Description: TODO(执行无参方法,返回一个Integer)
* @param: @param func
* @param: @return
* @return: Integer
* @throws
*/
public Integer execFuncToInteger(PyFunction func){
PyObject pyobj = execFunc(func);
return (Integer) pyobj.__tojava__(Integer.class);
}
/**
* @Title: execFuncToList
* @Description: TODO(执行无参方法,返回一个List)
* @param: @param func
* @param: @return
* @return: List<T>
* @throws
*/
public List<T> execFuncToList(PyFunction func){
PyObject pyobj = execFunc(func);
return (List<T>) pyobj.__tojava__(List.class);
}
/**
* @Title: execFuncToMap
* @Description: TODO(执行无参方法,返回一个Map<String, Object>)
* @param: @param func
* @param: @return
* @return: Map<String,Object>
* @throws
*/
public Map<String, Object> execFuncToMap(PyFunction func){
PyObject pyobj = execFunc(func);
return (Map<String, Object>) pyobj.__tojava__(Map.class);
}
public void execFuncToByParamsList(PyFunction func, List<T> paramsList){
}
public static void main(String[] args){
PythonInterpreter interp = jythonInit();
//文件名
String filePath = "F:\\jpython_jar\\jpythonTest\\pythonTest.py";
interp = loadPythonFile(interp, filePath);
//函数名
String functionName = "count";
PyFunction func = loadPythonFunc(interp, functionName);
//执行无参方法,返回PyObject
PyObject pyobj = execFunc(func);
//执行无参方法,返回String
String resultStr = execFuncToString(func);
//执行有参方法,返回String
String paramName = "name";
String resultStr2 = execFuncToString2(func, paramName);
}
}
测试类
import org.python.core.PyFunction;
import org.python.util.PythonInterpreter;
/**
* 测试java和python的调用流程
*
*/
public enum TestExecPython
{
INSTANCE;
public void test(String ip,int port ,String userName,String pwd)
{
PythonInterpreter interp = JythonUtils.jythonInit();
//文件名
String filePath = "test1.py";
interp = JythonUtils.loadPythonFile(interp, filePath);
//函数名
String functionName = "create_proxyauth_extension";
PyFunction func = JythonUtils.loadPythonFunc(interp, functionName);
JythonUtils.execFuncToString4(func,ip,port,userName,pwd);
interp.execfile(filePath);
}
public static void main(String[] args) {
TestExecPython.INSTANCE.test("58.213.122.117",12808,"cetc","cetc");
/*int[] arg = {3,6};
PythonInterpreter.initialize(System.getProperties(),
System.getProperties(), new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("a="+ arg[0]+ "\n");
interpreter.exec("b="+arg[1] + "\n");
interpreter.exec("print b/a");*/
}
}
以上就实现了google proxy pluigin zip包的创建
chrome_proxyauth_plugin
selenium 里的使用
if (Main.useChrome) {
System.setProperty("webdriver.chrome.driver",
"C:\\Program Files\\chromedriver\\chromedriver.exe");
//这一步必不可少
DesiredCapabilities capability;
ChromeOptions options = new ChromeOptions();
//窗口最大化
//options.addArguments("--start-maximized");
//字符集
options.addArguments("lang=zh_CN.UTF-8");
//禁用java
options.addArguments("--disable-java");
//无痕模式
//options.addArguments("--incognito");
//禁用提示条框
options.addArguments("disable-infobars");
//模拟钥匙链
options.addArguments("--use-mock-keychain");
if(Main.usePROXY){
HttpProxyManager.HttpProxyInfo httpProxyInfo=HttpProxyManager
.getSingleInstance().getRandomJnProxy();
//TestExecPython.INSTANCE.test("58.213.122.117",12808,
"cetc28","cetc28");
TestExecPython.INSTANCE.test(httpProxyInfo.proxyIp,
httpProxyInfo.proxyPort,httpProxyInfo.proxyUser,
httpProxyInfo.proxyPwd);
//google代理插件
// options.addExtensions(new File
(ConfigUtil.getProp("CHROME_PROXYAUTH_PLUGIN_PATH")));
options.addExtensions(new File("chrome_proxyauth_plugin.zip"
));
}
//无界面
//options.addArguments("headless");
capability = DesiredCapabilities.chrome();
capability.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capability);
}
亲测可用
以上是关于java Selenium设置Chrome代理的主要内容,如果未能解决你的问题,请参考以下文章
python selenium怎么配置IE和chrome的代理,求代码