C#使用seleium实现一个自动登录器

1.http://docs.seleniumhq.org/ 下载seleium包

2.新建一个C#项目,比如控制台,引用seleium包中的dll

using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System.IO;

namespace WebLogin
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = Path.Combine(Environment.CurrentDirectory);
            IWebDriver driver = new ChromeDriver(path);            
            driver.Navigate().GoToUrl("http://www.***.com");

            var txtusername =driver.FindElement(By.Id("username"));
            txtusername.SendKeys("****");
            var txtpassword = driver.FindElement(By.Id("password"));
            txtpassword.SendKeys("*****");

            var submit = driver.FindElement(By.Name("Submit"));
            submit.Click();
        }
    }
}

亲测Chrome下的seleium没有问题,但是IE的话现在对IE11的版本支持还不太合适,我自己就遇到了模拟点击按钮时有时没有反应。

posted @ 2014-05-30 13:39  erictanghu  阅读(644)  评论(0编辑  收藏  举报