Search in sources :

Example 46 with JavascriptExecutor

use of org.openqa.selenium.JavascriptExecutor in project nutch by apache.

the class HtmlUnitWebDriver method getHTMLContent.

public static String getHTMLContent(WebDriver driver, Configuration conf) {
    try {
        if (conf.getBoolean("take.screenshot", false))
            takeScreenshot(driver, conf);
        String innerHtml = "";
        if (enableJavascript) {
            WebElement body = driver.findElement(By.tagName("body"));
            innerHtml = (String) ((JavascriptExecutor) driver).executeScript("return arguments[0].innerHTML;", body);
        } else
            innerHtml = driver.getPageSource().replaceAll("&", "&");
        return innerHtml;
    } catch (Exception e) {
        TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles();
        cleanUpDriver(driver);
        throw new RuntimeException(e);
    }
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) WebElement(org.openqa.selenium.WebElement) TimeoutException(org.openqa.selenium.TimeoutException)

Example 47 with JavascriptExecutor

use of org.openqa.selenium.JavascriptExecutor in project SneakerBot by Penor.

the class Adidas method splash.

@SuppressWarnings("deprecation")
public void splash() {
    try {
        WebDriverWait wait = new WebDriverWait(driver, 60L);
        print("Loading webpage: " + url);
        driver.get(url);
        wait.until(new Function<WebDriver, Boolean>() {

            public Boolean apply(WebDriver d) {
                // + String.valueOf(((JavascriptExecutor) d).executeScript("return document.readyState")));
                return String.valueOf(((JavascriptExecutor) d).executeScript("return document.readyState")).equals("complete");
            }
        });
        // TODO: Get to splash page.
        boolean displayed = wait.until(x -> x.findElement(By.className("sk-fading-circle"))).isDisplayed();
        if (displayed) {
            print(proxy != null ? ("[" + proxy.getPassword() + ":" + proxy.getPort() + "] -> ") : "" + "waiting at splash page!");
            while (driver.findElements(By.className("g-recaptcha")).size() == 0) Thread.sleep(5000L);
            print(proxy != null ? ("[" + proxy.getPassword() + ":" + proxy.getPort() + "] -> ") : "" + "passed splash page!");
            driver.manage().getCookies().stream().forEach(c -> {
                if (c.getValue().contains("hmac")) {
                    hmac = c.getValue();
                    hmacExpiration = c.getExpiry();
                }
            });
            if (driver.findElements(By.className("g-recaptcha")).size() > 0)
                siteKey = driver.findElement(By.className("g-recaptcha")).getAttribute("data-sitekey");
            if (driver.findElements(By.id("flashproductform")).size() > 0)
                clientId = driver.findElement(By.id("flashproductform")).getAttribute("action").split("clientId=")[1];
            Date timeLeft = new Date(hmacExpiration.getTime() - System.currentTimeMillis());
            print("[Success] -> SiteKey: " + siteKey + " Client ID: " + clientId + " HMAC: " + hmac + " Time Left: " + timeLeft.getMinutes() + "m" + timeLeft.getSeconds() + "s");
        } else
            print("Error, Element displayed: " + displayed);
        if (manual) {
            FirefoxProfile profile = new FirefoxProfile();
            if (proxy != null) {
                profile.setPreference("network.proxy.type", 1);
                profile.setPreference("network.proxy.http", proxy.getAddress());
                profile.setPreference("network.proxy.http_port", proxy.getPort());
                profile.setPreference("network.proxy.ssl", proxy.getAddress());
                profile.setPreference("network.proxy.ssl_port", proxy.getPort());
            }
            // profile.setPreference("general.useragent.override", driver.set);
            WebDriver checkout = new FirefoxDriver(profile);
            for (Cookie cookie : driver.manage().getCookies()) checkout.manage().addCookie(cookie);
            checkout.get(driver.getCurrentUrl());
        } else
            while (!carted && hmacExpiration.getTime() > System.currentTimeMillis()) atc();
    } catch (Exception e) {
        String name = e.getClass().getName();
        print("[Exception] -> " + name);
        carted = true;
        if (name.equals("org.openqa.selenium.TimeoutException"))
            carted = false;
    } finally {
        print(carted ? "Closing driver, and ending" : "Failed, Retrying...");
        if (carted) {
            driver.quit();
            Thread.currentThread().interrupt();
        }
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) ProxyConfig(com.machinepublishers.jbrowserdriver.ProxyConfig) CredentialObject(main.java.sneakerbot.loaders.Credentials.CredentialObject) Settings(com.machinepublishers.jbrowserdriver.Settings) ExpectedConditions(org.openqa.selenium.support.ui.ExpectedConditions) Date(java.util.Date) JBrowserDriver(com.machinepublishers.jbrowserdriver.JBrowserDriver) By(org.openqa.selenium.By) WebDriver(org.openqa.selenium.WebDriver) WebElement(org.openqa.selenium.WebElement) Random(java.util.Random) Type(com.machinepublishers.jbrowserdriver.ProxyConfig.Type) Function(java.util.function.Function) Builder(com.machinepublishers.jbrowserdriver.Settings.Builder) Level(java.util.logging.Level) TimeUnit(java.util.concurrent.TimeUnit) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) UserAgent(com.machinepublishers.jbrowserdriver.UserAgent) Cookie(org.openqa.selenium.Cookie) ProxyObject(main.java.sneakerbot.loaders.Proxy.ProxyObject) Cookie(org.openqa.selenium.Cookie) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile) Date(java.util.Date)

Aggregations

JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)47 WebElement (org.openqa.selenium.WebElement)25 WebDriver (org.openqa.selenium.WebDriver)10 PublicAtsApi (com.axway.ats.common.PublicAtsApi)9 RealHtmlElementState (com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState)4 Test (org.junit.Test)4 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)4 HiddenHtmlElementState (com.axway.ats.uiengine.utilities.hiddenbrowser.HiddenHtmlElementState)3 Dimension (org.openqa.selenium.Dimension)3 RemoteWebElement (org.openqa.selenium.remote.RemoteWebElement)3 Select (org.openqa.selenium.support.ui.Select)3 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)3 VerificationException (com.axway.ats.uiengine.exceptions.VerificationException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Cookie (org.openqa.selenium.Cookie)2 TimeoutException (org.openqa.selenium.TimeoutException)2 Actions (org.openqa.selenium.interactions.Actions)2 MobileDriver (com.axway.ats.uiengine.MobileDriver)1 MobileOperationException (com.axway.ats.uiengine.exceptions.MobileOperationException)1