昨天百度了半天关于Selenium Webdriver怎样重新使用已打开的浏览器的问题,就找到了这么位大佬的文章:
https://blog.csdn/wwwqjpcom/article/details/51232302
因为没积分,代码是在这下的
https://github/ANBUZHIDAO/myFirefoxDriver
把代码下下来研究了半天,勉强算是改了个Chrome版的,能够在已经打开的Chrome浏览器上继续操作,但是有很大缺陷,代码运行时不会报一些异常了,所以发出来希望有大佬帮忙修改一下,注解是我自己的理解,可能会有错
一共三个工具类,第一个是修改的ChromeDriver,代码如下
import static org.openqa.selenium.remote.CapabilityType.PROXY;
import java.io.IOException;
import java.MalformedURLException;
import java.URL;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.Response;
public class MyChromeDriver extends ChromeDriver {
// 用于设置capabilities
private Capabilities myCapabilities;
public MyChromeDriver(String sessionID,String localserver) {
mystartClient(localserver);
mystartSession(sessionID);
}
//重写startSession方法,可以防止调用父级startSession方法而导致打开多个浏览器
@Override
protected void startSession(Capabilities desiredCapabilities) {
// Do Nothing
}
//改写的startClient方法,用于传入localserver(即浏览器的地址),配合sessionID能找出在用的浏览器
protected void mystartClient(String localserver) {
HttpCommandExecutor delegate = null;
try {
URL driverserver = new URL(localserver);
delegate = new MyHttpCommandExecutor(driverserver);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setCommandExecutor(delegate);
Sy
昨天百度了半天关于Selenium Webdriver怎样重新使用已打开的浏览器的问题,就找到了这么位大佬的文章:
https://blog.csdn/wwwqjpcom/article/details/51232302
因为没积分,代码是在这下的
https://github/ANBUZHIDAO/myFirefoxDriver
把代码下下来研究了半天,勉强算是改了个Chrome版的,能够在已经打开的Chrome浏览器上继续操作,但是有很大缺陷,代码运行时不会报一些异常了,所以发出来希望有大佬帮忙修改一下,注解是我自己的理解,可能会有错
一共三个工具类,第一个是修改的ChromeDriver,代码如下
import static org.openqa.selenium.remote.CapabilityType.PROXY;
import java.io.IOException;
import java.MalformedURLException;
import java.URL;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.Response;
public class MyChromeDriver extends ChromeDriver {
// 用于设置capabilities
private Capabilities myCapabilities;
public MyChromeDriver(String sessionID,String localserver) {
mystartClient(localserver);
mystartSession(sessionID);
}
//重写startSession方法,可以防止调用父级startSession方法而导致打开多个浏览器
@Override
protected void startSession(Capabilities desiredCapabilities) {
// Do Nothing
}
//改写的startClient方法,用于传入localserver(即浏览器的地址),配合sessionID能找出在用的浏览器
protected void mystartClient(String localserver) {
HttpCommandExecutor delegate = null;
try {
URL driverserver = new URL(localserver);
delegate = new MyHttpCommandExecutor(driverserver);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setCommandExecutor(delegate);
Sy