私下有朋友和同事问我“用Selenium能不能做2345浏览器的自动化操作“。
经过一番研究发现Selenium是基于IE和Chrome内核的,通过“帮助”-“关于”我们可以看到以下信息:
既然是基于Chrome内核,应该可以通过Chrome driver去驱动2345浏览器。此时Chrome内核版本是“69”, 要找到对应版本的ChromeDriver才可以。
通过几次尝试Chromedriver2.38适配这个版本。
下载链接: https://chromedriver.storage.googleapis.com/index.html?path=2.38/
前提准备
下载2.38版本的ChromeDriver并解压到指定文件夹下,此时我解压在:D:\SelfDevelop\Selenium Drivers\2345
代码展示
package com.stone.demos.seleniumdemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;
public class DemoOf2345 {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Program Files (x86)\\2345Soft\\2345Explorer\\2345Explorer.exe");
System.setProperty("webdriver.chrome.driver", "D:\\SelfDevelop\\Selenium Drivers\\2345\\chromedriver.exe");
// options.addArguments("");
WebDriver chrome = new ChromeDriver(options);
WebDriverWait wait = new WebDriverWait(chrome, 30);
try {
//Visit www.sogou.com
chrome.get("https://www.sogou.com");
WebElement searchTextBox = wait.until(presenceOfElementLocated(By.cssSelector(".sec-input")));
WebElement searchButton = wait.until(presenceOfElementLocated(By.cssSelector("#stb")));
//input CSDN
searchTextBox.clear();
searchTextBox.sendKeys("CSDN");
// click search button
searchButton.click();
// click the first link to CSDN home page
WebElement theFirstLink = wait.until(presenceOfElementLocated(By.xpath("(//h3[@class='vrTitle'])[1]/a")));
theFirstLink.click();
assert chrome.getTitle().equalsIgnoreCase("CSDN-专业IT技术社区");
}finally {
chrome.quit();
}
}
}
代码解释
代码演示的是启动2345浏览器访问搜狗主页,搜索“CSDN”并点击第一个搜索结果
- 设置ChromeDriver路径
System.setProperty(“webdriver.chrome.driver”, “D:\SelfDevelop\Selenium Drivers\2345\chromedriver.exe”); - 设置2345浏览器启动路径
options.setBinary(“C:\Program Files (x86)\2345Soft\2345Explorer\2345Explorer.exe”);
如果有不懂的地方可以留言提问哦!