Not able to select the value from drop down list by using Select method in Selenium

大城市里の小女人 提交于 2019-12-13 08:37:16

问题


I not able to select the options from dropdown list by using 'Select' method.

Here is my gmail account creation code.

enter code here

package samples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;

public class SmallFunctionalities {

    public static void main(String[] args) throws InterruptedException {


        System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver.exe");
        FirefoxDriver driver= new FirefoxDriver();
        driver.manage().window().maximize();
        Thread.sleep(5000);
        driver.get("https://accounts.google.com/SignUp");   
        Thread.sleep(5000);
        Select sitem= new Select(driver.findElement(By.id("BirthMonth")));
        sitem.selectByIndex(5);
        Thread.sleep(5000);

    }

}

Error Message

Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "span"



回答1:


It is not drop down value , you have to click on drop down arrows and then click on any value which you have to pass from script.

Code is below:

System.setProperty("webdriver.chrome.driver", "E:/software and tools/chromedriver_win32/chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://accounts.google.com/SignUp");   
Thread.sleep(5000);

//Click on up/down arrow
driver.findElement(By.xpath(".//*[@id='BirthMonth']/div[1]/div[2]")).click();

// Now select value from list
driver.findElement(By.xpath(".//*[@id=':7']/div")).click();


来源:https://stackoverflow.com/questions/45538290/not-able-to-select-the-value-from-drop-down-list-by-using-select-method-in-selen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!