2014-10-16 38 views
0

我写过一个使用硒的基本测试。由于硒无法从下拉列表中选择项目,因此测试失败。无法在硒中找到下拉列表元素

我的select语句没有得到执行。此外,我还在iframe之内包含此select声明,因为这属于它。

您能否让我知道我的select语句有什么问题。

这里是我的southwest网站代码:

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

    //oBrw.get("http://www.southwest.com/html/email/click_n_save_signup.html?clk=GFOOTER-CNS-ENROLL"); 

    Thread.sleep(5000); 

    //Enter First Name 

    oBrw.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 

    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("[email protected]"); 

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

    //after return 
    oBrw.switchTo().defaultContent(); 


    } 

} 
+1

** **德selectByVisibleText()?你确定你的意思是取消选择而不仅仅是选择? – SiKing 2014-10-16 19:33:53

回答

0

由于@SiKing所指出的,你的问题是与你的deselectByVisibleText()。你想要selectByVisibleText()

uiHomeAp.selectByVisibleText("Atlanta, GA - ATL"); 

也想指出的是,如果你是刚刚起步,对检查出的getting started with selenium框架可能更容易。如果你正在使用maven,你可以这样做:

<dependency> 
    <groupId>io.ddavison</groupId> 
    <artifactId>getting-started-with-selenium-framework</artifactId> 
    <version>1.2</version> 
</dependency> 

那么你的测试应该是这样的:

@Config(browser = Browser.CHROME, url="http://www.southwest.com/") 
public class FirstWDWithoutRecording extends AutomationTest { 
    public void southWestSignUp() { 
     click(By.linkText("Sign up")) 
     .switchToFrame(0) 
     .setText("input#FIRST_NAME", "abc") 
     .setText("input#LAST_NAME", "Kish123") 
     .setText("input#EMAIL", "[email protected]") 
     .selectOptionByText("select#HOME_AIRPORT", "Atlanta, GA - ATL") 
     .check("#IAN") // check the terms and conditions 
     .click("#submit") // form submitted 
     .switchToDefaultContent() 
     ; 
    } 
}