1

I am new to setting up a maven project. My code is sloppy and so is the POM.XML So I apologize in advance.

Could someone please explain how I can get WebDriver driver = new ChromeDriver(); to be recognized/working?

I am trying to create a scraper with selenium and like I said I am new to maven. Here is my code:

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();
        }
    }
}

and here is 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>

Any help would be greatly appreciated!

EDIT:

I am defining the path here for chromedriver. I have set its path and made it executable using chmod + x chromedriver. However, I am still having issues with instantiating ChromeDriver/getting my program to recognize that chromedriver is imported.

 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");
Daveyman123
  • 185
  • 2
  • 9

1 Answers1

1

First I would download the Chromedriver from official selenium site. The download will be a zip file and unzip the file. In your maven project, you should have "src/main/resources" folder where you should drag and drop the unzipped file [select copy file if prompted].

Then in your test method set property "weddriver.chrome.driver" to "src/main/resources/chromedriver".Then you can carry on, as usual, instantiating chrome driver.

Jeeva
  • 142
  • 1
  • 9
  • Thanks for the response. So you think it's an issue not having the path to the exe correct? I will check if this solves it when i get home to my computer. – Daveyman123 Sep 01 '18 at 20:53
  • Also if you are on linux based machine you need to provide executable permission to the "chromedriver.exe" file from the terminal. Just run "chmod +x chromedriver" – Jeeva Sep 01 '18 at 21:20
  • I have refined my question to include the steps you outlined. Still stuck here. – Daveyman123 Sep 01 '18 at 21:31
  • When you run the tests, did you see any exception? Post the exception stack trace. Also try to run the "chromedriver.exe" from the command line and check whether you see message " starting chromedriver...." – Jeeva Sep 02 '18 at 09:09
  • when i run chromedriver via cmd line it starts "starting chromedriver on port 9515" I am not too sure what you mean by run tests as I am unable to compile. I am new. I cant compile the code without WebDriver able to be instantiated. – Daveyman123 Sep 02 '18 at 12:11
  • I figured out a dirty way to do it which is to add a jar file to the system path ` com.sample sample 1.0 system ${project.basedir}/src/main/resources/yourJar.jar ` I found that answer in here: https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project – Daveyman123 Sep 02 '18 at 13:07
  • if it is not confidential could you send me your workspace with the issue. I would like see it myself to understand what is going on. – Jeeva Sep 03 '18 at 11:56
  • https://www.dropbox.com/s/d6cwy2o44cmfq8e/ScrapeLetGo.zip?dl=0 Here is a link to my uploaded workspace. I am not sure if it will work because of dependency issues. Thanks – Daveyman123 Sep 03 '18 at 14:17
  • 1
    @Daveyman123 I have successfully imported your project to my eclipse instance and can run the scraper method using TestNg [had to remove your hard reference to the chrome driver in the pom.xml file]. Running it as Java application throws this error "cannot execute binary file". You should probably use any unit testing framework to test these methods. – Jeeva Sep 03 '18 at 18:20