Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium webdriver 使用SeleniumWeb驱动程序从下拉列表中选择元素_Selenium Webdriver - Fatal编程技术网

Selenium webdriver 使用SeleniumWeb驱动程序从下拉列表中选择元素

Selenium webdriver 使用SeleniumWeb驱动程序从下拉列表中选择元素,selenium-webdriver,Selenium Webdriver,我想从下拉列表中选择元素,但在html中他们使用了标记。我怎样才能实现我的目标 这是我代码中的内容: public void country() { driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); Select country1 = new Select(country); country1.selectByVisibleText("Canada"); } 我在运行testNg测试时遇到此

我想从下拉列表中选择元素,但在html中他们使用了标记。我怎样才能实现我的目标

这是我代码中的内容:

public void country() {
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    Select country1 = new Select(country);
    country1.selectByVisibleText("Canada");
}
我在运行testNg测试时遇到此错误

org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该被选中,但它是img


使用以下代码:

List<WebElement> lstOptions=Country1.getoptions();
for(WebElement lstOption:lstOptions){
if(lstOption.gettext().tolowercase().equals("canada"))
lstOption.click();
}

如何查找下拉列表值。。。。。 使用此代码确保它对您有帮助

 driver.findElement(By.xpath("ur xpath")).click();

 new Select(driver.findElement(By.name("EventType"))).selectByVisibleText("Birthday");


请显示HTML。您的定位器搞错了,或者您的select实际上不是一个select元素。执行选项声明下拉列表的标记不是select,而是一个图像。要么元素标识错误,要么需要更改选择模式。仅当标记为Select时,Select方法才起作用。请分享html。你是对的,vinay,他们使用了图像标签,当我试图使用选择类选择项目列表时,它给了我错误,我想你误解了这个问题。问题的基础是用户引用的元素不是select元素。因此,定位器是错误的,或者下拉列表不是select,而是以某种方式设计的,使其看起来像select/dropdown。
 new Select(driver.findElement(By.id("city"))).selectByValue("Op3");