1

在此处输入图像描述

我需要自动选择 selenium 中的下拉框。下面给出了包含下拉列表的 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>

根据此处的回复,我使用以下代码自动进行下拉选择。

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)

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

有人可以让我知道可能的解决方案吗?

4

5 回答 5

3

试试这个:

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

它可能会起作用。根据您的 html,它看起来不像选择框。您可以通过正常的点击操作来实现它。

于 2013-04-10T11:46:33.683 回答
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");
于 2013-04-09T11:40:42.570 回答
1

如果您阅读错误消息,它会显示 -

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

这表明有问题的 Web 元素是输入字段类型。因此,不要选择,而是尝试输入/send您的输入,就像这样

driver.findElement(By.id("dnn_ctr373_View_BusinessList_admin_dd_Input")).sendKeys("All");
于 2013-04-09T12:48:56.317 回答
1

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

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

它应该在 python selenium webdriver 中工作。

于 2013-04-10T05:29:23.600 回答
1

上面带有区分大小写修饰的行

Driver.Instance.FindElement(By.XPath("//*[text()='select']")).Click();
Driver.Instance.FindElement(By.XPath("//*[text()='All']")).Click();
于 2016-02-15T18:29:49.353 回答