无法从下拉列表中进行选择 - Selenium Webdriver

时间:2017-03-27 06:16:21

标签: html selenium

我正在尝试从下拉列表中选择值SYSTEM CLASS,但我无法这样做。每次,我都会NoSuchElement例外ElementNotVisible例外。 请在下面找到HTML代码: -

<select class="gwt-ListBox">
  <option value="(Please select)">(Please select)</option>
  <option value="SYSTEM CLASS">SYSTEM CLASS</option>
</select>

另外,下面是我尝试用来从下拉列表中选择的硒代码: -

代码集1 -

WebElement dropDown = driver.findElement(By.className("gwt-ListBox"));
dropDown.click();
Thread.sleep(10000);
WebElement dropDownOptions = driver.findElement(By.id("country-codes-menu-1"));
dropDownOptions.findElement(By.name("SYSTEM CLASS")).click(); 

代码集2 -

WebElement CustClassDropDown = driver.findElement(By.className("gwt-ListBox"));
WebElement CustClassDropDown = driver.findElement(By.xpath(".//*[@id='main']/div/div[2]/div/div[3]/div/div[2]/div/div[3]/div/div[3]/div/div[3]/div/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td[1]/table/tbody/tr[1]/td/div/table/tbody/tr/td[4]/select"));
Select className = new Select(CustClassDropDown);
className.selectByVisibleText("SYSTEM CLASS");

代码集3 -

new Select(driver.findElement(By.className("gwt-ListBox"))).selectByVisibleText("SYSTEM CLASS");
Thread.sleep(1000)

每次我运行三个脚本中的任何一个时,我都会遇到异常: -

  

失败:createCustomer       org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互       命令持续时间或超时:20.04秒       构建信息:版本:&#39; 2.53.1&#39;,修订版:&#39; a36b8b1&#39;,时间:&#39; 2016-06-30 17:32:46&#39;       系统信息:主持人:&#39; ABHISHEP-IN&#39;,ip:&#39; 10.176.250.119&#39;,os.name:&#39; Windows 7&#39;,os.arch:&# 39; amd64&#39;,os.version:&#39; 6.1&#39;,java.version:   &#39; 1.8.0_111&#39;       驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver       Capabilities [{applicationCacheEnabled = true,rotate = false,handlesAlerts = true,databaseEnabled = true,version = 46.0,   platform = WINDOWS,nativeEvents = false,acceptSslCerts = true,   webStorageEnabled = true,locationContextEnabled = true,   browserName = firefox,takesScreenshot = true,javascriptEnabled = true,   cssSelectorsEnabled =真}]       会议ID:1f9d091d-b977-4ebe-8ccd-efc82d10033c

请您建议如何从下拉列表中选择值。感谢。

3 个答案:

答案 0 :(得分:0)

尝试以下内容:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@class='gwt-ListBox']"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//option[@value='SYSTEM CLASS']"))).click(); 

答案 1 :(得分:0)

很难说出为什么它不可见。您可能需要等待它变得可见,或者它可能是隐藏的SELECT支持虚假的下拉列表。试试这个......

WebElement select = new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.gwt-ListBox")));
new Select(select).selectByVisibleText("SYSTEM CLASS");

答案 2 :(得分:0)

这个怎么样..

Select cmb = new Select(driver.findElement(By.ClassName("gwt-ListBox")));

然后

cmb.selectByValue("SYSTEM CLASS");

OR

cmb.selectByIndex(1);