Loading

C# Selenium+HtmlAgilityPack 例子

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using System.Threading;
using System.Windows.Forms;

namespace OA_OT1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string baseUrl = "http://192.168.0.1/";
            string url = $"{baseUrl}login.aspx";

            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.AddArguments("headless");//隐藏google
            //IWebDriver webDriver = new ChromeDriver(chromeOptions);
            IWebDriver webDriver = new ChromeDriver();

            webDriver.Navigate().GoToUrl(url);

            webDriver.Manage().Window.Maximize();
            // 根据元素名称清除元素文本
            webDriver.FindElement(By.Name("txtUserNo")).Clear();
            webDriver.FindElement(By.Name("txtPassword")).Clear();

            // 用户名和密码赋值
            webDriver.FindElement(By.Name("txtUserNo")).SendKeys("DL00000");
            webDriver.FindElement(By.Name("txtPassword")).SendKeys("12345678");

            // 点击login按钮
            webDriver.FindElement(By.Name("btnLogin")).Click();

            // 根据超链接文字找到超链接
            IWebElement link = webDriver.FindElement(By.LinkText("XX一览"));
            // 点击超链接
            link.Click();

            // 切换frame
            webDriver = webDriver.SwitchTo().Frame(webDriver.FindElement(By.Id("iframeMain")));
            
            // 声明两个select
            SelectElement selDepartment;
            SelectElement selProject;

            // 根据ID找到第一个select
            IWebElement department = webDriver.FindElement(By.Name("ctl00$main$dropDepartment"));
            selDepartment = new SelectElement(department);
            // 取得select的option个数
            int departCnt = selDepartment.Options.Count;
            // 选择第一个
            selDepartment.SelectByIndex(1);
            Thread.Sleep(2000);

            // 找到第二个select(这个select的内容是根据第一个select选择的内容填充的,即二级联动)
            IWebElement project = webDriver.FindElement(By.Name("ctl00$main$dropItemGroup"));
            selProject = new SelectElement(project);
            // 取得select的option个数
            int projCnt = selProject.Options.Count;

            /*
             * 上面为什么要取得两个select的option个数?
             * 因为在循环里取控件,当页面刷新后,控件已经变了,但是变量还是刷新前的内容,这样会报错
             * 
             * 执行下面的代码,每次都重新取控件,程序可正常执行
             * 
             */

            for (int i = 1; i < 2; i++)
            {
                department = webDriver.FindElement(By.Name("ctl00$main$dropDepartment"));
                selDepartment = new SelectElement(department);

                selDepartment.SelectByIndex(i);
                Thread.Sleep(2000);

                for (int k = 1; k < projCnt; k++) 
                {
                    project = webDriver.FindElement(By.Name("ctl00$main$dropItemGroup"));
                    selProject = new SelectElement(project);

                    selProject.SelectByIndex(k);

                    webDriver.FindElement(By.XPath("//input[@id=\"ctl00_main_btnSelect\"]")).Click();

                    Thread.Sleep(2000);

                    // 下面的代码是用HtmlAgilityPack取得需要的内容
                    var html = webDriver.PageSource;

                    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                    //加载文档对象模型
                    doc.LoadHtml(html);
                    //获取到百度按钮节点  把我们刚刚复制的XPath粘贴上去
                    HtmlAgilityPack.HtmlNode htmlnode = doc.DocumentNode.SelectSingleNode("//input[@id='ctl00_main_btnSelect']");
                    string value = htmlnode.GetAttributeValue("value", "");

                    htmlnode = doc.DocumentNode.SelectSingleNode("//span[@id='ctl00_main_labTotal']");
                    value = htmlnode.InnerText;
                }
            }
        }
    }
}

 

posted @ 2021-11-03 18:05  码农加一  阅读(143)  评论(0编辑  收藏  举报