杀人放火厉飞羽,万人敬仰韩天尊。|

WangJunZzz

园龄:9年3个月粉丝:99关注:13

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
         

        }
复制代码

 

本文作者:WangJunZzz

本文链接:https://www.cnblogs.com/WangJunZzz/p/5778619.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   WangJunZzz  阅读(2062)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起