본문 바로가기
Linux

[Linux/Ubuntu] Maven 프로젝트로 Selenium 실행하기

by dev_haha 2024. 1. 22.

ChromOptions를 설정하고 난 후 아주 잘 된다링.

소스코드 기록

 

com.first.mavenapp.SeleniumSample.java

package com.first.mavenapp;

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


public class SeleniumSample {
    private static final String WEB_DRIVER_ID = "webdriver.chrome.driver";
    // /usr/bin/chromedriver
    private static final String WEB_DRIVER_PATH = "/usr/bin/chromedriver";

    public static void main(String[] args) {
        
        System.setProperty(WEB_DRIVER_ID, WEB_DRIVER_PATH);

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-dev-shm-usage");

        WebDriver driver = new ChromeDriver(options);

        try {
            driver.get("https://www.google.com");

            WebElement searchBox = driver.findElement(By.name("q"));

            searchBox.sendKeys("Selenium");

            searchBox.submit();

            System.out.println("Page title is : " + driver.getTitle());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            driver.quit();
        }
    }

}

 

 

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>com.first.mavenapp</groupId>
  <artifactId>realMaven</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>realMaven</name>
  <url>http://maven.apache.org</url>
  <build>
    <plugins>
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.5.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
            <filters>
                <filter>
                    <artifact>*:*</artifact>
                    <excludes>
                        <exclude>META-INF/versions/**</exclude>
                    </excludes>
                </filter>
            </filters>
          </configuration>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-api -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>3.141.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-remote-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-remote-driver</artifactId>
        <version>3.141.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.141.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-support</artifactId>
        <version>3.141.0</version>
    </dependency>
  </dependencies>
</project>

 

명령어 정리

// pom.xml, target 있는 경로
gkrud@DESKTOP-HJPVGM2:~/mavenTestProject/realMaven$ mvn clean install

// jar파일 있는 경로
gkrud@DESKTOP-HJPVGM2:~/mavenTestProject/realMaven/target$ java -cp realMaven-1.0-SNAPSHOT.jar com.first.mavenapp.SeleniumSample