举例说明吧我用webBrowser打开一个网页。这个网页中的<A>元素都没有id name等属性。只有href但是每一个还不一样。我但是超链接的value都是相同的。我想请问怎么遍历超链接的value,并且在找到需要的超链接之后模拟鼠标点击,或者直接打开他的href也行?希望各路英雄豪杰一定帮忙。
解决方案 »
- c#调用c++ 动态库报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”。错误
- 线程里面的Lock 求指导.
- C# 如何勾选Excel复选框
- 设计模式之桥接模式问题
- 一页打印多个DataGridview代码,希望大家来帮忙优化下!
- 怎么把byte[] 写入文件?
- C#2.0为什么没有实现泛型的 Hashtable?
- 在使用WEB服务端的控件Button提交时,如何利用js同时提交一个变量?
- [求助]]LayoutMdi方法的实现代码
- 怎样用正则表达式解析一个算式所有的变量?
- 江湖救急,第二张贴,怎样设置路径,才能将生成的xml文件保存。
- Framework 3.5编译的程序不能在4.0下运行
ts.Objects.Button("XXX(i)").Click();
有什么好方法实现吗?
我总不能这样吧?效率太低了。int i = 0;
while (true)
{
try
{
ts.Objects.Link("XXX(" + i + ")").Click();
return;
}
catch{}
finally { i++; }
}有好方法吗?
HTMLTestLink link = ts.Objects.Link("xxx") as HTMLTestLink;
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();
}
谢谢。
我先慢慢研究一下,消化消化。
再次感谢。还有新版本在哪里下载呀?
暂时先不结贴。但人格保证绝对是您的,当然可能也不在乎。关键是我不知道结贴后还能回复吗。一个小白的自白
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();//Exception
foreach (IClickable a in aaa)
{
//a.Click();
textBox1.Text = "";
textBox1.Text += a.ToString()+"\r\n";
}
这样也报异常。
为什么呢?我可以实现自动留言,迷你博客在测试时也可以通过指定的方式来做。日志不行,找不到textbox 但是它有一个超链接可以自动让输入框阔的焦点,关键就是怎么输入了,如果用剪切板那就有点不完美了。对了您有那个动态链接库的帮助文档吗?
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;
}
}
}
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; }
}
}
}
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;
}
}
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()
365的4Q已经全部实现。
在优化一下就好了。
哈哈高兴。谢谢shrinerain的帮助