阿里云函数计算使用headless-chromium
1、把headless-chromium和chromedriver上传到nas,chromedriver地址:https://chromedriver.storage.googleapis.com/index.html
2、代码
package xin.webx.handler;
import com.alibaba.fastjson.JSON;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.FunctionComputeLogger;
import com.aliyun.fc.runtime.StreamRequestHandler;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import xin.webx.vo.ApiResponse;
import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* chrome函数
*/
public class ChromeHandler implements StreamRequestHandler {
@Override
public void handleRequest(
InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
try {
ApiResponse resp = new ApiResponse();
FunctionComputeLogger logger = context.getLogger();
//获取请求数据
StringBuffer lines = new StringBuffer();
String line = null;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
while ((line = bufferedReader.readLine()) != null) {
lines.append(line);
}
logger.info("##request data##:" + lines.toString());
Map<String, Object> body = new HashMap<>();
body.put("status", false);//默认结果为false
/*
ApiRequest request = JSON.parseObject(lines.toString(), ApiRequest.class);
//参数为空的直接响应错误
if (request.getQueryParameters() == null) {
body.put("msg", "parameter is null");
resp.setBody(JSON.toJSONString(body));
resp.setStatusCode(200);
outputStream.write(JSON.toJSONString(resp).getBytes());
return;
}
//如果action=test就走测试
String action = (String) request.getQueryParameters().get("action");
if (action != null && "test".equals(action)) {
baidu(logger, request, body);
} else {
}*/
baidu(logger, body);
resp.setBody(JSON.toJSONString(body));
resp.setStatusCode(200);
outputStream.write(JSON.toJSONString(resp).getBytes());
} catch (Exception e) {
//响应异常结果
ApiResponse resp = new ApiResponse();
Map<String, Object> body = new HashMap<>();
body.put("status", false);
body.put("msg", e.getMessage());
resp.setBody(JSON.toJSONString(body));
resp.setStatusCode(200);
outputStream.write(JSON.toJSONString(resp).getBytes());
}
}
/**
* 测试,用百度搜索java
*/
public void baidu(FunctionComputeLogger log, Map<String, Object> body) {
String url = "http://www.baidu.com";
ChromeDriver webDriver = null;
try {
webDriver = getChromeDriver();
//设置延时操作
webDriver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);//-----页面加载时间
//webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//------元素等待时间(隐式等待)
//webDriver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);//----脚本执行时间
webDriver.get(url);
WebElement kw = findElement(webDriver, By.id("kw"), 2);
kw.sendKeys("java");
webDriver.findElement(By.id("su")).click();
WebElement contentLeft = findElement(webDriver, By.id("content_left"), 2);
List<WebElement> titles = contentLeft.findElements(By.className("t"));
StringBuilder sb = new StringBuilder(webDriver.getSessionId().toString());
sb.append("--");
for (WebElement title : titles) {
sb.append(title.getText()).append(",");
}
body.put("status", true);
body.put("msg", sb.toString());
} catch (Exception e) {
log.info("## err ###:" + e);
body.put("msg", e.getMessage());
} finally {
if (webDriver != null) {
webDriver.quit();
}
}
}
/**
* 获取chromeDriver
*/
private ChromeDriver getChromeDriver() {
System.setProperty("webdriver.chrome.driver", "/mnt/fun/soft/chromedriver");
ChromeOptions options = new ChromeOptions();
options.setBinary("/mnt/fun/soft/headless-chromium");
options.addArguments("--test-type"); //ignore certificate errors
options.addArguments("--headless");// headless mode
options.addArguments("--disable-gpu");
options.addArguments("blink-settings=imagesEnabled=false");
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--no-sandbox");
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-extensions");
options.addArguments("--disable-browser-side-navigation");
options.addArguments("--window-position=0,0");//指定窗口坐标
options.addArguments("--window-size=1903,1600");//指定窗口大小
options.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation", "ignore-certificate-errors"));
return new ChromeDriver(options);
}
/**
* 查询页面元素
*/
public WebElement findElement(WebDriver webDriver, By by, int waitSeconds) {
try {
WebDriverWait wait = new WebDriverWait(webDriver, waitSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
return webDriver.findElement(by);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 查询页面元素
*/
public WebElement findElement(WebDriver webDriver, By by) {
try {
return webDriver.findElement(by);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 执行本地命令
*/
public void exec(String command, FunctionComputeLogger log) throws IOException {
Runtime runtime = Runtime.getRuntime();
Process exec = runtime.exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
String line = null;
while ((line = reader.readLine()) != null) {
log.info(line);
}
reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
while ((line = reader.readLine()) != null) {
log.info(line);
}
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId>
<artifactId>webx</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>webx</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.aliyun.fc.runtime</groupId>
<artifactId>fc-java-core</artifactId>
<version>1.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.34</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-edge-driver</artifactId>
</exclusion>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
</exclusion>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-ie-driver</artifactId>
</exclusion>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-opera-driver</artifactId>
</exclusion>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-safari-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.brotli</groupId>
<artifactId>dec</artifactId>
<version>0.1.2</version>
</dependency>
-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/deploy/lib</outputDirectory>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<outputDirectory>${basedir}/deploy/lib</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
</project>
template.xml
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
chrome:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'webx'
LogConfig:
Project: 'webx-log'
Logstore: 'webx-store'
VpcConfig:
VpcId: 'vpc-xxx'
VSwitchIds: [ 'vsw-xxx' ]
SecurityGroupId: 'sg-xxx'
NasConfig:
UserId: 1000
GroupId: 1000
MountPoints:
- ServerAddr: 'xx.cn-beijing.nas.aliyuncs.com:/'
MountDir: '/mnt/fun'
webx:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: xin.webx.handler.ChromeHandler::handleRequest
Runtime: java8
CodeUri: './deploy'
Timeout: 60
MemorySize: 256
Events:
http-webx:
Type: HTTP
Properties:
AuthType: ANONYMOUS
Methods: ['GET','POST']
2019-09-10 00:56:00
共有0条评论!