Example usage for org.openqa.selenium WebElement submit

List of usage examples for org.openqa.selenium WebElement submit

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement submit.

Prototype

void submit();

Source Link

Document

If this current element is a form, or an element within a form, then this will be submitted to the remote server.

Usage

From source file:vacationscheduler.ScrapeWyndham.java

public static void logIn(String p_userName, String p_password, WebDriver firefoxWindow) {
    WebElement userNameElement;/*from ww w.j  a v a2  s .  co  m*/
    WebElement passwordElement;

    try {
        userNameElement = firefoxWindow.findElement(By.name("userNamelabel"));
        userNameElement.sendKeys(p_userName);

        passwordElement = firefoxWindow.findElement(By.name("passwordlabel"));
        passwordElement.sendKeys(p_password);
        passwordElement.submit();
    } catch (Exception e) {
        logIn(p_userName, p_password, firefoxWindow);
    }
}

From source file:wad.booklibrary.BooklibrarySelenium.java

@Test
public void addingNewUserWorks() {
    driver.get(baseUrl);//from w w  w. j  a va  2s .  c  o m
    WebElement element = driver.findElement(By.linkText("Register here!"));
    element.click();

    assertEquals(true, driver.getPageSource().contains("Register to book library!"));

    WebElement username = driver.findElement(By.name("username"));
    WebElement password = driver.findElement(By.name("password"));

    username.sendKeys("unama");
    password.sendKeys("pasa");

    username.submit();

    assertEquals(true, driver.getPageSource().contains("New user registered!"));

}

From source file:wad.booklibrary.BooklibrarySelenium.java

@Test
public void loggedInUserCanAddNewBook() {
    driver.get(baseUrl);/*from  w w  w  .j av a  2 s .c  o m*/
    WebElement username = driver.findElement(By.name("username"));
    WebElement password = driver.findElement(By.name("password"));

    // Testaamista varten luotu valmiiksi kyttj jonka username ja salasana ovat testi.
    username.sendKeys("testi");
    password.sendKeys("testi");

    username.submit();

    assertEquals(true, driver.getPageSource().contains("List of the books"));
    assertEquals(true, driver.getPageSource().contains("Add new book"));
    assertEquals(true, driver.getPageSource().contains("Kirjaudu ulos"));

    WebElement element = driver.findElement(By.linkText("Add new book"));
    element.click();

    WebElement name = driver.findElement(By.name("name"));
    WebElement isbn = driver.findElement(By.name("isbn"));
    WebElement year = driver.findElement(By.name("publishingYear"));
    WebElement authors = driver.findElement(By.name("authors"));
    WebElement publishers = driver.findElement(By.name("publishers"));

    name.sendKeys("bane");
    isbn.sendKeys("1024");
    year.sendKeys("1999");
    authors.sendKeys("authors");
    publishers.sendKeys("publishers");

    name.submit();

    assertEquals(true, driver.getPageSource().contains("New book created!"));

}

From source file:wsattacker.sso.openid.attacker.evaluation.SeleniumBrowser.java

License:Open Source License

public static void loginVictimToWordpress() {
    WebDriver driver = getWebDriver();//from ww w  .  jav a 2 s  . c  o m

    JavascriptExecutor jse = (JavascriptExecutor) driver;
    jse.executeScript("var win = window.open('https://de.wordpress.com/wp-login.php');");

    List<String> windowhandles = new ArrayList<>(driver.getWindowHandles());
    driver.switchTo().window(windowhandles.get(1));

    WebElement element = driver.findElement(By.id("user_login"));
    element.clear();
    element.sendKeys("victim123456789");

    element = driver.findElement(By.id("user_pass"));
    element.clear();
    element.sendKeys("Victim1234!");

    element.submit();

    /*windowhandles.forEach((windowHandle) -> {
    System.out.println("windowHandle: " + windowHandle);
    });*/

    driver.switchTo().window(windowhandles.get(0));
}

From source file:xmlparser.XmlConfigReader.java

License:Apache License

public void commonInject(final WebDriver driver, String injection, int delay) {
    if (delay != 0) {
        try {/*  w w w.java2s .  com*/
            Thread.sleep(delay);
        } catch (InterruptedException e) {
            Debug.printError("Error: " + e.getMessage());
            HaltHandler.quit_nok();
        }
    }

    if (method == 1) {
        String target_url = "";

        try {
            if (GetParamToInject().equals("")) {
                target_url = (GetTargetUrl().endsWith("/") ? GetTargetUrl() : GetTargetUrl() + "/")
                        + URLEncoder.encode(injection, "UTF-8");
            } else
                target_url = getCompleteTargetURL() + "&" + GetParamToInject() + "="
                        + URLEncoder.encode(injection, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            Debug.printError("\nERROR: unable to encode the target URL");
            HaltHandler.quit_nok();
        }

        // handle DoS vectors
        try {
            /*
             * Here follows a simple workaround to make it work in Google Chrome > 21.
             * It seems that starting from Chrome 22, the following code will block the
             * webdriver navigation:
             * 
             * driver.get(targetURL);
             * driver.switchTo().alert();
             * => an exception is launched!  
             *
             * targetURL is a web page containing an alert dialog window, such as
             * <script>alert(1)</script>
             * 
             * Since it works in the case the targeURL is reached from a link, then
             * we can force the browser to produce an injection starting from a link in a data URI web page.
             * 
             * Note that the injection is obviously url-encoded two times! 
             */
            if (Starter.getCurrentBrowser() != null && Starter.getCurrentBrowser().contains("Chrome")
                    && (GetReflectionUrl() == null || GetReflectionUrl().equals(""))) {
                try {
                    driver.get("data:text/html,<a href='" + URLEncoder.encode(target_url, "UTF-8")
                            + "'>go</click>");
                } catch (UnsupportedEncodingException e) {
                    Debug.printError("\nERROR: unable to encode the target URL");
                    HaltHandler.quit_nok();
                }
                driver.findElements(By.xpath("//a")).get(0).click();
            } else
                driver.get(target_url);

        } catch (UnhandledAlertException e) {
            Starter.refreshDriver();
            Debug.printError(
                    "\nERROR: the last injection resulted in a denial of service! Starting a new driver to continue the injection process");
        } catch (WebDriverException e) {
            Starter.refreshDriver();
            Debug.printError(
                    "\nINFO: an alert dialog window was blocking the injection process, starting a new driver...");
        }

        // <reflectionurl> is setted
        if (GetReflectionUrl() != null && !GetReflectionUrl().equals("")) {
            if (Starter.getCurrentBrowser() != null && Starter.getCurrentBrowser().contains("Chrome")) {
                try {
                    driver.get("data:text/html,<a href='" + URLEncoder.encode(target_url, "UTF-8")
                            + "'>go</click>");
                } catch (UnsupportedEncodingException e) {
                    Debug.printError("\nERROR: unable to encode the target URL");
                    HaltHandler.quit_nok();
                }
                driver.findElements(By.xpath("//a")).get(0).click();
            } else
                driver.get(GetReflectionUrl());
        }

    } else if (method == 2) {
        NodeList commands = root.getElementsByTagName("name");
        NodeList targets = root.getElementsByTagName("target");
        NodeList values = root.getElementsByTagName("value");

        if (commands.getLength() == 0 || targets.getLength() == 0 || values.getLength() == 0) {
            Debug.printError("ERROR: malformed <command> tags ( " + xmlFilename + " )");
            HaltHandler.quit_nok();
        } else if (commands.getLength() - Parameters.size() != targets.getLength()
                || commands.getLength() != values.getLength()) {
            Debug.printError("ERROR: malformed <command> tags ( " + xmlFilename + " )");
            HaltHandler.quit_nok();
        }

        for (int i = Parameters.size(); i < commands.getLength(); i++) {

            String name = commands.item(i).getTextContent();
            String target = root.getElementsByTagName("target").item(i - Parameters.size()).getTextContent();
            String value = root.getElementsByTagName("value").item(i).getTextContent();

            WebElement ele = null;

            try {
                if (target.startsWith("id="))
                    ele = driver.findElement(By.id(target.replace("id=", "")));
                else if (target.startsWith("xpath="))
                    ele = driver.findElement(By.xpath(target.replace("xpath=", "")));
                else if (target.startsWith("name="))
                    ele = driver.findElement(By.name(target.replace("name=", "")));
            } catch (Exception e) {
                Debug.printError("\nERROR: unable select the element [" + target + "]");
                Starter.brokenPage();
            }

            if (name.equals("open")) {

                try {
                    if (value.equals("noforce")) {
                        if (!stripFragmentFromCurrentURL(driver).equals(target))
                            driver.get(target);
                    } else {
                        driver.get(target);
                    }
                } catch (UnhandledAlertException e) {
                    Starter.refreshDriver();
                    Debug.printError(
                            "\nERROR: the last injection resulted in a denial of service! Starting a new browser instance to continue the injection process");
                } catch (WebDriverException e) {
                    Starter.refreshDriver();
                    Debug.printError(
                            "\nERROR: the last injection resulted in a denial of service! Starting a new browser instance to continue the injection process");
                }

            } else if (name.equals("type")) {
                try {
                    if (value.startsWith("${") && value.endsWith("}")) {

                        if (value.substring(2, value.length() - 1).equals("RANDOM"))
                            ele.sendKeys("" + (new Random()).nextInt(100000));
                        else if (value.substring(2, value.length() - 1).equals("RANDOM_EMAIL"))
                            ele.sendKeys((new Random()).nextInt(100000) + "@" + (new Random()).nextInt(100000)
                                    + ".org");
                        else if (value.substring(2, value.length() - 1).equals("INJECTION"))
                            ele.sendKeys((injection != null) ? injection : "");
                        else
                            ele.sendKeys(Parameters.get(
                                    value.toLowerCase().replace("$", "").replace("{", "").replace("}", "")));

                    } else if (value.equals("")) {
                        ele.clear();
                    } else
                        ele.sendKeys(value);
                } catch (NullPointerException e) {
                    Debug.printError("\nERROR: cannot find the element defined through [" + target + "]");
                    Starter.brokenPage();
                }

            } else if (name.equals("submit")) {
                try {
                    ele.submit();
                } catch (NullPointerException e) {
                    Debug.printError("\nERROR: cannot find the element defined through [" + target + "]");
                    Starter.brokenPage();
                }
            } else if (name.equals("deleteCookies")) {
                try {
                    driver.manage().deleteAllCookies();
                } catch (NullPointerException e) {
                    Debug.printError("\nERROR: cannot find the element defined through [" + target + "]");
                    Starter.brokenPage();
                }
            } else if (name.equals("click")) {
                try {
                    ele.click();
                } catch (NullPointerException e) {
                    Debug.printError("\nERROR: cannot find the element defined through [" + target + "]");
                    Starter.brokenPage();
                }
            }
        }
    }
}