问题
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