Java 将Selenium WebDriver连接到现有浏览器会话
我正在使用selenium,如果现有的浏览器会话(对于我来说是Chrome)当前存在,我想附加一个webdriver实例。我不想打开新的浏览器窗口/会话。我在谷歌上看到,有一些方法可以通过这些网站上的描述做到这一点: 我正在使用最新版本 我的代码如下所示:Java 将Selenium WebDriver连接到现有浏览器会话,java,selenium-webdriver,selenium-chromedriver,Java,Selenium Webdriver,Selenium Chromedriver,我正在使用selenium,如果现有的浏览器会话(对于我来说是Chrome)当前存在,我想附加一个webdriver实例。我不想打开新的浏览器窗口/会话。我在谷歌上看到,有一些方法可以通过这些网站上的描述做到这一点: 我正在使用最新版本 我的代码如下所示: public static void main(String[] args) throws Exception { // starting Chrome Webdriver server ChromeDriverServi
public static void main(String[] args) throws Exception {
// starting Chrome Webdriver server
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("D:\\Development\\chromedriver\\chromedriver.exe"))
.usingAnyFreePort()
.build();
service.start();
WebDriver driver = new CustomRemoteWebDriver(service.getUrl(),DesiredCapabilities.chrome());
driver.get("http://www.google.com");
WebDriver driver2 = new CustomRemoteWebDriver(service.getUrl(),DesiredCapabilities.chrome());
// here I am expecting www.google.com from last driver instance, because it should have the same session
System.out.println(driver2.getCurrentUrl());
driver.quit();
driver2.quit();
}
扩展RemoteWebDriver,用于检查会话是否已存在:
public class CustomRemoteWebDriver extends RemoteWebDriver {
public static String sessiondIdPath = "c:\\automation\\sessionid";
public CustomRemoteWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
}
@Override
protected void startSession(Capabilities desiredCapabilities) {
String sid = getPreviousSessionIdFromSomeStorage();
if (sid != null) {
setSessionId(sid);
try {
getCurrentUrl();
} catch (WebDriverException e) {
// session is not valid
e.printStackTrace();
sid = null;
}
}
if (sid == null) {
super.startSession(desiredCapabilities);
saveSessionIdToSomeStorage(getSessionId().toString());
}
}
private void saveSessionIdToSomeStorage(String sessionId) {
try {
FileUtils.writeStringToFile(new File(sessiondIdPath), sessionId, Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
}
private String getPreviousSessionIdFromSomeStorage() {
String sessionId;
try {
List<String> sidText = FileUtils.readLines(new File(sessiondIdPath), Charset.defaultCharset());
sessionId = sidText.get(0);
} catch (Exception e) {
return null;
}
return sessionId;
}
}
公共类CustomRemoteWebDriver扩展RemoteWebDriver{
公共静态字符串sessiondPath=“c:\\automation\\sessionid”;
公共CustomRemoteWebDriver(URL remoteAddress、功能desiredCapabilities){
超级(远程地址、所需功能);
}
@凌驾
受保护的void startSession(功能desiredCapabilities){
字符串sid=GetPreviousSessiondFromsomeStorage();
如果(sid!=null){
设置会话ID(sid);
试一试{
getCurrentUrl();
}catch(webdrivexception){
//会话无效
e、 printStackTrace();
sid=null;
}
}
如果(sid==null){
超级启动会话(所需功能);
saveSessionIdToSomeStorage(getSessionId().toString());
}
}
私有void saveSessionIdToSomeStorage(字符串sessionId){
试一试{
FileUtils.writeStringToFile(新文件(SessiondPath)、sessionId、Charset.defaultCharset());
}捕获(IOE异常){
e、 printStackTrace();
}
}
私有字符串getPreviousSessionIdFromSomeStorage(){
字符串sessionId;
试一试{
List sidText=FileUtils.readLines(新文件(sessiondPath),Charset.defaultCharset());
sessionId=sidText.get(0);
}捕获(例外e){
返回null;
}
返回会话ID;
}
}
以及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>org.andy.selenium</groupId>
<artifactId>remotewebdriverexample</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
4.0.0
org.andy.selenium
张贴,但它没有帮助。由于此异常,始终会打开一个新窗口。你知道我做错了什么吗?你不能重复使用同一个驱动程序实例吗?