如何使用selenium webdriver从输入类型下拉列表中选择一个值?

时间:2015-06-24 20:32:39

标签: java selenium-webdriver

我正在尝试在只读下拉列表中选择一个值,我尝试了很多选项,但仍然无法选择所需的选项。下拉列表有两个值ValueOne和ValueTwo。默认选择ValueOne,在我的情况下,我需要选择ValueTwo。当我点击下拉列表并使用firebug进行Inspect Element时,我使用firebug获取以下代码 守则是:

<td class="rcbInputCell rcbInputCellLeft" style="width:100%;">
<input id="ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input" class="rcbInput radPreventDecorate" type="text" readonly="readonly" value="ValueOne" name="ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl02$EditFormControl$rcbControllerType1" autocomplete="off">
</td>

到目前为止,我已经尝试了

1 ----------

Select DropDown = new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")));
        DropDown.selectByVisibleText("ValueTwo");

我得到一个例外

:org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"

2 ------------

WebElement Dropdown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input"));
        Select clickThis = new Select (Dropdown);
        clickThis.selectByVisibleText("ValueTwo");

获取例外:

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"

我也尝试过selectByIndex,但仍然收到上述异常消息。

3 --------------

driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")).sendKeys("ValueTwo");

没有任何反应,案件被标记为通行证。没有错误也不例外。

此外,我在 firefox 38.0.5 上使用 selenium 2.46.0和eclipse TestNG 运行 webscript 。 我确认框架不是iframe。

请建议解决方案。

4 个答案:

答案 0 :(得分:0)

根本问题可能是,这不是标准的选择框,而是基于javascript的解决方案。上面的代码只显示了主持人&#39; HTML元素。我很确定会有a)以前隐藏的元素变得可见或b)DOM中新创建的元素保存值。你必须找到那些(开发工具或萤火虫)进行交互。 一些可能出现的伪代码(只是为了得到一个提示):

<ul>
  <li id="element1">ValueOne</li>
  <li id="element2">ValueTwo</li>
</ul>

在它出现后(等待硒)你只需要点击所需的元素。

答案 1 :(得分:0)

在firefox中通过firepath插件找到你的xpath。

driver.findElement(By.xpath(&#34; .//*@ ID =&#39; ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input&#39;]&#34))点击();

在下拉列表中选择值 - &gt;通过右键单击并复制xpath

来转到firpath
driver.findElement(By.xpath(".//*@id='ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input']/span[3]")).click();

希望您能找到解决方案: - )

答案 2 :(得分:0)

您可以使用:-

driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")).sendKeys("ValueTwo", Keys.ARROW_DOWN, Keys.ENTER)

答案 3 :(得分:0)

您可以使用以下代码。在这里,我要做的就是找到下拉菜单,单击它,然后找到选择并发送键的选项,直到我们看到要选择的元素,然后单击它。

public class InputDropdownselect {
@Test
public void login() throws Exception
{
System.setProperty("webdriver.chrome.driver", "G:\\drivers\\chrome\\chromedriver_win32\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://yourwebsite.com");
driver.manage().window().maximize();
driver.findElement(By.id("txtuser")).sendKeys("123456");
driver.findElement(By.id("txtpassword")).sendKeys("Abc!@1");
driver.findElement(By.id("log-btn")).click();
Thread.sleep(2000);
driver.findElement(By.id("enrollment")).click();
//driver.findElement(By.xpath("//*[@id=\"enrollment\"]")).click();
driver.findElement(By.xpath("//*[@id=\"studentBasicForm\"]/div[2]/div[9]/div/div/input")).click();
Actions action= new Actions(driver);
WebElement joiningYear=driver.findElement(By.xpath("//input[@placeholder=\"Joining Year Group\"]/following::ul[1]/descendant::li/following::span[contains(text(),\"8\")]"));
do {
    action.sendKeys(Keys.ARROW_DOWN).perform();
} while (!joiningYear.isDisplayed());
joiningYear.click();
    }

}

相关问题