2013-04-09 47 views
0

enter image description here使用硒的下拉選擇自動化

我需要自動選擇硒中的下拉框。下面給出了包含下拉列表的HTML代碼。每個組件的html代碼都使用螢火蟲。

// 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 ">[email protected]</li> 
<li class="rcbHovered ">[email protected]</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) 

我不知道,我在上面使用下拉菜單選擇的代碼是否正確。

有人可以讓我知道相同的可能的解決方案。

回答

2

試試這個:

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

它可能會奏效。根據你的html,它看起來不像一個選擇框。您可以通過正常的點擊操作來實現它。

+0

上面評論中提到的同樣問題的工作就像一個魅力..謝謝! – user1400538 2013-04-11 11:42:32

+0

上面的行區分大小寫touchup Driver.Instance.FindElement(By.XPath(「// * [text()='select']」))。Click(); ();();();(); – Kurkula 2016-02-15 18:29:22

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"); 
+0

行我與給定的代碼 – user1400538 2013-04-09 12:30:14

+0

檢查編輯我的回答同樣的異常,讓我知道如果你仍然面臨着同樣的問題。 – Hemanth 2013-04-09 12:37:05

+0

也許'Dropdown.click'會更好。然後你將不得不找到下拉列表中顯示的元素。 – 2013-04-09 12:39:10

1

如果你讀它說的錯誤信息 -

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

這表明有問題的網頁元素是一個輸入字段類型。 因此,而不是選擇,嘗試輸入/ send您的輸入,像這樣

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

這也不起作用。我在HemChe的回答 – user1400538 2013-04-10 07:37:34

1

在蟒蛇webdriver的下拉菜單選擇是非常簡單的方式。請參閱下面的代碼。

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

它應該是在python硒webdriver工作。

0

上面帶有區分大小寫潤色

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