简体   繁体   English

使用硒(C#)从文本下拉菜单中选择一个项目

[英]Using Selenium (C#) Select an item from a drop down by text

I have been trying to figure this out and could use some help from Selenium experts here. 我一直在试图弄清楚这一点,可以在这里使用Selenium专家的一些帮助。 I have a dropdown on my page that I am trying to select an option for from the test and I cant seem to get this right. 我在页面上有一个下拉列表,试图从测试中选择一个选项,但我似乎无法正确地做到这一点。 I have tried the following example I see commonly for select Item. 我已经尝试了以下常见于select Item的示例。 The options always show text as an empty string "" even though I can see in the HTML markup the option text is there. 即使我可以在HTML标记中看到选项文本,这些选项也始终将文本显示为空字符串 “”。

wait.Until(d => ExpectedConditions.ElementExists(By.Id("Cars")));
var carStatusDropDown = new SelectElement(_driver.FindElement(By.Id("Cars")));
carStatusDropDown.SelectByText(filter.CarStatus);

I figured its because maybe the options are not visible to the browser, so then I tried to click on the button next to dropdown (Bootstrap) so it would be open before it selects the text 我想通了它,因为这些选项可能对浏览器不可见,因此我尝试单击下拉菜单(Bootstrap)旁边的按钮,以便在选择文本之前将其打开

wait.Until(ElementIsClickable(By.XPath("/html/body/div[2]/div[2]/div/div[2]/div[1]/div/form/fieldset/div[3]/div/div[1]/span[2]/div/div/span")));
IWebElement ddlButton = _driver.FindElement(By.XPath("/html/body/div[2]/div[2]/div/div[2]/div[1]/div/form/fieldset/div[3]/div/div[1]/span[2]/div/div/span"));
ddlButton.Click();

wait.Until(d => ExpectedConditions.ElementExists(By.Id("Cars")));
var carStatusDropDown = new SelectElement(_driver.FindElement(By.Id("Cars")));
carStatusDropDown.SelectByText(filter.CarStatus);

I can see the ddlButton opens the dropdown but am still getting >OpenQA.Selenium.ElementNotVisibleException when it tries to SelectByText . 我可以看到ddlButton会打开下拉列表,但在尝试SelectByText时仍会出现> SelectByText In VS I can see the the options text still shows "" and the Displayed property is false for them. VS我可以看到选项文本仍然显示为“”,并且它们的Displayed属性为false。 How can I find the options, Any feedback would be appreciated, I am posting the HTML below whats weird is that there are two drop downs one as a <ul> and a <select> one the appears below, I am trying to access the <select> one in the test code. 我如何找到这些选项,任何反馈将不胜感激,我将HTML发布到奇怪的地方是,下面有两个下拉菜单,分别是<ul><select>一个,我正在尝试访问在测试代​​码中<select>一个。 The select one in firebug shows up as faded in the markup?!? 萤火虫中的选择者显示为在标记中褪色?!

HTML: HTML:

<div class="combobox-container">
   <input type="hidden" name="Estimate.EstimateStatus" value=" ">
   <input type="text" autocomplete="off"  
      placeholder="All"class="combobox">
   <ul class="typeahead typeahead-long dropdown-menu" style="top:   
      112px;left: 445.117px; display: none;">
      <li data-value="Approved" class=""><a href="#"><strong>
         </strong>A<strong></strong>p<strong></strong>p<strong>
         </strong>r<strong></strong>o<strong></strong>v<strong>
         </strong>e<strong></strong>d<strong></strong></a>
      </li>
      <li data-value="Hold" class=""><a href="#"><strong>
         </strong>H<strong>
         </strong>o<strong></strong>l<strong></strong>d<strong></strong>
         </a>
      </li>
      <li data-value="In Progress"><a href="#"><strong></strong>I<strong>
         </strong>n<strong></strong> <strong></strong>P<strong>
         </strong>r<strong></strong>o<strong></strong>g<strong>
         </strong>r<strong></strong>e<strong></strong>s<strong>
         </strong>s<strong></strong></a>
      </li>
      <li data-value="Not Reviewed" class="active">
         <a href="#"><strong></strong>N<strong>
         </strong>o<strong></strong>t<strong></strong> <strong>
         </strong>R<strong></strong>e<strong></strong>v<strong>
         </strong>i<strong></strong>e<strong></strong>w<strong>
         </strong>e<strong></strong>d<strong></strong></a>
      </li>
      <li data-value="Rejected"><a href="#"><strong></strong>R<strong>
         </strong>e<strong></strong>j<strong></strong>e<strong>
         </strong>c<strong></strong>t<strong></strong>e<strong> 
         </strong>d<strong></strong></a>
      </li>
   </ul>
   <span class="add-on btn dropdown-toggle" data-dropdown="dropdown">
   <span class="caret"></span>
   <span class="combobox-clear">
   </span>
</div>
<select id="Cars" class="combobox" 
   onchange="javascript:OnCarsStatusChanged();" style="display: none;">
   <option value=" " selected="selected">All</option>
   <option value="3ad8cf29-f0b7-441f-
      81bc-c5cfe7c0d73d">Approved</option>
   <option value="19b573c8-fc88-4abe-9c19-9ffc580a1ffe">Hold</option>
   <option value="e3a8b2bd-c3eb-47c3-95e7-f15da2f3d25b">In      
      Progress
   </option>
   <option value="962e4bce-1ca5-404f-b7a0-6a3a42ad91a0">Not 
      Reviewed
   </option>
   <option value="c110e83a-5a93-4553-
      a907-02a47ca97467">Rejected</option>
</select>

It does seems like there is another action depended on this dropdwon. 似乎还有另一项操作取决于此dropwwon。 By default this dropdown is NOT visible. 默认情况下,此下拉列表可见。 So, best will be to see what previous action makes it visible. 因此,最好的办法是查看先前的操作使其可见。

However, another workaround can be to execute a simple javascript, make the dropdown list visible and select the intended element you need. 但是,另一种解决方法是执行简单的javascript,使下拉列表可见,然后选择所需的预期元素。 removeAttribute should remove the style attribute to make the list visible. removeAttribute应该删除style属性以使列表可见。

((IJavaScriptExecutor)Driver).ExecuteScript("document.getElementById('Cars').removeAttribute('style');");

By id = By.Id('Cars');
var carStatusDropDown = new SelectElement(_driver.FindElement(id));
carStatusDropDown.SelectByText(filter.CarStatus);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Web 程序集中使用 Selenium 从 C# 的下拉菜单中选择项目 - 不起作用 - Select item from drop down menu in C# using Selenium in web assembly - doesn't work 如何使用Selenium Webdriver C#从下拉列表中选择列表项的值 - How to select a list item from a drop down by its value using Selenium Webdriver C# 如何在列表框中使用Selenium选择一个下拉项目,并在每个页面加载时使用动态(更改)Xpath值 - How to select a drop down item using Selenium in C# from a Listbox with Dynamic (changing) Xpath value on every page load 使用C#的Selenium WebDriver:从下拉菜单中选择项(在IE中不起作用) - Selenium WebDriver with C#: select item from drop-down menu (doesn't work in IE) 如果下拉菜单不是选择元素,如何将Webdriver与C#一起使用,如何通过文本选择下拉项? - Using webdriver with c# how can I select a drop down item by text if the drop down menu is not a select element? 使用Selenium WebDriver C#在下拉列表中选择每个选项 - Select each option in a drop down using Selenium WebDriver C# 如何从下拉列表中选择值(Selenium C#) - How to select value from the drop down list (Selenium C#) 如何使用 Selenium WebDriver C# 从下拉列表中选择一个选项? - How to select an option from drop down using Selenium WebDriver C#? 使用Selenium从C#的下拉菜单中选择选项 - Selecting Options from a Drop Down Menu in C# using Selenium WebDriver C# - 使用部分文本从下拉列表中选择项目 - WebDriver C# - Selecting an item from a drop down list using partial text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM