使用Selenium
可以操作 浏览器的行为,并修改界面元素。
官方文档比较齐全
官方示例需要跑通
文档地址: 入门指南
options 参考地址:chromium-command-line-switches
chrome 功能特性:chromedriver
下载
示例
maven
依赖
1 2 3 4 5 6
| <!--操作浏览器--> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.9.1</version> </dependency>
|
管理浏览器驱动的第三方组件,下面的示例没有使用
1 2 3 4 5 6
| <!--管理浏览器驱动--> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.3.3</version> </dependency>
|
官方 chrome
示例
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
| import cn.hutool.core.lang.Assert; import io.github.bonigarcia.wdm.WebDriverManager; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.edge.EdgeOptions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait;
import java.time.Duration; import java.util.*; import java.util.NoSuchElementException;
public class SeleniumTest { @Test public void chrome() { System.setProperty("webdriver.chrome.driver", "D:\\develop\\selenium-server\\chromedriver-113.exe"); ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("--remote-allow-origins=*"); WebDriver driver = new ChromeDriver(chromeOptions); driver.get("https://www.selenium.dev/selenium/web/web-form.html");
String title = driver.getTitle(); Assert.equals("Web form", title); driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
WebElement textBox = driver.findElement(By.name("my-text")); WebElement submitButton = driver.findElement(By.cssSelector("button"));
textBox.sendKeys("Selenium"); submitButton.click();
WebElement message = driver.findElement(By.id("message")); String value = message.getText(); Assert.equals("Received!", value); driver.quit(); } }
|
edge 启动
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 57 58 59 60 61
| import cn.hutool.core.lang.Assert; import io.github.bonigarcia.wdm.WebDriverManager; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.edge.EdgeOptions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait;
import java.time.Duration; import java.util.*; import java.util.NoSuchElementException;
public class SeleniumTest { @Test public void edge() { System.setProperty("webdriver.edge.driver", "D:\\develop\\selenium-server\\msedgedriver-113.0.1774.3.exe"); EdgeOptions edgeOptions = new EdgeOptions(); edgeOptions.addArguments("--remote-allow-origins=*"); edgeOptions.addArguments("--window-size=200,200"); Map<String, Object> prefs = new HashMap<>(); prefs.put("profile.default_content_settings.popups", 0); prefs.put("profile.default_content_setting_values.automatic_downloads", 1); prefs.put("download.default_directory", "E:\\Downloads"); edgeOptions.setExperimentalOption("prefs", prefs); EdgeDriver driver = new EdgeDriver(edgeOptions); driver.get("https://www.selenium.dev/selenium/web/web-form.html"); String thisWindowHandle = driver.getWindowHandle(); driver.switchTo().newWindow(WindowType.TAB); driver.close(); driver.switchTo().newWindow(WindowType.WINDOW); for (String windowHandle : driver.getWindowHandles()) { driver.switchTo().window(windowHandle); } driver.get("https://www.selenium.dev/selenium/web/web-form.html"); driver.executeScript("arguments[0].click();", button); JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("arguments[0].click();", button); driver.quit(); } }
|
本文地址: https://github.com/maxzhao-it/blog/post/670100ec/