用 C# 与网页交互
有一个使用 ColdFusion 创建的网站(不确定这是否重要)。我需要与该网站进行交互。我需要做的主要事情是导航到不同的页面并单击按钮。
关于如何做到这一点,我提出了两个想法。第一种是使用WebBrowser 控件。有了这个,我当然可以导航页面,并单击按钮(根据 这个)。
另一种方式是直接与 html 交互。不确定具体如何执行此操作,但我假设我可以单击按钮或使用 HTML 请求与页面交互。
有人建议哪种方式更好吗?还有我没有想到的更好的方法吗?
There is a website that was created using ColdFusion (not sure if this matters or not). I need to interact with this web site. The main things I need to do are navigate to different pages and click buttons.
I have come up with two ideas on how to do this. The first is to use the WebBrowser control. With this, I could certainly navigate pages, and click buttons (According to This).
The other way is to interact with the html directly. Not sure exactly how to do this, but I am assuming I could click buttons or use HTML requests to interact with the page.
Does anyone have a recommendation on which way is better? Is there a better way that I haven't thought of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会使用 Html AgilityPack 来解析 html,然后适当地执行 POST 和 GET与 HttpWebRequest。
虽然可以使用 WebBrowser 控件来模拟点击和导航,但您可以使用 Html AgilityPack 和 HttpWebRequest 对发送的内容进行更多控制
I'd use Html AgilityPack to parse the html and then do POSTs and GETs appropriately with HttpWebRequest.
While it may be possible to use the WebBrowser control to simulate clicks and navigation you get more control with Html AgilityPack and HttpWebRequest regarding what gets sent
您是否考虑过Selenium? WebDriver API 非常好,并且允许做很多事情网站自动化术语。
Did you consider Selenium? The WebDriver API is quite good, and permits a lot of things in terms of Website automation.
为什么不直接提交url呢?这就是单击按钮将执行的操作。
使用 WebRequest.Create 您可以直接提交到网址。无需加载、解析和“单击”按钮。
why not submit directly the url? that's what the button click will do.
using WebRequest.Create you can submit directly to the url. no need to load, parse and "click" the button.
HtmlAguilityPack 对于轻松提取 Web 元素和查找标签非常有用。不过,如果您需要远程“引导”网络会话,我更喜欢使用 WatiN。它标榜自己是一个 Web 单元测试框架,但只要您需要伪造浏览器部分,它就非常有用。此外,它可以很好地远程控制不同的浏览器来完成您需要的大多数任务(例如找到一个按钮并按下它,或者如果您需要登录则找到一个文本字段并填写文本)。
HtmlAguilityPack is useful for pulling the web elements and finding tags easily. If you need to remotely "steer" a web session, though, I prefer to use WatiN. It bills itself as a web unit testing framework, but it's very useful anytime you need to fake a browser section. Further, it can remote control different browsers well enough for most tasks you'll need (like finding a button and pushing it, or a text field and filling in text if you need a login).