我是新成立Maven项目的人。我的代码很草率,所以POM.XML也很脏,所以我先向您道歉。
有人可以解释一下如何让WebDriver driver = new ChromeDriver();
得到认可/工作吗?
我正在尝试用硒制成刮刀,就像我说过的我是Maven新手一样。这是我的代码:
package Scrape;
/**
*
* @author bob
*/
public class Scrape {
public void scraper(){
try{
String phantomjsExecutableFilePath = "//usr//local//bin//phantomjs";
System.setProperty("phantomjs.binary.path", phantomjsExecutableFilePath);
//WebDriver driver = new PhantomJSDriver();
String chromeDriverPath = "/home/bob/Desktop/selenium-chrome-driver-2.26.0";
//System.setProperty("webdriver.chrome.driver", chromeDriverPath);
//ChromeOptions options = new ChromeOptions();
//options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors", "--silent");
WebDriver driver = new ChromeDriver();
driver.get("https://us.letgo.com/en?searchTerm=dell%20optiplex");
//driver.manage().window().maximize();
driver.get("http://www.yahoo.com");
Thread.sleep(3000);
//Print out yahoo home page title.
System.out.println("Page title is: " + driver.getTitle());
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
这是POM.XML
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>ScrapeLetGo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.28.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.28.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>2.28.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.detro</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>2.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/htmlunit-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.32.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>24.1-jre</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
任何帮助将不胜感激!
编辑:
我在这里为chromedriver定义路径。我已经设置了它的路径,并使用chmod + x chromedriver使它可执行。但是,在实例化ChromeDriver /让程序识别出chromedriver已导入时,我仍然遇到问题。
String chromeDriverPath = "src/main/resources/chromedriver";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors", "--silent");
WebDriver driver = new ChromeDriver(options);
driver.get("https://us.letgo.com/en?searchTerm=dell%20optiplex");
答案 0 :(得分:1)
首先,我将从官方硒site下载Chromedriver。下载将是一个zip文件,然后将其解压缩。 在您的maven项目中,应该有“ src / main / resources”文件夹,在其中应拖放解压缩的文件(如果出现提示,请选择复制文件)。
然后在测试方法中将属性“ weddriver.chrome.driver”设置为“ src / main / resources / chromedriver”。然后,您可以像往常一样进行实例化chrome驱动程序。