Selenium
Selenium automates browsers. That’s it!
这个解释简单明了,就是用代码去操控浏览器. API简洁易用,想象空间很大.
Java example
Demo场景
- 打开一个网站
- 使用用户名密码登录
- 屏幕截图并保存
- 关闭浏览器
前置条件
安装好浏览器,基本主流浏览器都可以,后面会用chrome举例
具体步骤
- 添加selenium的maven依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.4.0</version> </dependency>
<dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.2.3</version> </dependency>
|
- java code
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| package chrometest.service;
import java.io.File;
import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
public class HelloSeleniumTest {
public static void main(String[] args) throws Exception {
WebDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.setHeadless(true);
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://chengchao.name/springrun/welcome/welcome.html");
Dimension dem = new Dimension(1400, 1000); driver.manage().window().setSize(dem);
WebElement usernameElement = driver.findElement(By.id("usernameInput")); usernameElement.sendKeys("demo-selenium-user"); driver.findElement(By.id("passwordInput")).sendKeys("selenium_********"); Thread.sleep(2000); driver.findElement(By.className("btn-block")).click();
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); String userhome = System.getProperty("user.home"); FileUtils.copyFile(srcFile, new File(userhome + "/screenshot.png"));
Thread.sleep(5000); driver.quit(); } }
|
Linux 环境
可以使用docker来测试
1 2
| docker pull ubuntu:20.04 docker run -itd --name myubuntu -v /Users/charles/Desktop/mydata:/mydata ubuntu:20.04
|
这里比较麻烦的是安装chrome浏览器,如果是arm平台,可以直接firefox,也是一样的
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # chrome,暂时不支持arm架构 # 1. import the GPG key wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add - # 2. add source list sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' # 3. update&install apt update apt install google-chrome-stable -y
# firefox apt install firefox
# 安装中文字体 apt install ttf-wqy-microhei ttf-wqy-zenhei xfonts-intl-chinese
|
1 2 3 4 5 6
| System.setProperty("webdriver.gecko.driver", "/root/geckodriver"); FirefoxOptions firefoxOptions = new FirefoxOptions(); firefoxOptions.setBinary("/usr/bin/firefox"); firefoxOptions.setHeadless(true); WebDriver driver = new FirefoxDriver(firefoxOptions);
|