There is a need to test, need to use Webdriver login to Sina Weibo, because the individual relatively good long webdriver, so took the webdriver+firefox to achieve.
Configuring the Environment
A. You must first load the Selenium Webdriver package in Eclipse "[I'm importing selenium-server-standalone-2.45.0.jar here]
B. and installed the Firefox browser.
In this article I used my Sina Weibo address to do the test, we can change for their own Sina Weibo account number and password.
This is part of the main Java code.
1. Methods for initializing Webdriver
/*** for init the webdriver* @return webdriver* author:aerchi* 2015-07-14*/public static Webdriver Initwebdriver () {WebDri Ver curdriver=null;try{if (!browserpath.equals (")") && browserpath.tolowercase (). EndsWith (". exe")) {I F (Browserpath.tolowercase (). Contains ("Firefox")) {closeprocess.killprocess ("Firefox.exe"); System.setproperty ("Webdriver.firefox.bin", Browserpath); Firefoxprofile profile = new Firefoxprofile (); Profile.setenablenativeevents (TRUE); Curdriver=new Firefoxdriver (); } if (Browserpath.tolowercase (). Contains ("Iedriverserver")) {closeprocess.killprocess ("iexplore.exe"); Closeprocess.killprocess ("IEDriverServer.exe"); System.setproperty ("Webdriver.ie.driver", Browserpath); Desiredcapabilities capabilities = Desiredcapabilities.internetexplorer (); Capabilities.setcapability (Internetexplorerdriver.introduce_flakiness_by_ignoring_security_domains, true); Curdriver = new Internetexplorerdriver (capabilitIES); }}else {curdriver=new firefoxdriver (); }}catch (Exception ex) {}return curdriver;}
2. Sign in to Sina Weibo section code as follows
/*** @param args* Login Sina Weibo * author:aerchi* www.aerchi.com* 2015-07-14*/public static void Main (string[] args) {//TODO Auto -generated method Stub closeprocess.killprocess ("Firefox.exe"); Closeprocess.killprocess ("EXCEL. EXE "); Constant.waitsecond (1); Firefox's full path browserpath= "C:\\Program Files (x86) \\Mozilla Firefox\\firefox32\\firefox.exe"; Mainwindow=initwebdriver (); Out.println ("OK"); Mainwindow.navigate (). Refresh (); Constant.waitsecond (3); String Loginprefixurl = "", refweibourl= "", loginurl = "", Refweiboauthoren = "";//Loginprefixurl = "Http://weibo.com/logi N.php "; Loginprefixurl = "Http://weibo.com/login?url="; Aerchi Refweibourl = "Http://weibo.com/aerchi"; Refweiboauthoren = "Aerchi"; loginurl = Loginprefixurl + Refweibourl.trim (); Mainwindow.get (loginurl); Login username and password String username= "Aerchi", password= "******"; Webelement Logdiv = mainwindow.findelement (By.xpath ("//*[@class = ' w_login_form ']"); Out.println (Logdiv.gettext ()); CoNstant.waitsecond (6); Fill in the login user name and click the login button/********************************************************************************************** *[@id = "Pl_login_form"]/div[5]/div[1]/div/input//mainwindow.findelement (By.xpath ("//*[@name = ' username ']). SendKeys (username); Mainwindow.findelement (By.xpath ("//*[@id = ' pl_login_form ']/div[5]/div[1]/div/input")). SendKeys (UserName); Mainwindow.findelement (By.xpath ("//*[@id = ' pl_login_form ']/div[5]/div[2]/div/input")). SendKeys (PassWord); Mainwindow.findelement (By.xpath ("//*[@id = ' pl_login_form ']/div[5]/div[6]/div[1]/a"). Click (); /************************************************************************************************************** *///wait 10 seconds Constant.waitsecond (10); Login successful here {//can do other things code}//End acquisition mainwindow.quit (); Out.sprintln ("****************finish the test****************"); Mainwindow.navigate (). Refresh (); Cookie cookie=mainwindow.manage (). getcookies();////mainwindow.get (Newurl);//Constant.waitsecond (1);}
Figure 1: Pre-logon
Figure 2: After a successful login
Happy to be original, forbidden reprint
This address: http://blog.csdn.net/aerchi/article/details/46876361
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Happy to be original] use selenium webdriver+firefox browser to login Sina Weibo