c# webBrowser 模拟点击超链接问题--在线等

dogslife 2009-07-03 12:23:58


举例说明吧

我用webBrowser打开一个网页。这个网页中的<A>元素都没有id name等属性。只有href但是每一个还不一样。

我但是超链接的value都是相同的。

我想请问怎么遍历超链接的value,并且在找到需要的超链接之后模拟鼠标点击,或者直接打开他的href也行?

希望各路英雄豪杰一定帮忙。
...全文
4671 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
U2008 2010-09-09
  • 打赏
  • 举报
回复
好rrrrrrrrrr
psho78 2009-07-29
  • 打赏
  • 举报
回复
mark
dogslife 2009-07-05
  • 打赏
  • 举报
回复
说一下成果。
365的4Q已经全部实现。
在优化一下就好了。
哈哈高兴。

谢谢shrinerain的帮助
liyoubaidu 2009-07-03
  • 打赏
  • 举报
回复
up
dogslife 2009-07-03
  • 打赏
  • 举报
回复
好了。

邮件以发送
dogslife 2009-07-03
  • 打赏
  • 举报
回复
25楼从下面这行看

报过异常的代码


我总是先在记事本里写,再粘过去,这次站多了
dogslife 2009-07-03
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 shrinerain 的回复:]
不不. 我不需要你其他的代码, 我只关注Autotester执行功能的部分就可以了.

你给我一个用户名密码, 告诉我你的动作流程, 我看看你说的"TextBox" 无法获取.
[/Quote]

我发邮件告诉您
shrinerain 2009-07-03
  • 打赏
  • 举报
回复
你加qq群90799215
dogslife 2009-07-03
  • 打赏
  • 举报
回复
遇到问题了。我不客气的问问题了,呵呵。

下面这句话有问题吗?

//父类 子类
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()

















dogslife 2009-07-03
  • 打赏
  • 举报
回复
我马上把报过异常的代码发上来。
又有个新的异常了
我正在重新测试,马上好
shrinerain 2009-07-03
  • 打赏
  • 举报
回复
不不. 我不需要你其他的代码, 我只关注Autotester执行功能的部分就可以了.

你给我一个用户名密码, 告诉我你的动作流程, 我看看你说的"TextBox" 无法获取.
dogslife 2009-07-03
  • 打赏
  • 举报
回复

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;
}
}

dogslife 2009-07-03
  • 打赏
  • 举报
回复
老是没说完就提交了。

还有一个 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; }
}
}
}




dogslife 2009-07-03
  • 打赏
  • 举报
回复
那两个循环不算。
shrinerain 2009-07-03
  • 打赏
  • 举报
回复
哪些控件不能获得的你可以到google code那里报bug, 我会处理的.
dogslife 2009-07-03
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 shrinerain 的回复:]
你把完整的程序贴出来.
[/Quote]

对不起呀。
没刷新,
没及时是我的损失。。
dogslife 2009-07-03
  • 打赏
  • 举报
回复
来了


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;
}
}
}

shrinerain 2009-07-03
  • 打赏
  • 举报
回复
你可以加群90799215
dogslife 2009-07-03
  • 打赏
  • 举报
回复
还是模糊搜索好用,虽然慢一点,但现在至少解决问题了。
高兴。。估计迷你博客也可以自动留言了。高兴的真早。。

这网站有的标签,元素不好获得
shrinerain 2009-07-03
  • 打赏
  • 举报
回复
你把完整的程序贴出来.
加载更多回复(13)

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧