Selenium WebDriver选择combobox项目?
我们正在使用Selenium WebDriver和JBehave在我们的网络应用程序上运行“集成”测试。 我有一个方法将值输入表单输入。
@When("I enter $elementId value $value") public void enterElementText(final String elementId, final String value) { final WebElement webElement = webdriver.findElement(By.id(elementId)); webElement.clear(); webElement.sendKeys(value); }
但是,当我尝试使用它来选择下拉列表中的项目时(不出所料)会失败
java.lang.UnsupportedOperationException:您只能设置作为输入元素的元素的值
如何在组合中选择一个值?
这是怎么做的:
@When("I select $elementId value $value") public void selectComboValue(final String elementId, final String value) { final Select selectBox = new Select(web.findElement(By.id(elementId))); selectBox.selectByValue(value); }
Selenium中的Support包包含您所需要的一切:
using OpenQA.Selenium.Support.UI; SelectElement select = new SelectElement(driver.findElement( By.id( elementId ) )); select.SelectByText("Option3"); select.Submit();
您可以通过NuGet将其作为单独的包导入: http : //nuget.org/packages/Selenium.Support
通过使用ext jscomboboxtypeAhead在UI中显示值。
var theCombo = new Ext.form.ComboBox({ ... id: combo_id, typeAhead: true, ... }); driver.findElement(By.id("combo_id-inputEl")).clear(); driver.findElement(By.id("combo_id-inputEl")).sendKeys("The Value you need"); driver.findElement(By.id("combo_id-inputEl")).sendKeys(Keys.ARROW_DOWN); driver.findElement(By.id("combo_id-inputEl")).sendKeys(Keys.ENTER);
如果这不起作用,这也值得一试
driver.findElement(By.id("combo_id-inputEl")).sendKeys("The Value you need"); driver.findElement(By.className("x-boundlist-item")).click();
Selenium范例是你应该模拟用户在现实生活中会做些什么。 这可能是点击或导航键。
Actions builder = new Actions( driver ); Action action = builder.click( driver.findElement( By.id( elementId ) ) ).build(); action.perform();
只要你有一个工作选择器输入findElement,你应该没有问题。 我发现CSS选择器对于涉及多个元素的事情来说是更好的选择。 你有样本页面吗?