selenium是什么
Selenium 是一个开源的自动化测试工具,主要用于Web应用程序的自动化测试。它允许用户编写测试脚本,模拟用户的操作,如点击链接、填写表单、键盘输入等,来自动化浏览器中的各种任务。
Selenium 支持多种编程语言,包括Java、C#、Python、Ruby等,并且可以用于多种浏览器,如Chrome、Firefox、Safari等。
Selenium 广泛应用于软件开发过程中的功能测试、回归测试、性能测试等,也可以用于自动化各种Web相关的任务,如数据的抓取、网页内容的自动化发布等。在使用Selenium时,需要遵守目标网站的使用条款,避免进行任何可能对网站服务造成负面影响的操作。
java案例
我使用的版本
浏览器 |
selenium-java |
webdrivermanager |
jdk |
chromeDriver |
Google 124.0.6367.92 (正式版本) (64 位) – 最新版 |
3.141.59 |
5.2.0 |
1.8 |
124.0.6367.91 |
驱动下载地址:https://getwebdriver.com/chromedriver#stable
解压安装
驱动下载成功后解压并放到java的bin目录下

代码
1 2 3 4 5 6 7 8 9 10 11 12 13
| <!-- Selenium Java Client --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency>
<!-- ChromeDriver 需要的依赖 --> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.2.0</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package org.example.selenium;
import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumExample { public static void main(String[] args) throws InterruptedException { WebDriver driver = new ChromeDriver();
driver.get("https://www.baidu.com"); WebElement searchBox = driver.findElement(By.id("kw"));
searchBox.sendKeys("Selenium");
WebElement searchButton = driver.findElement(By.id("su")); searchButton.click();
Thread.sleep(5000);
System.out.println("执行完毕...");
driver.quit(); } }
|
效果

这样一来应该就能玩些好玩的东西了…
不过执行速度好像有点慢。
相关链接
https://getwebdriver.com/chromedriver#stable
https://zhuanlan.zhihu.com/p/97198043