步骤 1
下载 Electron 应用对应版本的 ChromeDriver(至关重要),查看方法:
步骤 2
打开 ChromeDriver,记住对应的端口
步骤 3
上代码
ChromeOptions chromeOptions = new ChromeOptions();
// C:\Users\Administrator\AppData\Local\Programs\dlc\dlc.exe
chromeOptions.setBinary("C:\\Users\\Administrator\\AppData\\Local\\Programs\\dlc\\dlc.exe");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WINDOWS);
WebDriver webdriver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), capabilities);
for (String handle : webdriver.getWindowHandles()) {
System.out.println(handle);
webdriver.switchTo().window(handle);
}
//System.out.println(webdriver.getPageSource());
webdriver.findElement(By.tagName("input")).sendKeys("Hello");
webdriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[1]/div/button/span")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
webdriver.quit();
注意
1、setBinary 将 Electron 应用的 exe 填入
2、new RemoteWebDriver(new URL("http://127.0.0.1:9515"), capabilities);里面填写正确的 ChromeDriver 端口
3、其他操作同浏览器操作
参考网址
1、https://www.w3cschool.cn/electronmanual/m9e31qkq.html
2、https://electronjs.org/docs/tutorial/using-selenium-and-webdriver#%E9%85%8D%E7%BD%AE-spectron
3、https://wizardforcel.gitbooks.io/electron-doc/content/tutorial/using-selenium-and-webdriver.html