本文介绍了结合使用SelectByText(部分)和C#Selenium WebDriver绑定似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#中的Selenium WebDriver扩展通过部分文本值(实际值在前面有一个空格)从选择列表中选择一个值.我无法使用部分文字匹配来使其正常工作.我是在做错什么还是这是一个错误?

I am using the Selenium WebDriver Extensions in C# to select a value from a select list by a partial text value (the actual has a space in front). I can't get it to work using a partial text match. Am I doing something wrong or is this a bug?

可复制的示例:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace AutomatedTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://code.google.com/p/selenium/downloads/list");
            var selectList = new SelectElement(driver.FindElement(By.Id("can")));
            selectList.SelectByText("Featured downloads");
            Assert.AreEqual(" Featured downloads", selectList.SelectedOption.Text);
            selectList.SelectByValue("4");
            Assert.AreEqual("Deprecated downloads", selectList.SelectedOption.Text);
            driver.Quit();
        }
    }
}

提供错误:OpenQA.Selenium.NoSuchElementException: Cannot locate element with text: Featured downloads

推荐答案

SelectByText 方法已损坏,所以我编写了自己的扩展方法 SelectBySubText 来完成此任务该做的.

The SelectByText method is broken, so I wrote my own extension method called SelectBySubText to do what it is meant to do.

using System.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace AutomatedTests.Extensions
{
    public static class WebElementExtensions
    {
        public static void SelectBySubText(this SelectElement me, string subText)
        {
            foreach (var option in me.Options.Where(option => option.Text.Contains(subText)))
            {
                option.Click();
                return;
            }
            me.SelectByText(subText);
        }
    }

这篇关于结合使用SelectByText(部分)和C#Selenium WebDriver绑定似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 10:36