Selenium 远程打开chrome (java实现)

该博客介绍了如何使用Java和Selenium远程打开Chrome浏览器。首先,需要下载Chrome驱动(chromedriver_win32.zip)和Selenium Java库(selenium-java-2.53.0.zip)。注意,直接运行Chrome驱动可能无法实现远程连接,因此需要设置相关参数以允许远程控制。通过设置DesiredCapabilities和RemoteWebDriver,可以成功实现Java代码远程启动Chrome。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.net.MalformedURLException;
import java.net.URL;


import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;


public static void main(String[] args) throws MalformedURLException {

// TODO Auto-generated method stub

// File file = new File("C:/Users/Administrator/Desktop/chromedriver.exe");
// System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());

DesiredCapabilities capabilities = new DesiredCapabilities();


// ... but only if it supports javascript
capabilities.setJavascriptEnabled(true);


// Get a handle to the driver. This will throw an exception
// if a matching driver cannot be located
// WebDriver driver = new ChromeDriver(capabilities);


// Query the driver to find out more information
DesiredCapabilities dc = DesiredCapabilities.chrome();
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://10.1.0.64:9515"), dc);

// String currentWindowHandle=driver.getWindowHandle();
// driver.switchTo().window(currentWindowHandle);
// And now use it
driver.get("http://www.google.com");

}

下载想需要的软件(这个软件要看自己是什么系统的我演示的是XP的需要翻墙才能打开selenium官网)

chromedriver_win32.zip

selenium-java-2.53.0.zip

如果你是双击打开chromedriver.exe 默认是别的机器远程连接不了你的    所以要到上参数

这样通过java 代码就可以远程打开chrome了



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值