C#硒 - 下拉菜单/组合框
问题描述:
我有一个问题,从自定义下拉菜单中选择..我已经尝试通过使用XPath,CssSelector和Id。C#硒 - 下拉菜单/组合框
我添加了一个链接到这里的代码:
我想我已经访问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']"));
能有人给我如何可以访问在下拉列表中的项目线索?
谢谢! :)
答
尝试这个
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
我需要访问
答
你需要使用 “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");
发布您尝试使用该元素的html元素。 – IamBatman
请仔细阅读[问],特别是关于[mcve](MCVE)的部分,以及[预计需要多少研究工作?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort这是预期的堆栈溢出用户)这将帮助您调试自己的程序并为自己解决问题。如果你这样做并且仍然卡住,你可以回过头来发布你的MCVE,你试过的,以及执行结果,包括任何错误信息,这样我们可以更好地帮助你。还提供了一个链接到页面和/或相关的HTML。 – JeffC
请阅读为什么[代码截图是一个坏主意](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-orrors)。代码粘贴并正确格式化。 – JeffC