本文介绍了无法使用Selenium中的Select方法从下拉列表中选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法使用选择"方法从下拉列表中选择选项.
I not able to select the options from dropdown list by using 'Select' method.
这是我的gmail帐户创建代码.
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);
}
}
错误消息
线程"main"中的异常org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应为选择"但为跨度"
Error Message
Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "span"
推荐答案
它不是下拉值,您必须单击下拉箭头,然后单击必须从脚本传递的任何值.
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.
代码如下:
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();
这篇关于无法使用Selenium中的Select方法从下拉列表中选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!