无法在下拉框中找到网页元素?

时间:2017-07-12 15:51:26

标签: java selenium

我无法在下拉框中找到特定元素。如何找到性别类型“男性”或“女性”?

代码:

Reporter.log("=====Browser Session Started=====", true);
    System.setProperty("webdriver.chrome.driver","D:\\Java Selenium\\Selenium jar\\driver\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();

    driver.manage().window().maximize();
    driver.get("http://www.delta.com");
    Reporter.log("=====Application Started=====", true);

    //Click SIGN UP to enter Personnel information for creating Delta login//

    driver.findElement(By.linkText("SIGN UP")).click();
    Thread.sleep(500);

    //Enter Personnel information for creating Delta login//
    //To select the Prefix using drop down via ID locator 
    driver.findElement(By.cssSelector("#basicInfoTitle-button")).click();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    //Select the option from drop down
    driver.findElement(By.cssSelector("#ui-id-28")).click();
    Thread.sleep(500);

    driver.findElement(By.cssSelector("#basicInfoFirstName")).sendKeys("Suresh");
    Thread.sleep(500);
    driver.findElement(By.cssSelector("#basicInfoMiddleName")).sendKeys("Ram");
    Thread.sleep(500);
    driver.findElement(By.cssSelector("#basicInfoLastName")).sendKeys("Ponrajan");
    Thread.sleep(500);

    //To select the Gender using drop down via ID locator 
    driver.findElement(By.cssSelector("#basicInfoGender-button")).click();
    //Select the option from drop down  
    Actions action = new Actions(driver);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    action.moveToElement(driver.findElement(By.xpath("//*[@id='ui-id-40']")));
    action.click();

2 个答案:

答案 0 :(得分:1)

您需要使用“选择”定位器。

Select theDropdown = driver.findElement(By.cssSelector("#basicInfoGender-button"));

theDropdown.selectByVisibleText("Male");

OR

theDropdown.selectByVisibleText("Female");

您也可以按索引选择它。

theDropdown.selectByIndex(1);

答案 1 :(得分:0)

您可以使用下拉菜单上的发送键选择所需的选项。

driver.findElement(By.cssSelector("#basicInfoGender-button")).sendKeys("male");