Skip to content

saro-lab/selenium-chrome-all-in-one

Repository files navigation

Selenium Chrome All-in-One

Maven Central GitHub license

Introduction

Selenium Chrome All-in-One is a library that automatically downloads Chrome/ChromeDriver and helps you use Selenium with those files.

Try the example below.

QUICK START

Gradle

compile 'me.saro:selenium-chrome-all-in-one:4.28.1.1'

Maven

<dependency>
  <groupId>me.saro</groupId>
  <artifactId>selenium-chrome-all-in-one</artifactId>
  <version>4.28.1.1</version>
</dependency>

Version info

  • CDP: Chrome DevTools Protocol (Version) == Chrome Browser Version
Selenium All-in-One CDP JDK
4.28.1.1 132 21+
4.28.1.0 131 21+
4.27.0.0 131 21+
4.26.0.0 130 11+
4.25.0.0 129 11+
4.24.0.1 128 11+
4.22.0.3 126 11+

Kotlin example

// use example
val chromeBinPath = File("./chrome-bin")

val manager = ChromeDriverManager.builder(chromeBinPath)
    //.downloadStrategy(DOWNLOAD_IF_NO_VERSION) // default value
    .enableRecommendChromeOptions(true)
    .build()

val list = manager.openBackground("https://gs.saro.me") {
    finds(".post-list .node")
        .map { it.find("a").text.trim() + " : " + it.find("a").getAttribute("href") }
}

list.forEach(::println)
// just download
val chromeBinPath = File("./chrome-bin")
ChromeDriverManager.download(chromeBinPath, getPlatform(), DOWNLOAD_IF_NO_VERSION)
// with spring project
@Configuration
@EnableAutoConfiguration
class SeleniumConfiguration {
    @Bean
    fun getChromeDriverManager(): ChromeDriverManager =
        ChromeDriverManager
            .builder(File("./chrome-bin"))
            .enableRecommendChromeOptions(true)
            .build()
}
// use example
@Service
class ScrapTradeService(
    private val chromeDriverManager: ChromeDriverManager,
    private val tradingVolumeRepository: TradingVolumeRepository,
): ScrapTrade {
    ...
}

Java example

// use example
File chromeBinPath = new File("./chrome-bin");

ChromeDriverManager manager = ChromeDriverManager.builder(chromeBinPath)
        .enableRecommendChromeOptions(true)
        .build();

List<String> list = manager.openBackground("https://gs.saro.me", dp -> {
    List<String> items = new ArrayList<>();
    dp.finds(".post-list .node").forEach(
            e -> items.add(dp.find(e, "a").getText().trim() + " " + dp.find(e, "a").getAttribute("href"))
    );
    return items;
});

list.forEach(System.out::println);
// just download
File chromeBinPath = new File("./chrome-bin");
SeleniumChromeAllInOne.download(chromeBinPath, Platform.getPlatform(), DownloadStrategy.DOWNLOAD_IF_NO_VERSION);

Documentation

class ChromeDriverManager

  • This is a manager that creates ChromeDriverPlus and ChromeDriver objects, and it is a singleton class.
  • It is recommended to use it by creating it as a @Bean.
static fun builder(manageChromePath: File): ChromeDriverBuilder
  • manageChromePath
    • Specify the folder to store and manage the versions of the Chrome browser and ChromeDriver.
    • To avoid conflicts, it is recommended to use a folder created exclusively for the Selenium Chrome All-In-One project.
static fun download(manageChromePath: File, platform: Platform, downloadStrategy: DownloadStrategy)
  • manageChromePath
    • Specify the folder to store and manage the versions of the Chrome browser and ChromeDriver.
    • To avoid conflicts, it is recommended to use a folder created exclusively for the Selenium Chrome All-In-One project.
  • platform
    • Specify the platform to download the Chrome browser and Chrome Driver.
    • Using Platform.getPlatform() allows you to retrieve the current platform you are using.
  • downloadStrategy
    • DownloadStrategy.THROW_IF_NO_VERSION
      • Throws an error if the version does not exist.
        • For example, in a server environment where the firewall is blocking, you can set DownloadStrategy.THROW_IF_NO_VERSION and configure the usage environment by placing the downloaded file in a folder through ChromeDriverManager.download().
    • DownloadStrategy.DOWNLOAD_IF_NO_VERSION
      • Downloads if the version does not exist.
    • DownloadStrategy.DOWNLOAD_IF_NO_VERSION_OR_DIFFERENT_REVISION
      • Downloads if the version does not exist or if the revision is different.
fun openBackground(url: String, use: ChromeDriverPlus.() -> T): T
  • Open the ChromeDriverPlus in the background.
fun openBackground(url: String, addOption: Set, use: ChromeDriverPlus.() -> T): T
  • Open the ChromeDriverPlus in the background.
fun openWith(url: String, use: ChromeDriverPlus.() -> T): T
  • Open the ChromeDriverPlus
fun openWith(url: String, addOption: Set, use: ChromeDriverPlus.() -> T): T
  • Open the ChromeDriverPlus
fun newChromeDriver(chromeOptions: ChromeOptions): ChromeDriver
  • Open the ChromeDriver raw.
  • Since ChromeDriver does not support auto close unlike ChromeDriverPlus, you need to manually release the resources.

class ChromeDriverPlus

var implicitWaitTimeout: Duration
  • same as ChromeDriver.manage().timeouts().implicitlyWait
var pageLoadTimeout: Duration
  • same as ChromeDriver.manage().timeouts().pageLoadTimeout
fun move(url: String)
  • move to the url
fun scrollDown(size: Int)
  • scroll down by the specified size.
fun scrollDown()
  • scroll down to the bottom of the page
fun script(script: String): Any?
  • same as JavascriptExecutor.executeScript
fun sleep(millis: Long)
  • same as Thread.sleep
fun windowSize(width: Int, height: Int)
  • same as ChromeDriver.manage().window().size
fun find(css: String): WebElement
  • same as SearchContext.findElement
fun finds(css: String): List
  • same as SearchContext.findElements
fun findsNotWait(css: String): List
  • finds() without waiting
fun hasElementsNotWait(css: String): Boolean
  • find exist element without waiting
fun inImplicitWaitTimeout(duration: Duration, run: () -> T): T
  • run in implicit wait timeout
fun inPageLoadTimeout(duration: Duration, run: () -> T): T
  • run in page load timeout

class ChromeDriverBuilder

  • create this object through ChromeDriverManager.builder().
fun downloadStrategy(downloadStrategy: DownloadStrategy): ChromeDriverBuilder
  • DownloadStrategy.THROW_IF_NO_VERSION
    • Throws an error if the version does not exist.
  • DownloadStrategy.DOWNLOAD_IF_NO_VERSION
    • Downloads if the version does not exist.
  • DownloadStrategy.DOWNLOAD_IF_NO_VERSION_OR_DIFFERENT_REVISION
    • Downloads if the version does not exist or if the revision is different.
fun option(option: String): ChromeDriverBuilder
  • Enter the options for ChromeDriver.
  • However, the --headless option cannot be used.
  • Instead, use ChromeDriverManager.openBackground().
fun enableRecommendChromeOptions(disabledSecurity: Boolean): ChromeDriverBuilder
  • recommend chrome options
    // Prevents socket errors.
    --user-data-dir=System.getProperty("java.io.tmpdir")
    
    // Disables browser information bar.
    --disable-infobars
    
    // Ignores the limit on temporary disk space for the browser.
    --disable-dev-shm-usage
    
    // Disables image loading.
    --blink-settings=imagesEnabled=false
    
    --disable-extensions
    --disable-popup-blocking
    --disable-gpu
    
  • disabled security options
    webdriver.chrome.whitelistedIps = "" (system properties)
    --no-sandbox
    --ignore-certificate-errors
    
fun build(): ChromeDriverManager
  • create ChromeDriverManager object

Repository

Selenium Project

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published