Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

selenium - Elements are identifying successfully but while sending keys getting no such element error

Elements are identifying successfully but while sending keys getting no such element error.

Assert.assertTrue() used for element presence.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Example of using pageObjects to grab elements:

public class Grabber {
    /*
     *      There exists a plugin in firefox to right click an element, inspect it, then right clicking the element
     *      and copying the xpath and pasting it here.
     */

    private static WebElement element = null;

    public static WebElement input_box(WebDriver driver, WebDriverWait wait) {
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("XPATH HERE")));
        //Used if element is a button or needs to bet clicked
        //wait.until(ExpectedConditions.elementToBeClickable(By.xpath("XPATH HERE")));
        element = driver.findElement(By.xpath("XPATH HERE"));
        return element;
    }

}

How to use it :

EDIT: Initialize, NavigateTo, and Dispose will give you an error since they must be static, I wrote this quickly to give an example and you should edit it as you see fit to get what you want working. I hope I pointed you the right direction to solving your problem.

EDIT: The dispose here is to get rid of the driver when it is complete or an exception has been thrown. To remove the Temp files that are left out.

public class Test {

    private WebDriver driver;
    private WebDriverWait wait;

    public static void main(String[] args) {

        try {
            initialize();
            navigateTo("www.somewhere.com");
            Grabber.input_box(driver, wait).sendKeys("I want to send these keys");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dispose();
        }

    }

    private void initialize() {
        driver = new FirefoxDriver();
        wait = new WebDriverWait(driver, 15);
    }

    private void navigateTo(String url) {
        driver.get(url);
    }

    private void dispose() {
        RemoteWebDriver cRDriver = (RemoteWebDriver) driver;
        while (cRDriver.getSessionId() != null) {
            driver.quit();
        }
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...