繁体   English   中英

如何在C#中使用WebDriver根据XML元素的'类型'属性添加两个动作

[英]How to add two actions depending on 'type' attribute of element in XML using webdriver in c#

我将元素存储在config.xml文件中作为我的项目的一部分,目前我有一个“ setData”方法,该方法将通过id查找元素,然后将其值设置为用户输入(使用名为FireFoxBrowser的Webdriver实例)

我想向xml添加类型属性,以区分将使用当前代码的“输入”和“按钮”来添加将单击此类型的任何内容的代码。 如何使用webdriver编写此代码?

    public void setData(string elementName, string elementValue)
    {
        XmlDocument docXml = null;
        try
        {
            docXml = new XmlDocument();

            string xmlPath = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName + @"\config.xml";
            docXml.Load(xmlPath);
            XmlNode nd = docXml.SelectSingleNode(string.Format(@"//page[@url='{0}']", FireFoxBrowser.Url.ToString()));
            if (nd != null)
            {
                var id = nd.SelectSingleNode(string.Format(@"element[@name='{0}']", elementName)).Attributes["id"].Value;

                FireFoxBrowser.FindElement(By.Id(id)).Clear();
                FireFoxBrowser.FindElement(By.Id(id)).SendKeys(elementValue);
            }
        }
        finally
        {
            if (docXml != null)
                docXml = null;
        }

我可以使用下面的代码行来区分类型属性集,以实现此目的:

var id = nd.SelectSingleNode(string.Format(@“ element [@name ='{0}']”,elementName))。Attributes [“ id”]。Value;

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM