From 00c47d697b1fe9c9a5bf713efe97859346dde9ca Mon Sep 17 00:00:00 2001 From: vasgat Date: Mon, 8 Feb 2021 09:43:05 +0200 Subject: [PATCH] updating BrowserEmulator --- .../dynamicpages/BrowserEmulator.java | 29 +++++----- .../extractors/dynamicpages/Selenium.java | 55 ------------------- 2 files changed, 15 insertions(+), 69 deletions(-) delete mode 100644 src/main/java/certh/iti/mklab/easie/extractors/dynamicpages/Selenium.java diff --git a/src/main/java/certh/iti/mklab/easie/extractors/dynamicpages/BrowserEmulator.java b/src/main/java/certh/iti/mklab/easie/extractors/dynamicpages/BrowserEmulator.java index 557e56d..cce4347 100644 --- a/src/main/java/certh/iti/mklab/easie/extractors/dynamicpages/BrowserEmulator.java +++ b/src/main/java/certh/iti/mklab/easie/extractors/dynamicpages/BrowserEmulator.java @@ -23,8 +23,10 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.openqa.selenium.*; +import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.support.ui.WebDriverWait; /** * @author vasgat @@ -33,24 +35,26 @@ public class BrowserEmulator extends Fetcher { public WebDriver driver; - public BrowserEmulator(String baseURL, String relativeURL, String ChromeDriverPath) throws InterruptedException { - -// driver.manage().window().setPosition(new Point(-2000, 0)); - driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); + public BrowserEmulator(String baseURL, String relativeURL, String pathToChromeDriver) throws InterruptedException { + System.setProperty("webdriver.chrome.driver", pathToChromeDriver); + driver = new ChromeDriver(); driver.get( baseURL + relativeURL ); - Thread.sleep(10000); } - public BrowserEmulator(String fullURL, String ChromeDriverPath) throws InterruptedException { - driver = Selenium.setUpChromeDriver(ChromeDriverPath); -// driver.manage().window().setPosition(new Point(-2000, 0)); - driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); + public BrowserEmulator(String fullURL, String pathToChromeDriver) throws InterruptedException { + System.setProperty("webdriver.chrome.driver", pathToChromeDriver); + driver = new ChromeDriver(); driver.get( fullURL ); - Thread.sleep(10000); + } + + public void waitPageLoad() { + new WebDriverWait(driver, 60).until( + webDriver -> ((JavascriptExecutor) webDriver) + .executeScript("return document.readyState").equals("complete")); } /** @@ -61,10 +65,7 @@ public BrowserEmulator(String fullURL, String ChromeDriverPath) throws Interrupt */ public void clickEvent(String CSSSelector) throws InterruptedException { WebElement element = driver.findElement(By.cssSelector(CSSSelector)); - Actions actions = new Actions(driver); - Action action = actions.moveToElement(element).click().release().build(); - action.perform(); - Thread.sleep(3000L); + element.click(); } /** diff --git a/src/main/java/certh/iti/mklab/easie/extractors/dynamicpages/Selenium.java b/src/main/java/certh/iti/mklab/easie/extractors/dynamicpages/Selenium.java deleted file mode 100644 index 5654a8e..0000000 --- a/src/main/java/certh/iti/mklab/easie/extractors/dynamicpages/Selenium.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2016 vasgat. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package certh.iti.mklab.easie.extractors.dynamicpages; - -import java.io.File; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.chrome.ChromeDriver; -import org.openqa.selenium.firefox.FirefoxDriver; -import org.openqa.selenium.phantomjs.PhantomJSDriver; -import org.openqa.selenium.phantomjs.PhantomJSDriverService; -import org.openqa.selenium.remote.DesiredCapabilities; - -/** - * - * @author vasgat - */ -public class Selenium { - - /** - * Setting up PhantoJSDriver - * - * @return PhantomJSDriver - */ - public static WebDriver setUpPhantomJSDriver(String driverPath) { - File phantomjs = new File(driverPath); - DesiredCapabilities dcaps = new DesiredCapabilities(); - dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[]{"--ignore-ssl-errors=yes", "--ssl-protocol=tlsv1", "version=2.0", "driverVersion=1.2.0"}); - dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath()); - - return new PhantomJSDriver(dcaps); - } - - public static WebDriver setUpFireFoxDriver() { - return new FirefoxDriver(); - } - - public static WebDriver setUpChromeDriver(String ChromeDriverPath) { - File file = new File(ChromeDriverPath + "\\chromedriver.exe"); - System.setProperty("webdriver.chrome.driver", file.getAbsolutePath()); - return new ChromeDriver(); - } -}