【问题标题】:How To select from combobox with selenium c#?如何从带有 selenium c# 的组合框中进行选择?
【发布时间】:2023-03-14 12:30:02
【问题描述】:

如何将默认值下拉列表或组合框列表更改为其他值?

我想把allshow改成onlyactive

<html>
<body>

<form method="post" action=""> <br>
First Name:<input type="text" size="12" maxlength="12" name="Fname"> <br>
Last Name:<input type="text" size="12" maxlength="36" name="Lname"> <br>
Gender:<br>
Male:<input type="radio" value="Male" name="gender"><br>
Female:<input type="radio" value="Female" name="gender"><br>
Favorite Food:<br>
Steak:<input type="checkbox" value="Steak" name="food[]"><br>
Pizza:<input type="checkbox" value="Pizza" name="food[]"><br>
Chicken:<input type="checkbox" value="Chicken" name="food[]"><br>

<div class="uk-width-1-6@m uk-grid-margin">
        <div class="uk-margin" data-select2-id="11">
            <label class="uk-form-label">Status :</label>
            <select class="uk-select js-select2 select2-hidden-accessible" name="search[active]" data-select2-id="7" tabindex="-1" aria-hidden="true">
                    <option value="" data-select2-id="9">AllShow</option>
                        <option value="1" data-select2-id="17">onlyactive</option>
                                    <option value="2" data-select2-id="18">onlyinactive</option>
            </select>
            <span class="select2 select2-container select2-container--default select2-container--below select2-container--focus" dir="rtl" data-select2-id="8" style="width: 217.325px;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-searchactive-21-container"><span class="select2-selection__rendered" id="select2-searchactive-21-container" role="textbox" aria-readonly="true" title="AllShow">AllShow</span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>
        </div>
</div>

</form>

</body>
</html>
IWebDriver driver = new FirefoxDriver(service);
IWebElement comboBox = driver.FindElement(By.Name("search[active]"));
comboBox.Click();

使用此代码:

    // select the drop down list
    var education = driver.FindElement(By.Name("education"));
    //create select element object 
    var selectElement = new SelectElement(education);

但是错误

找不到类型或命名空间名称“SelectElement”(您是 缺少 using 指令或程序集引用?)

Selenium.WebDriver.3.141.0

【问题讨论】:

  • var selectElement = new SelectElement dosnt in version ,帮帮我
  • 你是什么意思dosnt in version
  • 在selectelement初始化时指定变量,试试var selectElement = new SelectElement(comboBox);
  • 找不到类型或命名空间名称“SelectElement”(您是否缺少 using 指令或程序集引用?)

标签: c# selenium selenium-webdriver


【解决方案1】:

我相信您缺少程序集引用,SelectElement 类在命名空间OpenQA.Selenium.Support.UI 中可用。

Nuget:

https://www.nuget.org/packages/Selenium.Support/3.141.0

那么它应该可以工作了。

using OpenQA.Selenium.Support.UI;

var comboBox = driver.FindElement(By.Name("search[active]"));
new SelectElement(comboBox).SelectByText("onlyactive");

编辑:

SelectByText 提供要选择的选项的文本。如果您的组合框不包含指定的文本值,那么它将抛出 NoSuchElementException

【讨论】:

  • 仅安装 selenium.webdriver 3.141.0 并使用 OpenQA.Selenium.Support.UI;
  • 好的,亲爱的,创建新项目并为我安装和工作,谢谢。
  • 元素
  • 附加信息:元素
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 2013-09-30
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
相关资源
最近更新 更多