ParserBase.java 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package parser;
  2. import java.time.Duration;
  3. import java.util.Collections;
  4. import org.openqa.selenium.By;
  5. import org.openqa.selenium.JavascriptExecutor;
  6. import org.openqa.selenium.WebDriver;
  7. import org.openqa.selenium.WebElement;
  8. import org.openqa.selenium.chrome.ChromeDriver;
  9. import org.openqa.selenium.chrome.ChromeOptions;
  10. import org.openqa.selenium.remote.RemoteWebDriver;
  11. import org.openqa.selenium.support.ui.WebDriverWait;
  12. public class ParserBase {
  13. protected boolean checkIfElementExists(WebDriver driver, String xpath) {
  14. return !driver.findElements(By.xpath(xpath)).isEmpty();
  15. }
  16. protected boolean checkIfElementExists(WebElement element, String xpath) {
  17. return !element.findElements(By.xpath(xpath)).isEmpty();
  18. }
  19. protected float formatFloat(String string) {
  20. float result = -1f;
  21. try {
  22. result = Float.parseFloat(string);
  23. } catch (NumberFormatException e) {
  24. // Empty by design
  25. }
  26. return result;
  27. }
  28. protected JavascriptExecutor getJsExecutor(ChromeDriver driver) {
  29. return driver;
  30. }
  31. protected ChromeDriver getSeleniumDriver() {
  32. ChromeOptions options = new ChromeOptions();
  33. System.setProperty("webdriver.chrome.driver",
  34. System.getProperty("user.dir") + "/chromedriver.exe");
  35. System.setProperty("webdriver.chrome.silentOutput", "true");
  36. // Fixing 255 Error crashes
  37. options.addArguments("--no-sandbox");
  38. options.addArguments("--disable-dev-shm-usage");
  39. // Options to trick bot detection
  40. // Removing webdriver property
  41. options.addArguments("--disable-blink-features=AutomationControlled");
  42. options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
  43. options.setExperimentalOption("useAutomationExtension", null);
  44. // Changing the user agent / browser fingerprint
  45. options.addArguments("window-size=1920,1080");
  46. options.addArguments(
  47. "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
  48. // Other
  49. options.addArguments("disable-infobars");
  50. ChromeDriver driver = new ChromeDriver(options);
  51. driver.executeScript("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})");
  52. return driver;
  53. }
  54. protected WebDriverWait getWaitDriver(ChromeDriver driver) {
  55. return new WebDriverWait(driver, Duration.ofSeconds(30));
  56. }
  57. protected WebDriverWait getWaitDriver(RemoteWebDriver driver, int duration) {
  58. return new WebDriverWait(driver, Duration.ofSeconds(duration));
  59. }
  60. protected void scrollElementIntoView(WebDriver driver, WebElement element) {
  61. ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
  62. }
  63. protected void scrollElementIntoViewCenter(ChromeDriver driver, WebElement element) {
  64. ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({ block: 'center' });", element);
  65. }
  66. protected void scrollToTopOfPage(ChromeDriver driver) {
  67. ((JavascriptExecutor) driver)
  68. .executeScript("document.body.scrollTop = document.documentElement.scrollTop = 0;");
  69. }
  70. }