元素识别成功,但发送键时没有出现此类元素错误

时间:2016-06-17 06:50:40

标签: selenium selenium-webdriver

元素识别成功,但发送密钥时没有出现此类元素错误。

用于元素存在的Assert.assertTrue()。

1 个答案:

答案 0 :(得分:-2)

使用pageObjects抓取元素的示例:

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;
    }

}

如何使用它:

编辑:初始化,NavigateTo和Dispose会给你一个错误,因为它们必须是静态的,我快速写下来给出一个例子,你应该按照你认为合适的方式编辑它以获得你想要的工作。我希望我指出了解决问题的正确方向。

编辑:这里的处理是在驱动程序完成或抛出异常时清除驱动程序。删除遗漏的Temp文件。

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();
        }
    }

}