Initial commit
This commit is contained in:
29
selenium/pom.xml
Normal file
29
selenium/pom.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>ch.gtache.fro</groupId>
|
||||
<artifactId>fro</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>fro-selenium</artifactId>
|
||||
|
||||
<properties>
|
||||
<selenium.version>4.35.0</selenium.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>ch.gtache.fro</groupId>
|
||||
<artifactId>fro-jsoup</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.seleniumhq.selenium</groupId>
|
||||
<artifactId>selenium-java</artifactId>
|
||||
<version>${selenium.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,73 @@
|
||||
package ch.gtache.fro.selenium;
|
||||
|
||||
import ch.gtache.fro.BirdProvider;
|
||||
import ch.gtache.fro.Configuration;
|
||||
import ch.gtache.fro.Fetcher;
|
||||
import ch.gtache.fro.jsoup.AbstractJSoupFetcher;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.chrome.ChromeOptions;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Abstract implementation of {@link Fetcher} using Selenium
|
||||
*/
|
||||
public abstract class AbstractSeleniumFetcher extends AbstractJSoupFetcher implements AutoCloseable {
|
||||
private final ChromeDriver driver;
|
||||
|
||||
/**
|
||||
* Instantiates the fetcher
|
||||
*
|
||||
* @param birdProvider The bird provider
|
||||
* @param configuration The configuration
|
||||
* @throws NullPointerException If any parameter is null
|
||||
*/
|
||||
protected AbstractSeleniumFetcher(final BirdProvider birdProvider, final Configuration configuration) {
|
||||
super(birdProvider, configuration);
|
||||
final var options = new ChromeOptions();
|
||||
options.addArguments("--headless=new");
|
||||
this.driver = new ChromeDriver(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the web driver
|
||||
*
|
||||
* @return The driver
|
||||
*/
|
||||
protected WebDriver driver() {
|
||||
return driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a document from an URL
|
||||
*
|
||||
* @param url The URL
|
||||
* @throws IOException If an error occurs
|
||||
*/
|
||||
@Override
|
||||
protected Document getDocument(final String url) throws IOException {
|
||||
driver.get(url);
|
||||
waitFor();
|
||||
final var source = (String) driver.executeScript("return document.documentElement.outerHTML");
|
||||
if (source == null) {
|
||||
throw new IOException("Page source is null");
|
||||
} else {
|
||||
return Jsoup.parse(source);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for the page to be loaded
|
||||
*
|
||||
* @throws IOException If an error occurs
|
||||
*/
|
||||
protected abstract void waitFor() throws IOException;
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
driver.quit();
|
||||
}
|
||||
}
|
||||
11
selenium/src/main/java/module-info.java
Normal file
11
selenium/src/main/java/module-info.java
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Selenium fetcher module for FRO
|
||||
*/
|
||||
module ch.gtache.fro.selenium {
|
||||
requires transitive ch.gtache.fro.jsoup;
|
||||
requires org.seleniumhq.selenium.chrome_driver;
|
||||
requires org.jsoup;
|
||||
requires org.seleniumhq.selenium.devtools_v137;
|
||||
requires ch.gtache.fro.api;
|
||||
exports ch.gtache.fro.selenium;
|
||||
}
|
||||
Reference in New Issue
Block a user