C#Selenium - 下拉菜单/组合框

时间:2017-08-31 15:12:00

标签: c# html css selenium xpath

我在从自定义下拉菜单中选择时遇到问题。我已尝试使用XPath,CssSelector和Id。

我在这里添加了代码链接:

Picture of the code

我想我必须访问div class =" SelectBox"为了访问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']"));

有人能给我一些关于如何访问下拉列表中项目的线索吗?

谢谢! :)

2 个答案:

答案 0 :(得分:0)

你需要使用" SelectElement"而不是" IWebElement"。

.jumbotron {background-image: url("images/desktop-urania.jpg")}

答案 1 :(得分:-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]);
相关问题