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

人盡茶涼 提交于 2020-01-17 18:08:24

问题


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

Assert.assertTrue() used for element presence.


回答1:


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

}


来源:https://stackoverflow.com/questions/37874942/elements-are-identifying-successfully-but-while-sending-keys-getting-no-such-ele

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!