com.fastchar.selenium.FastSeleniumChrome Maven / Gradle / Ivy
package com.fastchar.selenium;
import com.fastchar.core.FastChar;
import com.fastchar.core.FastHandler;
import com.fastchar.selenium.interfaces.IFastSeleniumListener;
import com.fastchar.utils.FastStringUtils;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chromium.ChromiumOptions;
import org.openqa.selenium.remote.Browser;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.*;
/**
* @author 沈建(Janesen)
* @date 2021/10/12 10:43
*/
public class FastSeleniumChrome extends ChromiumOptions {
public FastSeleniumChrome() {
super("browserName", Browser.CHROME.browserName(), ChromeOptions.CAPABILITY);
initDefaults();
}
private ChromeDriver driver;
private ChromeOptions options;
private String url;
private int timeout = 30;//页面加载超时时间,单位 秒 ,默认 30秒
private String userAgent;
private int sleep;//页面等待时间
protected void initDefaults() {
setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
setExperimentalOption("useAutomationExtension", false);
addArguments("--window-size=1920,1080");
addArguments("--disable-extensions");
addArguments("--disable-blink-features");
addArguments("--disable-blink-features=AutomationControlled");
addArguments("--no-sandbox");
addArguments("--disable-software-rasterizer");
addArguments("--ignore-certificate-errors");
addArguments("--allow-running-insecure-content");
addArguments("--disable-dev-shm-usage");
addArguments("--disable-gpu");
addArguments("blink-settings=imagesEnabled=false");//禁用图片加载
}
public String getUrl() {
return url;
}
public FastSeleniumChrome setUrl(String url) {
this.url = url;
return this;
}
public int getTimeout() {
return timeout;
}
public FastSeleniumChrome setTimeout(int timeout) {
this.timeout = timeout;
return this;
}
public String getUserAgent() {
return userAgent;
}
public FastSeleniumChrome setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
public int getSleep() {
return sleep;
}
public FastSeleniumChrome setSleep(int sleep) {
this.sleep = sleep;
return this;
}
public String get() {
try {
if (FastStringUtils.isEmpty(url)) {
return null;
}
if (driver == null) {
options = new ChromeOptions().merge(this);
if (FastStringUtils.isNotEmpty(userAgent)) {
options.addArguments("--user-agent=" + userAgent);
} else {
options.addArguments("--user-agent=");
}
driver = new ChromeDriver(options);
Map params = new HashMap<>();
params.put("source", "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })");
driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(timeout));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(timeout));
}
List iFastSeleniumListeners = FastChar.getOverrides().newInstances(false, IFastSeleniumListener.class);
for (IFastSeleniumListener iFastSeleniumListener : iFastSeleniumListeners) {
FastHandler handler = new FastHandler();
iFastSeleniumListener.onBeforeGet(options, driver, handler);
if (handler.getCode() != 0) {
return null;
}
}
driver.get(url);
ExpectedCondition condition = driver -> {
assert driver != null;
Object executeScript = ((JavascriptExecutor) driver).executeScript("return document.readyState");
return executeScript.equals("complete");
};
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(condition);
Thread.sleep(this.sleep * 1000L);
String source = driver.getPageSource();
for (IFastSeleniumListener iFastSeleniumListener : iFastSeleniumListeners) {
iFastSeleniumListener.onAfterGet(driver, url, source);
}
return source;
} catch (Exception e) {
e.printStackTrace();
}finally {
close();
}
return null;
}
public void close() {
if (driver != null) {
driver.quit();
driver = null;
}
}
}