元素正在成功识别,但是在发送密钥时不会出现此类元素错误 [英] Elements are identifying successfully but while sending keys getting no such element error

查看:75
本文介绍了元素正在成功识别,但是在发送密钥时不会出现此类元素错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

元素可以成功识别,但是在发送密钥时不会出现此类元素错误.

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

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

Assert.assertTrue() used for element presence.

推荐答案

使用pageObject捕获元素的示例:

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

}

如何使用它:

Initialize,NavigateTo和Dispose会给您一个错误,因为它们必须是静态的,我迅速编写了此示例以供参考,您应根据需要对其进行编辑以得到想要的工作.希望我为您指出解决问题的正确方向.

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.

这里的处置是在驱动程序完成或引发异常时摆脱驱动程序.删除遗漏的Temp文件.

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

}

这篇关于元素正在成功识别,但是在发送密钥时不会出现此类元素错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆