举例说明吧我用webBrowser打开一个网页。这个网页中的<A>元素都没有id name等属性。只有href但是每一个还不一样。我但是超链接的value都是相同的。我想请问怎么遍历超链接的value,并且在找到需要的超链接之后模拟鼠标点击,或者直接打开他的href也行?希望各路英雄豪杰一定帮忙。

解决方案 »

  1.   

    难道真的没人会吗?那AutoTesterLib.dll有人知道吗?我要
    ts.Objects.Button("XXX(i)").Click();
    有什么好方法实现吗?
    我总不能这样吧?效率太低了。int i = 0;
                while (true)
                {
                    try
                    {
                        ts.Objects.Link("XXX(" + i + ")").Click();
                        return;
                    }
                    catch{}
                    finally { i++; }
                }有好方法吗?
      

  2.   

    AutoTesterLib提供了多种方式获取控件.你可以用正则表达式,属性值前面加@就可以了, 比如ts.Objects.Links("href=@[0-9].sina.com");甚至你什么都不用做, ts.Objects.Links();返回所有链接.你可以将每个链接转化为HTMLTestLink类型, 你可以获取任意想要的信息, 比如href, text之类.
    HTMLTestLink link = ts.Objects.Link("xxx") as HTMLTestLink;
      

  3.   

    关于你email的问题. 可以考虑用正则表达式.
    AutoTester里面, 如果属性值以@符号开头, 表示属性值为正则表达式.            string url = "xxx";
                HTMLTestSession ts = new HTMLTestSession();
                ts.Browser.Start(url);
                HTMLTestLink[] links = ts.Objects.Links("@评论(\\d)");
                foreach (HTMLTestLink link in links)
                {
                    link.Click();
                }
      

  4.   

    没想到您能这么快就回复Email
    谢谢。
    我先慢慢研究一下,消化消化。
    再次感谢。还有新版本在哪里下载呀?
    暂时先不结贴。但人格保证绝对是您的,当然可能也不在乎。关键是我不知道结贴后还能回复吗。一个小白的自白
      

  5.   

    遇到问题了。我不客气的问问题了,呵呵。下面这句话有问题吗?//父类                        子类
    TestSession ts = new HTMLTestSession();ts.Objects.Link("@迷你博客(\\d)").Click();
    异常信息未处理 Shrinerain.AutoTester.Core.ObjectNotFoundException
      Message="Shrinerain.AutoTester.Core.ObjectNotFoundException: Can not find object by type [Link]\r\n   在 Shrinerain.AutoTester.HTMLUtility.HTMLTestObjectPool.GetObjectsByType(String type, TestProperty[] properties)\r\n   在 Shrinerain.AutoTester.Core.TestObjectMap.TryGetObjectsFromPool(String type, TestProperty[] properties, TestObject[]& obj, TestException& exception)"
      Source="AutoTesterLib"
      StackTrace:
           在 Shrinerain.AutoTester.Core.TestObjectMap.GetMapObjects(String type, String name)
           在 Shrinerain.AutoTester.Core.TestObjectMap.Links(String name)
           在 webWindowsApplication8.Form1.Work() 位置 E:\MyDoucument\C#Files\webWindowsApplication8\webWindowsApplication8\Form1.cs:行号 122
           在 webWindowsApplication8.Form1.tsmiStart_Click(Object sender, EventArgs e) 位置 E:\MyDoucument\C#Files\webWindowsApplication8\webWindowsApplication8\Form1.cs:行号 90
           在 System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
           在 System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
           在 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
           在 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
           在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
           在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
           在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
           在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           在 System.Windows.Forms.Control.WndProc(Message& m)
           在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           在 System.Windows.Forms.ToolStrip.WndProc(Message& m)
           在 System.Windows.Forms.MenuStrip.WndProc(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.Run(Form mainForm)
           在 webWindowsApplication8.Program.Main() 位置 E:\MyDoucument\C#Files\webWindowsApplication8\webWindowsApplication8\Program.cs:行号 17
           在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()还有这句HTMLTestLink[] links = ts.Objects.Links("@迷你博客(\\d)");返回类型需要强转我写成
                IClickable[] aaa = ts.Objects.Links("@迷你博客(\\d)");
                foreach (IClickable a in aaa)
                {
                    a.Click();
                }
    但还是报上面的那个异常。
    复制了一下连接,发现有个空格,代码中测试
    (\\d)后面有没有空格都一样报。我查看了源代码,如下<li style="width:161px;padding-left:7px; margin-top:2px;">
    <a href="home!mini.do?id=10420577">迷你博客(2)</a>
    </li>其中 href超链接中的内容也不好找规律。
    但是“迷你博客”是一直存在的。这该怎么解决呢?
    我想有没有一种类似于ms sqlserver中的模糊搜索的方法呀?
      

  6.   

    新版本就在google code的项目主页就可以下载.只所以需要强制转型是因为你用的TestSession ts, 我贴的代码是HTMLTestSession ts.模糊搜索是有的.ts.Objects.ObjectPool.EnableFuzzySearch(true); //启用模糊搜索.HTMLTestLink[] links = ts.Objects.Links("迷你博客(*)"); //返回所有迷你博客的链接.
      

  7.   

    问题到底在哪里呢?
                ts.Browser.Start("http://www.yy365.com"); 
                //ts.Browser.Load("http://www.yy365.com", true); 
                ts.Objects.Link("好友").Click();
                ts.Objects.Link("XXX").Click();            IClickable[] aaa = ts.Objects.Links();//Exception
                foreach (IClickable a in aaa)
                {
                    //a.Click();
                    textBox1.Text = "";
                    textBox1.Text += a.ToString()+"\r\n";
                }
    这样也报异常。
      

  8.   


    为什么呢?我可以实现自动留言,迷你博客在测试时也可以通过指定的方式来做。日志不行,找不到textbox 但是它有一个超链接可以自动让输入框阔的焦点,关键就是怎么输入了,如果用剪切板那就有点不完美了。对了您有那个动态链接库的帮助文档吗?
      

  9.   

    来了
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;using Shrinerain.AutoTester.Core;
    using Shrinerain.AutoTester.MSAAUtility;
    using Shrinerain.AutoTester.HTMLUtility; namespace webWindowsApplication8
    {
        public partial class Form1 : Form
        {
            /// <summary>
            /// 网页对象
            /// </summary>
            TestSession ts = new HTMLTestSession();        /// <summary>
            /// datagrideview对象的值是否改变
            /// </summary>
            bool Change=false;        public Form1()
            {
                InitializeComponent();
            }
            [STAThread]
            private void Form1_Load(object sender, EventArgs e)
            {
                
            }        /// <summary>
            /// 无限重复直到为真
            /// </summary>
            /// <param name="link">你要链接的超链接名字,不包含括号</param>
            public void linkClickEndity(string link)
            {
                int i = 0;
                while (true)
                {
                    try
                    {
                        ts.Objects.Link(link+"(" + i + ")").Click();
                        return;
                    }
                    catch{}
                    finally { i++; }
                }
                
            }        /// <summary>
            /// 评论     无限重复直到为真
            /// </summary>
            /// <param name="link">你要链接的超链接名字,不包含括号</param>
            public void linkPingLunClickEndity(string link)
            {
                int i = 0;
                while (true)
                {
                    try
                    {
                        ts.Objects.Link(link+"(" + i + "条)").Click();
                        return;
                    }
                    catch { }
                    finally { i++; }
                }        }        /// <summary>
            /// 点击开始按钮
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void tsmiStart_Click(object sender, EventArgs e)
            {
                if (tsmiStart.Text.Equals("开始 [Ctrl+Alt+Shift+S]"))
                {
                    tsmiStart.Text = "停止 [Ctrl+Alt+Shift+S]";
                    Work();
                }
                else
                {
                    tsmiStart.Text = "开始 [Ctrl+Alt+Shift+S]";
                }        }        /// <summary>
            /// 开始工作
            /// </summary>
            public void Work()
            {
                ts.Browser.Start(); //打开默认浏览器    http://www.yy365.com/
                ts.Browser.Load("http://www.yy365.com", true); //用浏览器打开网页
                ts.Objects.Link("好友").Click();//点击好友超链接
                ts.Objects.Link("XXX").Click();//点击该好友的名字超链接            ts.Objects.ObjectPool.EnableFuzzySearch(true); //启用模糊搜索.             //HTMLTestLink[] links = ts.Objects.Links("迷你博客(*)"); //返回所有迷你博客的链接. 
                IClickable[] aaa = ts.Objects.Links("迷你博客(*)");
                foreach (IClickable a in aaa)
                {
                    a.Click();
                }
                aaa = ts.Objects.Links("评论(*条)");
                foreach (IClickable a in aaa)
                {
                    a.Click();
                }            IVisible [] iv = ts.Objects.AnyObjects();
                foreach (IClickable a in iv)
                {
                    textBox1.Text = "";
                    textBox1.Text += a + "\r\n\r\n";
                }
            }
            /// <summary>
            /// 如果修改了好友信息,在切换界面时,会有提示
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                Change = true;
            }        /// <summary>
            /// 如果修改了语句信息,在切换界面时,会有提示
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                Change = true;
            }
        }
    }
      

  10.   

    哪些控件不能获得的你可以到google code那里报bug, 我会处理的.
      

  11.   

    老是没说完就提交了。还有一个 Dictionary<string,FirendInfo>的泛型和FirendInfo类,
    using System;
    using System.Collections.Generic;
    using System.Text;namespace webWindowsApplication8
    {
        class FirendInfo
        {
            private String name;        /// <summary>
            /// 好友姓名
            /// </summary>
            public String Name
            {
                get { return name; }
                set { name = value; }
            }        private bool liuyan;        /// <summary>
            /// 是否访问留言板
            /// </summary>
            public bool Liuyan
            {
                get { return liuyan; }
                set { liuyan = value; }
            }        private bool buoke;        /// <summary>
            /// 是否访问迷你博客
            /// </summary>
            public bool Buoke
            {
                get { return buoke; }
                set { buoke = value; }
            }        private bool rizhi;        /// <summary>
            /// 是否访问日志
            /// </summary>
            public bool Rizhi
            {
                get { return rizhi; }
                set { rizhi = value; }
            }        private bool xiangce;        /// <summary>
            /// 是否访问相册
            /// </summary>
            public bool Xiangce
            {
                get { return xiangce; }
                set { xiangce = value; }
            }        private bool random;        /// <summary>
            /// 是否随机输入语句
            /// </summary>
            public bool Random
            {
                get { return random; }
                set { random = value; }
            }
        }
    }
      

  12.   


    using System;
    using System.Collections.Generic;
    using System.Text;namespace webWindowsApplication8
    {
        class HelperClass
        {
            /// <summary>
            /// Static 存放好友的信息 类型:Dictionary<string,FirendInfo>
            /// </summary>
            public static Dictionary<string,FirendInfo> dicFirend;        /// <summary>
            /// Static 存放要说的语句 类型:List<String>
            /// </summary>
            public static List<String> lstSay;
        }
    }
      

  13.   

    不不. 我不需要你其他的代码, 我只关注Autotester执行功能的部分就可以了. 你给我一个用户名密码, 告诉我你的动作流程, 我看看你说的"TextBox" 无法获取.
      

  14.   

    遇到问题了。我不客气的问问题了,呵呵。下面这句话有问题吗?//父类                        子类
    TestSession ts = new HTMLTestSession();ts.Objects.Link("@迷你博客(\\d)").Click();
    异常信息未处理 Shrinerain.AutoTester.Core.ObjectNotFoundException
      Message="Shrinerain.AutoTester.Core.ObjectNotFoundException: Can not find object by type [Link]\r\n   在 Shrinerain.AutoTester.HTMLUtility.HTMLTestObjectPool.GetObjectsByType(String type, TestProperty[] properties)\r\n   在 Shrinerain.AutoTester.Core.TestObjectMap.TryGetObjectsFromPool(String type, TestProperty[] properties, TestObject[]& obj, TestException& exception)"
      Source="AutoTesterLib"
      StackTrace:
           在 Shrinerain.AutoTester.Core.TestObjectMap.GetMapObjects(String type, String name)
           在 Shrinerain.AutoTester.Core.TestObjectMap.Links(String name)
           在 webWindowsApplication8.Form1.Work() 位置 E:\MyDoucument\C#Files\webWindowsApplication8\webWindowsApplication8\Form1.cs:行号 122
           在 webWindowsApplication8.Form1.tsmiStart_Click(Object sender, EventArgs e) 位置 E:\MyDoucument\C#Files\webWindowsApplication8\webWindowsApplication8\Form1.cs:行号 90
           在 System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
           在 System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
           在 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
           在 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
           在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
           在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
           在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
           在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           在 System.Windows.Forms.Control.WndProc(Message& m)
           在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           在 System.Windows.Forms.ToolStrip.WndProc(Message& m)
           在 System.Windows.Forms.MenuStrip.WndProc(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.Run(Form mainForm)
           在 webWindowsApplication8.Program.Main() 位置 E:\MyDoucument\C#Files\webWindowsApplication8\webWindowsApplication8\Program.cs:行号 17
           在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()还有这句HTMLTestLink[] links = ts.Objects.Links("@迷你博客(\\d)");返回类型需要强转我写成
                IClickable[] aaa = ts.Objects.Links("@迷你博客(\\d)");
                foreach (IClickable a in aaa)
                {
                    a.Click();
                }
    但还是报上面的那个异常。
    复制了一下连接,发现有个空格,代码中测试
    (\\d)后面有没有空格都一样报。我查看了源代码,如下<li style="width:161px;padding-left:7px; margin-top:2px;">
    <a href="home!mini.do?id=10420577">迷你博客(2)</a>
    </li>其中 href超链接中的内容也不好找规律。
    但是“迷你博客”是一直存在的。这该怎么解决呢?
    我想有没有一种类似于ms sqlserver中的模糊搜索的方法呀?
                ts.Browser.Start("http://www.yy365.com"); 
                //ts.Browser.Load("http://www.yy365.com", true); 
                ts.Objects.Link("好友").Click();
                ts.Objects.Link("XXX").Click();            IClickable[] aaa = ts.Objects.Links();
                foreach (IClickable a in aaa)
                {
                    //a.Click();
                    textBox1.Text = "";
                    textBox1.Text += a.ToString()+"\r\n";
                }
    报过异常的代码IClickable[] aaa = ts.Objects.Links();//异常在六楼
    ts.Objects.Link("@迷你博客(\\d) ").Click();//异常在六楼IVisible [] iv = ts.Objects.AnyObjects();//异常在下面未处理 Shrinerain.AutoTester.Core.ObjectNotFoundException
      Message="Shrinerain.AutoTester.Core.CannotBuildObjectException: Can not build test button: Shrinerain.AutoTester.Core.PropertyNotFoundException: Property value not found.\r\n   在 Shrinerain.AutoTester.HTMLUtility.HTMLTestObject.GetProperty(String propertyName)\r\n   在 Shrinerain.AutoTester.HTMLUtility.HTMLTestButton..ctor(IHTMLElement element, HTMLTestBrowser browser)\r\n   在 Shrinerain.AutoTester.HTMLUtility.HTMLTestButton..ctor(IHTMLElement element, HTMLTestBrowser browser)\r\n   在 Shrinerain.AutoTester.HTMLUtility.HTMLTestObjectFactory.BuildHTMLTestObjectByType(IHTMLElement element, HTMLTestObjectType type, HTMLTestBrowser browser)\r\n   在 Shrinerain.AutoTester.HTMLUtility.HTMLTestObjectFactory.BuildHTMLTestObject(IHTMLElement element, HTMLTestBrowser browser)\r\n   在 Shrinerain.AutoTester.HTMLUtility.HTMLTestObjectPool.GetAllObjects()\r\n   在 Shrinerain.AutoTester.Core.TestObjectMap.TryGetObjectsFromPool(String type, TestProperty[] properties, TestObject[]& obj, TestException& exception)"
      Source="AutoTesterLib"
      StackTrace:
           在 Shrinerain.AutoTester.Core.TestObjectMap.GetMapObjects(String type, String name)
           在 Shrinerain.AutoTester.Core.TestObjectMap.AnyObjects(String type, String name)
           在 Shrinerain.AutoTester.Core.TestObjectMap.AnyObjects(String name)
           在 Shrinerain.AutoTester.Core.TestObjectMap.AnyObjects()
           在 webWindowsApplication8.Form1.Work() 位置 E:\MyDoucument\C#Files\webWindowsApplication8\webWindowsApplication8\Form1.cs:行号 140
           在 webWindowsApplication8.Form1.tsmiStart_Click(Object sender, EventArgs e) 位置 E:\MyDoucument\C#Files\webWindowsApplication8\webWindowsApplication8\Form1.cs:行号 90
           在 System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
           在 System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
           在 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
           在 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
           在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
           在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
           在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
           在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           在 System.Windows.Forms.Control.WndProc(Message& m)
           在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           在 System.Windows.Forms.ToolStrip.WndProc(Message& m)
           在 System.Windows.Forms.MenuStrip.WndProc(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.Run(Form mainForm)
           在 webWindowsApplication8.Program.Main() 位置 E:\MyDoucument\C#Files\webWindowsApplication8\webWindowsApplication8\Program.cs:行号 17
           在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      

  15.   

    说一下成果。
    365的4Q已经全部实现。
    在优化一下就好了。
    哈哈高兴。谢谢shrinerain的帮助