none
知道一个对象的地址,怎么获取这个对象的属性 RRS feed

  • 问题

  • 现在我用工具抓到了页面上一个对象的地址path,这个对象有很多的属性,现在怎么根据这个地址,来的到这个对象的属性值
    2016年6月27日 6:32

答案

  • Hi,

    就是类似想DataTable那样的? 你获取的对象在第四列,你想要获取全部行的第四列的数据, 类似下文这种?

                List<string> list = new List<string>();
                foreach (DataRow row in dt.Rows)
                    list.Add(row[4].ToString());

    用个foreach遍历集合,把行数据类型作为遍历的类型,这样直接输出或者用数组接收指定列就行了,不要考虑遍历数据的index了。

    例外,能提供些代码吗? 光靠描述很难提供有效的帮助。

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2016年7月1日 7:15

全部回复

  • 需要将html网页文本读取到内存中,再用html parser分析得出

    http://stackoverflow.com/questions/56107/what-is-the-best-way-to-parse-html-in-c

    推荐 Html Agility Pack


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年6月27日 11:18
  • Hi,

    >>现在怎么根据这个地址,来的到这个对象的属性值

    能否提供些详细信息?

    另外,你可以参考下用htmlelement获取对应属性:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    namespace NewProject
    {
        public class HtmlElementHelper
        {
    
            public WebBrowser Browser { get; private set; } = new WebBrowser();
    
            public HtmlElementHelper(WebBrowser wb)
            {
                Browser = wb;
            }
    
            /// <summary>
            ///  use name get element
            /// </summary>
            /// <param name="Name"></param>
            /// <returns></returns>
            public HtmlElement GetElement_Name(string Name)
            {
                HtmlElement e = Browser.Document.All[Name];
                return e;
            }
            /// <summary>
            /// use id get element
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            public HtmlElement GetElement_Id(string id)
            {
                HtmlElement e = Browser.Document.GetElementById(id);
                return e;
            }
            /// <summary>
            /// use index get element
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            public HtmlElement GetElement_Index(int index)
            {
                HtmlElement e = Browser.Document.All[index];
                return e;
            }
            /// <summary>
            /// use type to get element (unknow name and id)
            /// </summary>
            /// <param name="type"></param>
            /// <returns></returns>
            public HtmlElement GetElement_Type(string type)
            {
                HtmlElement e = null;
                HtmlElementCollection elements = Browser.Document.GetElementsByTagName("input");
                foreach (HtmlElement element in elements)
                {
                    if (element.GetAttribute("type") == type)
                    {
                        e = element;
                    }
                }
                return e;
            }
            /// <summary>
            /// use type to get specify element
            /// </summary>
            /// <param name="type">type</param>
            /// <param name="index">index</param>
            /// <returns></returns>
            public HtmlElement GetElement_Type_No(string type, int index)
            {
                int j = 1;
                HtmlElement e = null;
                HtmlElementCollection elements = Browser.Document.GetElementsByTagName("input");
                foreach (HtmlElement element in elements)
                {
                    if (element.GetAttribute("type") == type)
                    {
                        if (j == index)
                        {
                            e = element;
                        }
                        j++;
                    }
                }
                return e;
            }
            /// <summary>
            /// get form's name
            /// </summary>
            /// <param name="form_name"></param>
            /// <returns></returns>
            public HtmlElement GetElement_Form(string formName)
            {
                HtmlElement e = Browser.Document.Forms[formName];
                return e;
            }
            /// <summary>
            /// set element's value
            /// </summary>
            /// <param name="e"></param>
            /// <param name="value"></param>
            public void Write_value(HtmlElement e, string value)
            {
                e.SetAttribute("value", value);
            }
            /// <summary>
            /// call element's method
            /// e.g. click,submit
            /// </summary>
            /// <param name="e"></param>
            /// <param name="s"></param>
            public void InvokeMethod(HtmlElement e, string s)
            {
                e.InvokeMember(s);
            }
        }
    }
    

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2016年6月28日 7:43
  • 比如说,只是举个例子,我获取了一款软件里的一个对象的地址,就好比Excel中的一个单元格C4它的path为:.//table[@name='Sheet1']/row[@index='4']/cell[@address='C4']

    正常情况下是这样获取这个单元格属性text的值:TEXT= repo.工作薄1Excel.CellC4.Element.GetAttributeValueText("Text")

    但是我现在希望能用c4单元格的path代替它,GetAttributeValueText("Text")得到它的值。

    因为在软件界面上存在很多个这样的对象,它的path相同,只不过其中的row不同,我希望能用地址代替掉具体的对象,从而对row进行参数化,获得界面上所有对象的text的值。

    另外不是网页上的对象,而是客户端的这种

    2016年6月28日 11:46
  • Hi,

    就是类似想DataTable那样的? 你获取的对象在第四列,你想要获取全部行的第四列的数据, 类似下文这种?

                List<string> list = new List<string>();
                foreach (DataRow row in dt.Rows)
                    list.Add(row[4].ToString());

    用个foreach遍历集合,把行数据类型作为遍历的类型,这样直接输出或者用数组接收指定列就行了,不要考虑遍历数据的index了。

    例外,能提供些代码吗? 光靠描述很难提供有效的帮助。

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2016年7月1日 7:15