package parser; import data.GuiMysql; import objects.SoccerMatch; import objects.SoccerMatchAnalysis; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.RemoteWebDriver; import java.net.MalformedURLException; import java.net.URL; import java.util.*; public class Svenskaspel { String matchesXPATH = "//div[@class = 'pg_coupon_static__event_container']"; String date; Map nameReplace = Map.ofEntries( new AbstractMap.SimpleEntry("manchester united", "Manchester Utd"), new AbstractMap.SimpleEntry("queens park rangers", "QPR"), new AbstractMap.SimpleEntry("west bromwich", "West Brom"), new AbstractMap.SimpleEntry("blackburn rovers", "Blackburn"), new AbstractMap.SimpleEntry("nottingham forest", "Nottingham"), new AbstractMap.SimpleEntry("wolverhampton", "Wolves")); public List GetStryktipsetSelenium(String date) throws MalformedURLException { this.date = date; String url = "https://spela.svenskaspel.se/stryktipset/resultat/" + date + "/statistik"; // RemoteWebDriver seleniumDriver = getSeleniumDriver(); // seleniumDriver.get(url); // WebDriverManager.chromedriver().driverVersion("124.0.6367.60").setup(); // ChromeDriver driver = new ChromeDriver(getChromeOptions()); ChromeOptions options = new ChromeOptions(); // Fixing 255 Error crashes // options.addArguments("--no-sandbox"); options.addArguments("--disable-dev-shm-usage"); // Options to trick bot detection // Removing webdriver property options.addArguments("--disable-blink-features=AutomationControlled"); options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation")); options.setExperimentalOption("useAutomationExtension", null); // Changing the user agent / browser fingerprint options.addArguments("window-size=1920,1080"); options.addArguments("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"); // Other options.addArguments("disable-infobars"); System.out.println("Getting page"); RemoteWebDriver driver = new RemoteWebDriver(new URL("http://nordh.xyz:14444/wd/hub"), options); driver.get(url); CloseCookiePopup(driver); try { Thread.sleep(3000); } catch (InterruptedException e) { throw new RuntimeException(e); } List matches = FindMatches(driver); driver.close(); driver.quit(); return matches; } private List FindMatches(WebDriver driver) { List soccerMatches = new ArrayList<>(); List matchAnalyses = new ArrayList<>(); List matches = driver.findElements(By.xpath(matchesXPATH)); for (WebElement match : matches) { List teamNames = GetNames(match); String homeTeamName = teamNames.get(0); String awayTeamName = teamNames.get(1); System.out.println("Match: " + homeTeamName + " " + awayTeamName); if (nameReplace.containsKey(homeTeamName.toLowerCase())) { homeTeamName = nameReplace.get(homeTeamName.toLowerCase()); } if (nameReplace.containsKey(awayTeamName.toLowerCase())) { awayTeamName = nameReplace.get(awayTeamName.toLowerCase()); } soccerMatches.add(GuiMysql.getInstance().getMatch(date, homeTeamName.trim().replace(" ", "%") + "%", awayTeamName.trim().replace(" ", "%") + "%")); } for (SoccerMatch match : soccerMatches) { try { matchAnalyses.add(new SoccerMatchAnalysis(match)); } catch (NullPointerException e) { System.out.println( "Failed to get team info between " + match.getHomeTeamName() + "-" + match.getAwayTeamName()); } } return matchAnalyses; } private List GetNames(WebElement match) { List result = new ArrayList<>(); WebElement hometeamWebElement = match.findElement(By.xpath("./a/div[2]/span[1]")); WebElement awayteamWebElement = match.findElement(By.xpath("./a/div[2]/span[3]")); if (hometeamWebElement.findElements(By.xpath("./abbr")).size() == 1) { String teamName = hometeamWebElement.findElement(By.xpath("./abbr")).getAttribute("title"); result.add(teamName); } else { result.add(hometeamWebElement.findElement(By.xpath("./span")).getText()); } if (awayteamWebElement.findElements(By.xpath("./abbr")).size() == 1) { String teamName = awayteamWebElement.findElement(By.xpath("./abbr")).getAttribute("title"); result.add(teamName); } else { result.add(awayteamWebElement.findElement(By.xpath("./span")).getText()); } return result; } private void CloseCookiePopup(WebDriver driver) { try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } WebElement acceptButton = driver.findElement(By.xpath("//button[@id='onetrust-accept-btn-handler']")); acceptButton.click(); } private ChromeOptions getChromeOptions() { ChromeOptions options = new ChromeOptions(); // Fixing 255 Error crashes options.addArguments("--no-sandbox"); options.addArguments("--disable-dev-shm-usage"); // Options to trick bot detection // Removing webdriver property options.addArguments("--disable-blink-features=AutomationControlled"); options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation")); options.setExperimentalOption("useAutomationExtension", null); // Changing the user agent / browser fingerprint options.addArguments("window-size=1920,1080"); /* * options.addArguments( * "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" * ); */ // Other options.addArguments("disable-infobars"); return options; } }