C# 通过Selecnuim WebDriver操作非IE浏览器

时间:2023-03-10 07:02:58
C# 通过Selecnuim WebDriver操作非IE浏览器

之前有需求需要操作Chrome中的dom元素,没做过这个,但是网上关于这个方面的资料比较少,所以自己捣腾了几天,在知道.net中有这个玩意。

但是一百度,全是关于java,python的而c#的那是一个少。当然对其中的操作我还有很多不了解,这里我只是提出来c#是可以操作非IE的浏览器的。

C# 通过Selecnuim WebDriver操作非IE浏览器

C# 通过Selecnuim WebDriver操作非IE浏览器

  public override void LoadApplication()
{
#region MyRegion
Process[] processes = Process.GetProcessesByName("chromedriver");
for (int i = ; i < processes.Length; i++)
{
processes[i].Kill();
} //获取所有类为Chrome_WidgetWin_1的信息,如果打开了特定(单)病种质量监测系统,就直接返回
List<WindowInfo> listInfo= WindowOperate.GetAllDesktopWindows("Chrome_WidgetWin_1");
for (int i = ; i < listInfo.Count; i++)
{
if (listInfo[i].szWindowName != null)
{
IntPtr ptr = listInfo[i].hWnd;
if (listInfo[i].szWindowName.Contains("特定(单)病种质量监测系统"))
{
WindowOperate.ShowWindow(ptr, (int)WindowShowStatus.SW_RESTORE);
WindowOperate.SetForegroundWindow(ptr);
WindowOperate.ShowWindow(ptr, (int)WindowShowStatus.SW_SHOWNORMAL);
return;
} }
} string currtentPath = Application.StartupPath + "\\" + "chromedriver.exe";
IWebDriver web = new ChromeDriver();
hideConsole(currtentPath);
web.Navigate().GoToUrl(str_LoginUrl);
var username = web.FindElement(By.Id("UserName"));
var password = web.FindElement(By.Id("Password"));
username.SendKeys(this._loginName);
password.SendKeys(this._password);
var buttonLogin = web.FindElement(By.ClassName("btn"));
buttonLogin.Click();
//测试
if (web.Url == "http://192.168.100.143/Account/Login")
{
var msg = web.FindElement(By.ClassName("vali-summary-phd"));
string strmsg = msg.Text;
if (strmsg == "提供的用户名或密码不正确。")
{
//如果密码错误,重新修改配置密码
frmChangeExternalAppUser frmChangeExternalAppUser = new frmChangeExternalAppUser(this._appId, this._loginName, "单病种系统");
frmChangeExternalAppUser.ShowDialog();
if (frmChangeExternalAppUser.IsChanged)
{
this._loginName = frmChangeExternalAppUser.LoginName;
this._password = frmChangeExternalAppUser.Password;
this.LoadApplication();
}
}
}
#endregion }