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:au.unick.testing.oos.lazylocators.BaseLazyLocator.java

License:Apache License

/**
 * Create and return WebElement available from WebDriver by locator
 * @param wd//from w w w  .j  av  a  2s  . c o  m
 * @return
 */
public WebElement getElement(WebDriver wd) {
    if (webElement == null) {
        WebElement parentWebElement = getParentElement(wd);
        By by = by();
        if (null == by) {
            webElement = parentWebElement;
        } else if (null == parentWebElement) {
            webElement = wd.findElement(by);
        } else {
            webElement = parentWebElement.findElement(by);
        }
    }
    return webElement;
}

From source file:authentication.AccountInfo.java

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

    WebDriver driver = new ChromeDriver();
    driver.get(/*  ww  w .jav  a  2s  .c  om*/
            "https://www.facebook.com/?stype=lo&jlou=Afeo3usnUHyB5AT4TqVF4PpoNba5Ld2sOALGhGMfhib8xyeI0FjYqdYf72ZtVncLyfHnYBBU6pY3XZ_l-D-KchQXCwLecdkAemJcXs_dIu-UmQ&smuh=16711&lh=Ac-txP8J-TK6lCJ5");
    WebElement username = driver.findElement(By.id("email"));
    WebElement password = driver.findElement(By.id("pass"));
    username.sendKeys(b.getUserName());
    password.sendKeys(b.getPassWord());
    WebElement button = driver.findElement(By.id("loginbutton"));
    button.click();
}

From source file:authentication.TestingTable.java

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
    WebDriver driver = new ChromeDriver();
    driver.get(/*from  ww w .ja  v  a  2s  .co m*/
            "https://www.facebook.com/?stype=lo&jlou=Afeo3usnUHyB5AT4TqVF4PpoNba5Ld2sOALGhGMfhib8xyeI0FjYqdYf72ZtVncLyfHnYBBU6pY3XZ_l-D-KchQXCwLecdkAemJcXs_dIu-UmQ&smuh=16711&lh=Ac-txP8J-TK6lCJ5");
    WebElement username = driver.findElement(By.id("email"));
    WebElement password = driver.findElement(By.id("pass"));
    password.sendKeys(model.getValueAt(jTable1.getSelectedRow(), 0).toString());
    username.sendKeys(model.getValueAt(jTable1.getSelectedRow(), 1).toString());
    WebElement button = driver.findElement(By.id("loginbutton"));
    button.click();
}

From source file:be.ugent.mmlab.webdriver.Demo.java

License:Apache License

public void demoTime() throws InterruptedException {
    // initialize web browser.
    WebDriver driver = new FirefoxDriver();

    // go to the Belgian railways website
    driver.get("http://www.belgianrail.be");

    // select "English"
    // HTML://from   w w  w  .  j  a  va  2 s. c  o  m
    // <a
    //   id="ctl00_bodyPlaceholder_languagesList_ctl02_languageNameLink"
    //   href="javascript:__doPostBack('ctl00$bodyPlaceholder$languagesList$ctl02$languageNameLink','')"
    // > English </a>
    WebElement english = driver
            .findElement(By.id("ctl00_bodyPlaceholder_languagesList_ctl02_languageNameLink"));
    english.click();

    // fill out departure
    WebElement from = driver.findElement(By.id("departureStationInput"));
    from.sendKeys("Gent-Dampoort");

    // pause for a second to make it not too fast
    Thread.sleep(1000);
    // click in the field to get the auto-completion away
    from.click();

    // fill out arrival
    WebElement to = driver.findElement(By.id("arrivalStationInput"));
    to.sendKeys("Brussel-Noord");

    Thread.sleep(1000);
    to.click();

    // click timetable button
    WebElement timetableButton = driver.findElement(By.id(
            "ctl00_ctl00_bodyPlaceholder_bodyPlaceholder_mobilityTimeTableSearch_HomeTabTimeTable1_submitButton"));
    timetableButton.click();

    // get departure info
    // HTML:
    // <td headers="hafasOVTimeOUTWARD" class="time">
    //    <div>
    //     <div class="planed overviewDep">
    //      10:00 dep <span class="bold prognosis">+12 min.</span>
    //     </div>
    //     <div class="planed">
    //      11:20 arr <span class="bold green">+0 min.</span>
    //     </div>
    //   </div>
    // </td>
    List<WebElement> timeCells = driver.findElements(By.className("time"));
    for (WebElement timeCell : timeCells) {
        List<WebElement> times = timeCell.findElements(By.className("planed"));
        System.out.println("----------------------------------------------");
        System.out.println("departure time: " + times.get(0).getText());
        System.out.println("arrival time:   " + times.get(1).getText());
    }
}

From source file:bg.pragmatic.lecture13mvn.waits.utils.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.
 * //from   w  w  w  .  j a  v a2  s  . c om
 * 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
 */
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>() {

            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:bg.pragmatic.lecture13mvn.waits.utils.WaitTool.java

License:Open Source License

/**
 * Checks if the elment is in the DOM, regardless of being displayed or not.
 * //from   w  w w  . j  a  v a  2  s .  co m
 * @param driver
 *            - The driver object to use to perform this element search
 * @param by
 *            - selector to find the element
 * @return boolean
 */
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:bi.com.seleniumgrid.PhantomJsTest.java

License:Apache License

@Test
public void test() {
    final DesiredCapabilities capabilities = new DesiredCapabilities();
    // Configure our WebDriver to support JavaScript and be able to find the PhantomJS binary
    capabilities.setJavascriptEnabled(true);
    capabilities.setCapability("takesScreenshot", false);
    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJS_BINARY);
    final WebDriver driver = new PhantomJSDriver(capabilities);
    // Your test code here. For example:
    WebDriverWait wait = new WebDriverWait(driver, 30); // 30 seconds of timeout
    driver.get("https://en.wikipedia.org/wiki/Main_Page"); // navigate to Wikipedia

    pageTitle = driver.getTitle().trim();
    Assert.assertEquals(pageTitle, "GoalQuest");

    System.out.println("Page title is: " + driver.getTitle());

    By searchInput = By.id("searchInput"); // search for "Software"
    wait.until(ExpectedConditions.presenceOfElementLocated(searchInput));
    driver.findElement(searchInput).sendKeys("Software");
    By searchButton = By.id("searchButton");
    wait.until(ExpectedConditions.elementToBeClickable(searchButton));
    driver.findElement(searchButton).click();

    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.tagName("body"), "Computer software")); // assert that the resulting page contains a text
}

From source file:br.com.dextraining.selenium.SeleniumTestCase.java

protected void esperarPor(String elementoDeId) throws InterruptedException {
    //Thread.sleep(2000); //apenas para podermos visualizar a tela antes da verificacao
    (new WebDriverWait(driver, TEMPO_MAXIMO_ESPERA)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            return driver.findElement(By.id(elementoDeId)).isDisplayed();
        }/*from w  ww . ja  v a2 s  . c om*/
    });
}

From source file:br.com.mundotigre.scripts.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 /*  www. j  a v  a 2  s.c o m*/
*/
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:br.com.mundotigre.scripts.WaitTool.java

License:Open Source License

/**
 * Checks if the elment is in the DOM, regardless of being displayed or not.
 * //from  ww w. j  a  va  2 s  .  c o  m
 * @param driver - The driver object to use to perform this element search
 * @param by - selector to find the element
 * @return boolean
 */
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;
    }
}