无法在selenium中找到下拉列表元素

时间:2014-10-16 11:40:38

标签: java select iframe selenium

我用硒写了一个基本的测试。测试失败,因为selenium无法从下拉列表中选择项目。

我的select语句没有被执行。此外,我在select中包含此iframe声明,因为它属于它。

请告诉我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("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();

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


     }

 }

1 个答案:

答案 0 :(得分: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", "abc@Kish123.com")
        .selectOptionByText("select#HOME_AIRPORT", "Atlanta, GA - ATL")
        .check("#IAN") // check the terms and conditions
        .click("#submit") // form submitted
        .switchToDefaultContent()
        ;
    }
}