sendkey()在selenium webdriver中不起作用

时间:2015-01-24 04:14:35

标签: java javascript selenium selenium-webdriver sendkeys

我尝试在字段中输入phone numbers,但它给了我一个错误

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

以下是代码:

    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");

这是您无法加载页面的内部网站。

我认为sendkey()对此字段不起作用。是否有任何我可以使用的元素sendkey()

1 个答案:

答案 0 :(得分:-2)

异常org.openqa.selenium.NoSuchElementException告诉执行操作时页面上不存在该元素。

这可能是因为XPATH不正确或者在调用操作之前页面上没有出现元素。

请在调试模式下运行代码以查找确切的问题。

以下是Google搜索框的快速示例。我把错误的id用来使代码失败。在这种情况下,我得到了例外。如果我们更正id“gbqfq”,代码就可以正常工作。

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

    }

}