【问题标题】:Select value from ComboBox\DropDown in Selenium从 Selenium 中的 ComboBox\DropDown 中选择值
【发布时间】:2019-09-25 10:26:31
【问题描述】:

我想使用 selenium 从下拉列表中选择一个值。 值为“其他”见图片

下拉列表的 xpath 是://nz-select[@formcontrolname='selectedIntegrationTypes']

页面代码为:

这是我的代码:

public static void selectDropDownByXpath()
    {
        WebDriver driver2 = WebDriverMgr.getDriver();
        Select dropDown = new Select(driver2.findElement(By.xpath("//nz-select[@formcontrolname='selectedIntegrationTypes']")));
        dropDown.selectByVisibleText("Other");
    }

我收到此错误消息:

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "nz-select"
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'PC', ip: '12.35.12.65', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: driver.version: unknown

有人可以告诉我如何从下拉列表中选择值吗? 问候

【问题讨论】:

标签: selenium selenium-webdriver xpath selenium-chromedriver


【解决方案1】:

首先这不是select标签,它是有角度的nz-select标签,这也是错误所说的

元素应该是“select”,但是是“nz-select”

所以不能使用 Select 类,需要使用普通的脚本标准。

你需要像这样使用 Xpath:

//nz-select/ng-reflect-name='selectedIntegrationtypes'

使用标准脚本,例如:

driver.findElement(By.xpath("//nz-select/ng-reflect-name='selectedIntegrationtypes'")).click();

【讨论】:

  • 在我点击之后,它如何搜索和选择我想要的值?角度
  • 正常使用 findElements 并将元素添加到列表中。您需要使用全选选项识别值的 xpath .. 参考:stackoverflow.com/questions/36848352/…
【解决方案2】:

Select 元素需要一个“选择”标签。在您的情况下,标签是“nz-select”。因此它抛出一个错误。

我会写一个下拉类。

   public DropDownMenu(By optionsStrategy, By optionButtonStrategy, IWebDriver driver)
    {
        driver = driver;
        optionContainerStrategy = optionsContainer;
        optionsStrategy = optionsStrategy;
        optionButtonStrategy = optionButtonStrategy;
        optionButton = driver.FindElement(_optionButtonStrategy);           
    }

您可以在哪里传递选项/项目的定位器,以及下拉按钮的定位器,这将触发下拉选项值的显示。

var dropDown = new Dropdown(By.CssSelector("formcontrolname['selectedIntegrationTypes']")),By.CssSelector("[insert options/items identifier here]"), driver)

我们现在可以创建一个选择选项的方法

    public void SelectItemByName(string itemName)
    {
        Actions action = new Actions(_driver);
        action.MoveToElement(_optionButton).Click().Build().Perform();
        Thread.Sleep(500);
        GetOption(itemName, _optionsStrategy).Click();
    }

    private IWebElement GetOption(string optionName, By optionStrategy)
    {
        IWebElement optionElement = _driver.FindElements(optionStrategy).Where(x => x.Text.Trim().Trim().Equals(optionName.Trim(), StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
        return optionElement ?? throw new Exception($"Option {optionName} not found.");
    }

    private IReadOnlyCollection<IWebElement> GetOptions(By optionStrategy) => _driver.FindElements(optionStrategy);   

那你就可以dropDown.SelectItemByName("Other")使用了

【讨论】:

    【解决方案3】:

    试试 selectbyindex 函数。

    Select ddlCCType = new Select(driver.findElement(By.xpath("put xpath here..")));
    ddlCCType.selectByIndex("index value..");
    

    【讨论】:

    • 错误信息说这里不能使用Select
    • 对不起,这不起作用,1.我想按值而不是按索引选择,但错误仍然是关于nz-select而不是选择
    • 导入命名空间 import org.openqa.selenium.support.ui.Select;高于班级,而不是尝试。
    • @DharitMehta 您应该阅读错误消息Element should have been "select" but was "nz-select"。你不能在这里使用Select
    猜你喜欢
    • 2023-04-09
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    相关资源
    最近更新 更多