Selenium WebDriver测试中的NoSuchElementException

时间:2014-10-16 08:52:40

标签: selenium xpath webdriver automated-tests

我是selenium的新手,并为推出网站编写了一个基本测试,点击链接,填写表格并提交。

一旦到达注册页面,就无法找到" FirstName"文本框。我使用萤火虫进行了双重检查,只能在一个地方使用。我甚至试图用xpath识别元素仍然得到相同的错误。

以下是使用xPath识别名字文本框的代码。

package Default;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
//import org.openqa.selenium.firefox.FirefoxDriver;

public class FirstWDWithoutRecording {

@Test
public void SouthWestSignUp() throws InterruptedException
{

    //Open the FF/Chrome browser
    //FirefoxDriver oBrw = new FirefoxDriver();
    ChromeDriver oBrw = new ChromeDriver();

    //Maximize Browser
    oBrw.manage().window().maximize();

    //Open/Launch www.southwest.com
    System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
    oBrw.get("http://www.southwest.com/");

    //Click on Sign up and Save
    //Recognising
    oBrw.findElement(By.linkText("Sign up")).click();

    Thread.sleep(7000);

    //Enter First Name
    oBrw.findElement(By.xpath("//input[@id='FIRST_NAME']")).clear();
    oBrw.findElement(By.xpath("//input[@id='FIRST_NAME']")).sendKeys("abc");

    //Enter Last Name
    oBrw.findElement(By.id("LAST_NAME")).clear();
    oBrw.findElement(By.id("LAST_NAME")).sendKeys("Kish123");

    //Enter Email ID
    oBrw.findElement(By.id("EMAIL")).clear();
    oBrw.findElement(By.id("EMAIL")).sendKeys("abc@Kish123.com");

    //Selecting Home Airport
    Select uiHomeAp = new Select(oBrw.findElement(By.id("HOME_AIRPORT")));
    uiHomeAp.deselectByVisibleText("Atlanta, GA - ATL");

    //Accepting Conditions
    oBrw.findElement(By.id("IAN")).click();

    //Click Submit
    oBrw.findElement(By.id("submit")).click();

   }

}

1 个答案:

答案 0 :(得分:2)

您的注册表单位于<iframe>内。为了让Webdriver能够“看到”表单,您首先需要切换到该iframe。

driver.switchTo().frame(0);   //'0' as it is the only iframe on the page, the value is the index of all iframes on the page
//do your login actions
//after return
driver.switchTo().defaultContent();