쌈@뽕하게 Spring Boot에서 Selenium 적용하기

rokong.me·2024년 7월 1일

Spring

목록 보기
5/5

1. Selenium 라이브러리 설치하기

  • 먼저 Selenium을 사용하기 전 라이브러리를 설치해줘야 한다.
  • Selenium 공식 maven repository에 들어가서 설치를 해보자.
// build.gradle
...
// Selenium 의존성 설치
implementation 'org.seleniumhq.selenium:selenium-java:4.22.0'

2. 쌈@뽕하게 Configuration 파일 작성하기

  • 많은 분들이 Component로 선언하였는데, 나는 Configuration을 이용하여 작성을 할까 한다.
  • 내가 만드는 크롤러 특성상 WebDriver를 이리저리 사용할 때가 많아서 Bean으로 등록해 놓으면 좋지 않을까 생각이 든다. (만약에 틀렸으면 댓글에 제보 바란다.)
// SeleniumConfig.java

@Configuration
public class SeleniumConfig {

    @Bean
    public static WebDriver getChromeDriver() {
        ChromeOptions chromeOptions = new ChromeOptions();
        return new ChromeDriver(chromeOptions);
    }
}

3. WebDriver 사용해보기

  • 아래와 같이 WebDriver를 주입받아서 사용해보면 정상적으로 실행이 되는 것을 볼 수 있다.
// SampleServiceImpl.java

@Service
public class SampleServiceImpl {
	
    private final WebDriver driver;
	
    public void crawl() {
    	driver.get("https://naver.com");
    }
}

0개의 댓글