若是要作百度文庫,百度貼吧,百度知道簽到,你,會怎麼作?前不久我還會以爲這好像太麻煩了,now,soeasy。html
Selenium是一個用於Web應用程序測試的工具。Selenium測試直接運行在瀏覽器中,就像真正的用戶在操做同樣。支持的瀏覽器包括 ,Chrome,Firefox,IE,PhantomJS(一個基於webkit內核的無界面瀏覽器)等。git
Selenium.WebDriver 3.5.1
Selenium.Support 3.5.1
Selenium.PhantomJS.WebDriver
//無頭瀏覽器 無界面Selenium.Chrome.WebDriver
//須要下載瀏覽器 ,運行時會打開瀏覽器Selenium.Firefox.WebDriver
//須要下載瀏覽器var driver = new PhantomJSDriver();//建立瀏覽器 driver.Navigate().GoToUrl("http://www.baidu.com");//打開百度 //截圖保存 Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot(); screenshot.SaveAsFile("baidu.jpg", ScreenshotImageFormat.Jpeg); //退出 driver.Quit();
var driver = new PhantomJSDriver();//建立瀏覽器 driver.Navigate().GoToUrl("http://www.baidu.com");//打開百度 driver.FindElement(By.Id("kw")).SendKeys("Selenium"); driver.FindElement(By.Id("su")).Click(); Thread.Sleep(3000);//搜索結果顯示出來再接圖 //截圖保存 Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot(); screenshot.SaveAsFile("baidu.jpg", ScreenshotImageFormat.Jpeg); //退出 driver.Quit();
源碼地址:https://coding.net/u/yimocoding/p/WeDemo/git/tree/SeleniumDemo/SeleniumDemogithub
要是用模擬請求的方式得累死我的。web
static void 百度簽到() { dynamic loginUser = new { UserName = "yimo", UserPwd = "123456" }; var myDriver = new PhantomJSDriver();//建立瀏覽器 var defTask = new List<string>() { "文庫", "貼吧", "知道" }; const string _loginUrl = "https://passport.baidu.com/v2/?login"; //跳轉頁面 myDriver.Navigate().GoToUrl(_loginUrl); myDriver.FindElement(By.Id("TANGRAM__PSP_3__userName")).SendKeys(loginUser.UserName); myDriver.FindElement(By.Id("TANGRAM__PSP_3__password")).SendKeys(loginUser.UserPwd); myDriver.FindElement(By.Id("TANGRAM__PSP_3__submit")).Click(); Console.WriteLine(myDriver.Title); if (defTask.Contains("文庫")) { const string _wkSignUrl = "https://wenku.baidu.com/task/browse/daily"; Console.WriteLine($"開始百度文庫簽到"); myDriver.Navigate().GoToUrl(_wkSignUrl); myDriver.ExecScript("$('.bg').remove();$('.g-btn-pass').click();"); Console.WriteLine("百度文庫簽到完成"); Thread.Sleep(1000); myDriver.SaveImg("文庫簽到.png"); } if (defTask.Contains("貼吧")) { const string _tiebaUrl = "https://tieba.baidu.com/index.html"; Console.WriteLine("開始百度貼吧簽到"); myDriver.Navigate().GoToUrl(_tiebaUrl); myDriver.FindElement(By.ClassName("onekey_btn")).Click(); myDriver.FindElement(By.ClassName("sign_btn_nonmember")).Click(); Console.WriteLine("百度貼吧簽到完成"); Thread.Sleep(1000); myDriver.SaveImg("貼吧簽到.png"); } if (defTask.Contains("知道")) { const string _tiebaUrl = "https://zhidao.baidu.com/"; Console.WriteLine("開始百度知道簽到"); myDriver.Navigate().GoToUrl(_tiebaUrl); myDriver.FindElement(By.ClassName("go-sign-in")).Click(); myDriver.FindElement(By.ClassName("sign-in-btn")).Click(); Console.WriteLine("百度知道簽到完成"); Thread.Sleep(1000); myDriver.SaveImg("知道簽到.png"); } //退出 myDriver.Quit(); }
static void 淘金幣領取() { dynamic loginUser = new { UserName = "yimo", UserPwd = "123456" }; var myDriver = new PhantomJSDriver();//建立瀏覽器 const string _loginUrl = "https://login.taobao.com/member/login.jhtml"; const string _taoUrl = "https://taojinbi.taobao.com/index.htm"; //跳轉頁面 myDriver.Navigate().GoToUrl(_loginUrl); //帳號登陸 myDriver.FindElement(By.Id("J_Quick2Static")).Click(); myDriver.FindElement(By.Id("TPL_username_1")).SendKeys(loginUser.UserName); myDriver.FindElement(By.Id("TPL_password_1")).SendKeys(loginUser.UserPwd); myDriver.FindElement(By.Id("J_SubmitStatic")).Click(); myDriver.Navigate().GoToUrl(_taoUrl); myDriver.FindElement(By.ClassName("J_GoTodayBtn")).Click(); //滑動驗證碼 Actions action = new Actions(driver); var source = driver.FindElement(By.ClassName("btn_slide")); if (source == null) { return; } action.ClickAndHold(source).MoveByOffset(300,0); action.MoveToElement(source).Release(); IAction actions = action.Build(); actions.Perform(); }
Ctrl+F5,截圖保存成功
api
var driver=new PhantomJSDriver();//驅動實例
瀏覽器
INavigation
接口: 瀏覽器導航接口(前進,後退,刷新),接口實例:driver.Navigate()
IOptions
接口: 瀏覽器信息接口(Cookie,設置窗口等操做),接口實例:driver.Manage()
IWebElement
經過driver.FindElement()
篩選獲取到元素信息(Text等),能夠對元素賦值(SendKeys())單擊(Click())等操做driver.Manage().Window.Maximize();
driver.Manage().Cookies
((IJavaScriptExecutor)driver).ExecuteScript("document.body.innerHTML='Selenium'");
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile("保存路徑.png", ScreenshotImageFormat.Png);
var driver = new PhantomJSDriver(); By by=By.ClassName("btn_slide"); int x=300,y=0 Actions action = new Actions(driver); var source = driver.FindElement(by); if (source == null) { return; } action.ClickAndHold(source).MoveByOffset(x, y); action.MoveToElement(source).Release(); IAction actions = action.Build(); actions.Perform();
下一篇:SeleniumNetCoreDemo+travis-ci+Github
Demo下載:https://coding.net/u/yimocoding/p/WeDemo/git/tree/SeleniumDemo
clone命令:git clone https://git.coding.net/yimocoding/WeDemo.git -b SeleniumDemo
ide