爬虫SeleniumSelenium Web自动化工具
DreamCollector一、引入依赖
Chrome浏览器驱动官方下载地址:http://chromedriver.storage.googleapis.com/index.html
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.1.4</version> </dependency>
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.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 35 36 37 38 39 40 41 42 43
| String DRIVER_PATH = "classpath:/chromedriver.exe";
Sting DRIVER_PATH=“classpath:/chromedriver.exe” ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource(DRIVER_PATH); System.setProperty("webdriver.chrome.driver",resource.getURL().getPath());
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("disable-infobars"); chromeOptions.addArguments("--disable-web-security"); chromeOptions.addArguments("headless"); chromeOptions.addArguments("sec-fetch-site=same-site"); chromeOptions.addArguments("--disable-blink-features"); chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
chromeOptions.setExperimentalOption("excludeSwitches", Lists.newArrayList("enable-automation"));
WebDriver driver = new ChromeDriver(chromeOptions);
JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("Object.defineProperties(navigator, {webdriver:{get:()=>undefined}});");
driver.get(LOGIN_URL);
driver.findElement(By.id("Username")).sendKeys("295170898@qq.com"); Thread.sleep(3000); driver.findElement(By.id("Password")).sendKeys("0nuB!IQWdnJ8"); Thread.sleep(3000); driver.findElement(By.name("button")).click(); Thread.sleep(3000);
driver.findElement(By.id("kw")).sendKeys("selenium");
driver.findElement(By.id("su")).click();
driver.quit();
|