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
214 views
in Technique[技术] by (71.8m points)

java - sendkey() doesnt work in selenium webdriver

I tried to input phone numbers in the field but it gives me an error

    Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element

Here is the code:

    driver.get("https://marswebtdc.tdc.vzwcorp.com/cdl/lte/fdr_llc/fdr.jsp?3gOr4g=4g");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//*[@id='content column']/table[1]/tbody/tr/td/form/b/table/tbody/tr/td/table/tbody/tr[2]/td[4]/input")).click();
    driver.findElement(By.xpath("//*[@id='content column']/table[1]/tbody/tr/td/form/b/table/tbody/tr/td/table/tbody/tr[2]/td[4]/input")).clear();

    driver.findElement(By.xpath("//*[@id='content column']/table[1]/tbody/tr/td/form/b/table/tbody/tr/td/table/tbody/tr[2]/td[4]/input")).sendKeys("9083071303");

this is internal site you cant load the page.

I assume the sendkey() doesnt work for this field. is there any element i can use instead sendkey().

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Exception org.openqa.selenium.NoSuchElementException tells the element not present on page when action is performed.

This may be because of either XPATH is not correct or element is not appeared on page before action is called.

Please run code in debug mode to find exact problem.

Here is quick example of google search box. I have put wrong id to make code fail. In this case I get the exception. If we correct the id "gbqfq" the code works fine.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GoogleSearchUsingSelenium {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");

        try
        {
            WebElement searchBox = driver.findElement(By.id("gbqfq1")); //Incorrect ID here
            searchBox.sendKeys("Cheese");
        }
        catch(Exception e){
            System.out.println("Eelemnt Not Found : "+e.getMessage());
        }

        driver.close();

    }

}

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

...