Windows 窗体应用程序 - 如何在后台打开网站并执行操作?

WindowsForms App - How to open a website in the background and do actions?

首先,感谢您看我的问题,这意味着很多。 :D

不久前,我在 Python 中制作了一个 Selenium 脚本,用于登录网站、单击一些按钮并关闭浏览器。 这是非常低效和笨重的,所以我尝试在 C# 的 WindowsForms 应用程序中制作一个更加用户友好的版本。

问题是,我什至不知道如何开始,我在网上寻找尝试做同样事情的人,但我找不到答案。 我如何在 WindowsForms 应用程序的后台创建网站 运行,使用用户名和密码登录,单击网站中的某些按钮,然后关闭网站?

我有“不错”的代码知识,所以如果我有任何错误,请原谅我。

更新 我设法通过将网络浏览器从工具箱中拖入我的应用程序,我现在正在查看 Microsoft 文档以了解如何操作它,有没有办法阻止网络浏览器的用户交互?

我认为您可以在 C# 中使用 selenium。使用 selenium Web 驱动程序,您可以使用您的网站或下载的 HTML 代码在代码中做任何您想做的事情。

有关更多信息,您可以使用 here and also see how to implement and use it here selenium 最常见的用法是在端到端测试中,但您可以随心所欲地进行测试。

这里有一个简单的例子:

    using OpenQA.Selenium;  
using OpenQA.Selenium.Chrome;  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading;  
using System.Threading.Tasks;  
namespace SeleniumTest  
{  
class Program  
{  
static void Main(string[] args)  
{  
Console.Write("test case started ");  
//create the reference for the browser  
IWebDriver driver = new ChromeDriver();  
// navigate to URL  
driver.Navigate().GoToUrl("https://www.google.com/");  
Thread.Sleep(2000);  
// identify the Google search text box  
IWebElement ele = driver.FindElement(By.Name("q"));  
//enter the value in the google search text box  
ele.SendKeys("javatpoint tutorials");  
Thread.Sleep(2000);  
//identify the google search button  
IWebElement ele1 = driver.FindElement(By.Name("btnK"));  
// click on the Google search button  
ele1.Click();  
Thread.Sleep(3000);  
//close the browser  
driver.Close();  
Console.Write("test case ended ");  
}  
}  
}