c# 使用Selenium模擬登錄和操作數據的學習記錄


1、添加引用

  Selenium.WebDriver

  Selenium.Chrome.WebDriver

2、執行代碼:

  下面的代碼是找到用戶列表頁,然后實現自動翻頁到最后一頁

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApp15
{
    class Program
    {
        static void Main(string[] args)
        {
            GrabUrlByKeyWord();

        }
        static void GrabUrlByKeyWord()
        {
            ChromeOptions options = new ChromeOptions();
            //創建chrome驅動程序
            IWebDriver webDriver = new ChromeDriver(options);
            //跳至百度
            webDriver.Navigate().GoToUrl("http://localhost:8066/Account/Logon");
            //找到頁面上的搜索框 輸入關鍵字
            webDriver.FindElement(By.Id("Account")).SendKeys("admin1");
            webDriver.FindElement(By.Id("Password")).SendKeys("123456");
            //點擊搜索按鈕
            webDriver.FindElement(By.Id("btnWebUser")).Click();

            webDriver.Url = "http://localhost:8066/BaseInfoManagement/User/List";

            while (true)
            {
                var isHaveNext = DoNext(webDriver);

                if (isHaveNext == false)
                {
                    break;
                }
            }
            webDriver.Close();
        }

        static bool DoNext(IWebDriver webDriver)
        {
            bool result = false;
            System.Threading.Thread.Sleep(2000);
            var nextPageButton = webDriver.FindElement(By.XPath("//a[contains(@title,'下一頁')]"));
            var strClass = nextPageButton.GetAttribute("class");
            if (strClass != "k-link k-pager-nav k-state-disabled")
            {
                nextPageButton.Click();
                result = true;
            }
            System.Threading.Thread.Sleep(2000);

            return result;
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM