Example usage for org.openqa.selenium WebDriver findElement

List of usage examples for org.openqa.selenium WebDriver findElement

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver findElement.

Prototype

@Override
WebElement findElement(By by);

Source Link

Document

Find the first WebElement using the given method.

Usage

From source file:seleniumTester.java

@Test
public void test2() throws Exception {
    WebElement element = driver.findElement(By.id("filter"));
    element.sendKeys("1999");
    (new WebDriverWait(driver, WAIT_MAX)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            WebElement e = d.findElement(By.tagName("tbody"));
            List<WebElement> rows = e.findElements(By.tagName("tr"));

            return rows.size() == 2;
        }//from  w ww  . jav a2 s  . c  o m
    });

}

From source file:seleniumTester.java

@Test
public void test3() {
    WebElement element = driver.findElement(By.id("filter"));
    element.sendKeys(Keys.CONTROL + "a");
    element.sendKeys(Keys.DELETE);// ww  w.jav a  2s.c  o m

    (new WebDriverWait(driver, WAIT_MAX)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            WebElement e = d.findElement(By.tagName("tbody"));
            List<WebElement> rows = e.findElements(By.tagName("tr"));

            Assert.assertThat(rows.size(), is(5));
            return rows.size() == 5;
        }
    });

}

From source file:seleniumTester.java

@Test
public void test_5() {
    List<WebElement> tds = driver.findElement(By.id("tbodycars")).findElements(By.cssSelector("tr")).get(0)
            .findElements(By.tagName("td"));
    tds.get(7).findElements(By.tagName("a")).get(0).click();
    WebElement element = driver.findElement(By.id("description"));
    element.sendKeys(Keys.CONTROL + "a");
    element.sendKeys(Keys.DELETE);/*  w  w  w . j  a v  a 2 s.  com*/
    element.sendKeys("Cool car");
    driver.findElement(By.id("save")).click();

    (new WebDriverWait(driver, WAIT_MAX)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            WebElement e = d.findElement(By.tagName("tbody"));
            List<WebElement> rows = e.findElements(By.tagName("tr"));
            String result = null;
            for (int i = 0; i < rows.size(); i++) {
                if (rows.get(i).findElements(By.tagName("td")).get(0).getText().equalsIgnoreCase("938")) {
                    result = rows.get(i).findElements(By.tagName("td")).get(5).getText();
                    break;
                }
            }
            assertThat(result, is("Cool car"));
            return true;
        }
    });
}

From source file:WaitTool.java

License:Open Source License

/**
 * Wait for an element to appear on the refreshed web-page.
 * And returns the first WebElement using the given method.
 *
 * This method is to deal with dynamic pages.
 *
 * Some sites I (Mark) have tested have required a page refresh to add additional elements to the DOM.
 * Generally you (Chon) wouldn't need to do this in a typical AJAX scenario.
 *
 * @param WebDriver   The driver object to use to perform this element search
 * @param locator   selector to find the element
 * @param int   The time in seconds to wait until returning a failure
 *
 * @return WebElement   the first WebElement using the given method, or null(if the timeout is reached)
 *
 * @author Mark Collin/*from   w w  w. ja va  2  s. c  om*/
 */
public static WebElement waitForElementRefresh(WebDriver driver, final By by, int timeOutInSeconds) {
    WebElement element;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait()
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                driverObject.navigate().refresh(); //refresh the page ****************
                return isElementPresentAndDisplay(driverObject, by);
            }
        });
        element = driver.findElement(by);
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait
        return element; //return the element
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:WaitTool.java

License:Open Source License

/**
 * Checks if the text is present in the element.
 *
 * @param driver - The driver object to use to perform this element search
 * @param by - selector to find the element that should contain text
 * @param text - The Text element you are looking for
 * @return true or false//from   w w  w.  jav a2 s  . com
 */
private static boolean isTextPresent(WebDriver driver, By by, String text) {
    try {
        return driver.findElement(by).getText().contains(text);
    } catch (NullPointerException e) {
        return false;
    }
}

From source file:WaitTool.java

License:Open Source License

/**
 * Checks if the elment is in the DOM, regardless of being displayed or not.
 *
 * @param driver - The driver object to use to perform this element search
 * @param by - selector to find the element
 * @return boolean/*from  w w  w.j a  v  a 2s .c  om*/
 */
private static boolean isElementPresent(WebDriver driver, By by) {
    try {
        driver.findElement(by);//if it does not find the element throw NoSuchElementException, which calls "catch(Exception)" and returns false;
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}

From source file:WaitTool.java

License:Open Source License

/**
 * Checks if the elment is in the DOM and displayed.
 *
 * @param driver - The driver object to use to perform this element search
 * @param by - selector to find the element
 * @return boolean/*from   w  w w  .jav a2  s .  c  o  m*/
 */
private static boolean isElementPresentAndDisplay(WebDriver driver, By by) {
    try {
        return driver.findElement(by).isDisplayed();
    } catch (NoSuchElementException e) {
        return false;
    }
}

From source file:UnitTest1.java

@Test
public void comparisonFBloginButton() throws InterruptedException {
    String exePath = "C:\\Unit Testing\\Drivers\\chromedriver.exe";
    System.setProperty("webdriver.chrome.driver", exePath);
    WebDriver driver = new ChromeDriver();

    driver.get("https://www.facebook.com");

    String actualTitle = "u_0_o";

    WebElement test = driver.findElement(By.id("u_0_o"));
    assertEquals(actualTitle, test.getAttribute("id"));
    Thread.sleep(5);//from  w  w w. j a  va 2s  . c  om
    driver.quit();

}

From source file:TestWriteanArticle.java

@org.junit.Test
public void positive() throws InterruptedException {
    System.setProperty("webdriver.gecko.driver", "C://Users/Mari/Downloads/geckodriver.exe");
    WebDriver webDriver = new FirefoxDriver();
    String page = "http://www.wikihow.com/Special:CreatePage";
    webDriver.get(page);/* w  w  w  .  ja  v a2  s. c  o  m*/
    WebElement title = webDriver.findElement(By.id("cp_title_input"));
    title.sendKeys("how to use selenium");
    webDriver.findElement(By.id("cp_title_btn")).click();
    Thread.sleep(10);

    WebDriverWait wait = new WebDriverWait(webDriver, 50);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("cpr_title_hdr")));
    assertTrue(webDriver.getPageSource().contains("Are any of these topics the same as yours?"));

    webDriver.close();
}

From source file:TestWriteanArticle.java

@org.junit.Test
public void negative() throws InterruptedException {
    System.setProperty("webdriver.gecko.driver", "C://Users/Mari/Downloads/geckodriver.exe");
    WebDriver webDriver = new FirefoxDriver();
    String page = "http://www.wikihow.com/Special:CreatePage";
    webDriver.get(page);/* w ww.  j  ava 2 s  .  c o m*/
    WebElement title = webDriver.findElement(By.id("cp_title_input"));
    title.sendKeys("how to use wikiHow");
    webDriver.findElement(By.id("cp_title_btn")).click();
    Thread.sleep(10);

    WebDriverWait wait = new WebDriverWait(webDriver, 50);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("cpr_title_hdr")));
    assertTrue(webDriver.getPageSource().contains("That article already exists!"));

    webDriver.close();
}