使用硒的下拉选择自动化

时间:2013-04-09 11:18:11

标签: selenium automated-tests selenium-webdriver

enter image description here

我需要自动选择硒中的下拉框。下面给出了包含下拉列表的html代码。每个组件的html代码都是使用firebug进行的。

// Input Area

<input type="text" title="Filter by Administrator" readonly="readonly" value="Filter by   Administrator" id="dnn_ctr373_View_BusinessList_admin_dd_Input" class="rcbInput   rcbEmptyMessage" name="dnn$ctr373$View$BusinessList_admin_dd" autocomplete="off">

// Dropdown arrow

<a style="overflow: hidden;display: block;position: relative;outline: none;"   id="dnn_ctr373_View_BusinessList_admin_dd_Arrow">select</a>

// Values inside drop down

<ul class="rcbList" style="list-style:none;margin:0;padding:0;zoom:1;">
<li class="rcbItem ">All</li>
<li class="rcbItem ">dat@nykkos.com</li>
<li class="rcbHovered ">dat1@nykkos.com</li>
</ul>

我根据here的回复,使用以下代码自动设置下拉列表。

Select select = new Select(driver.findElement(By.xpath("//*[@id=\"dnn_ctr373_View_BusinessList_admin_dd_Input\"]")));
select.deselectAll();
select.selectByVisibleText("All");

代码给出了以下异常:

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been  "select" but was "input"
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:18'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.35-32-generic',  java.version: '1.6.0_26'
Driver info: driver.version: unknown
at org.openqa.selenium.support.ui.Select.<init>(Select.java:46)
at FilterByAdministrator.testFilterByAdministrator(FilterByAdministrator.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我不确定,我上面用于下拉选择的代码是否正确。

有人可以告诉我相同的解决方案。

5 个答案:

答案 0 :(得分:3)

试试这个:

driver.findElement(By.xpath("//*[text()='select']")).click();
driver.findElement(By.xpath("//*[text()='All']")).click();

它可能会奏效。根据你的HTML,它看起来不像一个选择框。您可以通过正常的点击操作来实现它。

答案 1 :(得分:2)

使用:

driver.findElement(By.id("dnn_ctr373_View_BusinessList_admin_dd_Input"));

而不是:

driver.findElement(By.xpath("//*[@id=\"dnn_ctr373_View_BusinessList_admin_dd_Input\"]")));

修改

尝试下面的代码,让我知道它是否正常工作。

WebElement DropDown = driver.findElement(By.id("dnn_ctr373_View_BusinessList_admin_dd_Input"));

Dropdown.sendKeys("All");

答案 2 :(得分:1)

如果您阅读错误消息,则说明了

Element should have been  "select" but was "input"

表示所讨论的Web元素是输入字段类型。 因此,不要选择,而是尝试输入/ send您的输入,如此

driver.findElement(By.id("dnn_ctr373_View_BusinessList_admin_dd_Input")).sendKeys("All");

答案 3 :(得分:1)

在python webdriver下拉列表选择是非常简单的方法。请参阅以下代码。

Select(driver.find_element_by_xpath("xpath")).select_by_visible_text(" enter text with you want select")

它应该在python selenium webdriver中工作。

答案 4 :(得分:1)

带有区分大小写的上面的线条

Driver.Instance.FindElement(By.XPath("//*[text()='select']")).Click();
Driver.Instance.FindElement(By.XPath("//*[text()='All']")).Click();