
1. Selenium 라이브러리 설치하기
// build.gradle
...
// Selenium 의존성 설치
implementation 'org.seleniumhq.selenium:selenium-java:4.22.0'
2. 쌈@뽕하게 Configuration 파일 작성하기
- 많은 분들이 Component로 선언하였는데, 나는 Configuration을 이용하여 작성을 할까 한다.
- 내가 만드는 크롤러 특성상 WebDriver를 이리저리 사용할 때가 많아서 Bean으로 등록해 놓으면 좋지 않을까 생각이 든다. (만약에 틀렸으면 댓글에 제보 바란다.)
@Configuration
public class SeleniumConfig {
@Bean
public static WebDriver getChromeDriver() {
ChromeOptions chromeOptions = new ChromeOptions();
return new ChromeDriver(chromeOptions);
}
}
3. WebDriver 사용해보기
- 아래와 같이 WebDriver를 주입받아서 사용해보면 정상적으로 실행이 되는 것을 볼 수 있다.
@Service
public class SampleServiceImpl {
private final WebDriver driver;
public void crawl() {
driver.get("https://naver.com");
}
}