2017-08-31 136 views
-1

我有一個問題,從自定義下拉菜單中選擇..我已經嘗試通過使用XPath,CssSelector和Id。C#硒 - 下拉菜單/組合框

我添加了一個鏈接到這裏的代碼:

Picture of the code

我想我已經訪問DIV CLASS = 「選擇框」,以訪問ID = 'ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype'

但我不斷收到錯誤。

這是我目前想,但沒有任何運氣:

IWebElement test = driver.FindElement(By.XPath("//div[@class='input']//div[@id='ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype']")); 

能有人給我如何可以訪問在下拉列表中的項目線索?

謝謝! :)

+0

發佈您嘗試使用該元素的html元素。 – IamBatman

+0

請仔細閱讀[問],特別是關於[mcve](MCVE)的部分,以及[預計需要多少研究工作?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort這是預期的堆棧溢出用戶)這將幫助您調試自己的程序併爲自己解決問題。如果你這樣做並且仍然卡住,你可以回過頭來發布你的MCVE,你試過的,以及執行結果,包括任何錯誤信息,這樣我們可以更好地幫助你。還提供了一個鏈接到頁面和/或相關的HTML。 – JeffC

+0

請閱讀爲什麼[代碼截圖是一個壞主意](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-orrors)。代碼粘貼並正確格式化。 – JeffC

回答

-1

嘗試這個

var select = driver.FindElementById("ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype"); 

var stringValues = select.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None); 

((IJavaScriptExecutor)driver).ExecuteScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", select, stringValues[0]); 
+0

它給了我沒有錯誤,但它沒有從列表中選擇任何東西:) – jeez

+0

我需要訪問

0

你需要使用 「SelectElement」 而不是 「IWebElement」。

SelectElement mySelect = new SelectElement(yourDriver.FindElement(By.Id("ctl00_ctl00_ctl00_MP_Blank_Body_MP_Base_Body_MP_TopSideMenu_Body_ctl00_cboBehandlingstype"))); 

mySelect.SelectByText("510111 Normalbehandling");