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

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

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

  public override void LoadApplication()
       {
           #region MyRegion
           Process[] processes = Process.GetProcessesByName("chromedriver");
           for (int i = 0; i < processes.Length; i++)
           {
               processes[i].Kill();
           }

           //获取所有类为Chrome_WidgetWin_1的信息,如果打开了特定(单)病种质量监测系统,就直接返回
               List<WindowInfo> listInfo=    WindowOperate.GetAllDesktopWindows("Chrome_WidgetWin_1");
               for (int i = 0; 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
         

        }

 

转载于:https://www.cnblogs.com/WJ--NET/p/5778619.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值