创建Maven项目



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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>mchtool</groupId>
	<artifactId>mchtool</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.7.1</version>
		</dependency>
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>30.1.1-jre</version>
		</dependency>
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>3.141.59</version>
		</dependency>
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.31</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.14</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.github.liaochong</groupId>
			<artifactId>myexcel</artifactId>
			<version>3.11.4</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>easyexcel</artifactId>
			<version>2.2.10</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.76</version>
		</dependency>
	</dependencies>

</project>


下载chromedriver

去淘宝镜像 https://npm.taobao.org/mirrors/chromedriver/ 下载对应浏览器版本的chromedriver


启动Chrome浏览器

import java.util.List;
import com.google.common.collect.Lists;

public class 启动Chrome浏览器 {

   public static void main(String[] args) {
      List<String> cmds = Lists.newArrayList();
      cmds.add("C:\\Users\\test\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
      cmds.add("--remote-debugging-port=28899");
      cmds.add("--user-data-dir=\"C:\\Users\\Public\\temp_chrome\"");

      try {
         Process p = Runtime.getRuntime().exec(cmds.toArray(new String[0]));// 创建实例进程执行命令行代码
         p.waitFor();
         p.destroy();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}


selenium代码案例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class TestChrome {

    public static String 确定 = "#add-form > div > div.col-md-9.col-sm-12 > div > div.panel-body > div.form-group.layer-footer > div > button.btn.btn-success.btn-embossed";

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Public\\WebDriver\\chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("debuggerAddress", "127.0.0.1:28899");
        options.addArguments("--ignore-certificate-errors");

        WebDriver driver = new ChromeDriver(options);

        for (int i = 5; i <= 244; i++) {
            String imgname = "";
            if (i < 10) {
                imgname = "00" + i;
            } else if (i >= 10 && i < 100) {
                imgname = "0" + i;
            } else {
                imgname = "" + i;
            }
            String title = "XXXX标题" + imgname;

            driver.findElement(By.id("c-title")).clear();
            driver.findElement(By.id("c-title")).sendKeys(title);

            String zp_url = "http://www.test.com/xxxx1/" + imgname + ".jpg";

            driver.findElement(By.id("c-image")).clear();
            driver.findElement(By.id("c-image")).sendKeys(zp_url);

            driver.findElement(By.id("c-productdata")).clear();
            driver.findElement(By.id("c-productdata")).sendKeys(zp_url);

            driver.findElement(By.cssSelector(确定)).click();

            sleep(1000);
        }

    }

    public static void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}