Seleium官网:https://www.selenium.dev/
1.Pom.xml配置
创建Maven项目,添加Selenium依赖
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.4.0</version>
</dependency>
2.安装浏览器驱动
Setting up your system to allow a browser to be automated.
设置你的系统以允许浏览器自动化。
Through WebDriver, Selenium supports all major browsers on the market such as Chrome/Chromium, Firefox, Internet Explorer, Edge, and Safari. Where possible, WebDriver drives the browser using the browser’s built-in support for automation.
通过WebDriver,Selenium支持市场上所有主要浏览器,如Chrome/Chromium、Firefox、Internet Explorer、Edge和Safari。在可能的情况下,WebDriver使用浏览器的内置自动化支持驱动浏览器。
Since all the driver implementations except for Internet Explorer are provided by the browser vendors themselves, they are not included in the standard Selenium distribution. This section explains the basic requirements for getting started with the different browsers.
由于除Internet Explorer之外的所有驱动程序实现都是由浏览器供应商自己提供的,因此它们不包含在标准的Selenium发行版中。本节解释了开始使用不同浏览器的基本要求。
Browser | Supported OS | Maintained by | Download |
---|---|---|---|
Chromium/Chrome | Windows/macOS/Linux | 下载链接 | |
Firefox | Windows/macOS/Linux | Mozilla | 下载链接 |
Edge | Windows/macOS | Microsoft | 下载链接 |
Firefox | Windows/macOS/Linux | Mozilla | 下载链接 |
下载浏览器对应版本的驱动器即可
第一个自动化脚本
打开谷歌浏览器
public class LuanchChrome {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\chromedriver.exe");//后面放的是驱动器的路径
//初始化一个谷歌浏览器实例 实例名称叫做driver
WebDriver driver = new ChromeDriver();
}
}
如果你也可以打开浏览器,说明你已经成功的迈出了第一步~