原文章 http://blog.csdn.net/aerchi/article/details/46876361 不小心删除了,费了好大劲终于恢复了。
有个需求测试,需要用webdriver 登录到新浪微博,由于个人比较善长 Webdriver,于是采取了Webdriver+FireFox来实现。
配置环境
a. 必须首先在Eclipse里加载 selenium webdriver 包【[此处我导入的是 selenium-server-standalone-2.45.0.jar】
b. 以及安装了 firefox 浏览器。
此篇文章中我用了本人的新浪微博地址来做测试,大家 可以更换为自己的新浪微博帐号和密码。
文中为部分主要的Java 代码.
1.初始化Webdriver的方法
- /**
- * for init the webdriver
- * @return webdriver
- * author: aerchi
- * 2015-07-14
- */
- public static WebDriver initWebDriver()
- {
- WebDriver curDriver=null;
- try{
- if(!browserPath.equals("") && browserPath.toLowerCase().endsWith(".exe"))
- {
- if(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.登录新浪微博
部分代码如下
- /**
- * @param args
- * 登录新浪微博
- * 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的完整路径
- 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/login.php";
- loginPrefixUrl ="http://weibo.com/login?url=";
- //aerchi
- refWeiBoUrl = "http://weibo.com/aerchi";
- refWeiBoAuthorEn ="aerchi";
- loginUrl = loginPrefixUrl + refWeiBoUrl.trim();
- mainWindow.get(loginUrl);
- //登录的用户名和密码
- String userName="aerchi", passWord="******";
- WebElement logDiv = mainWindow.findElement(By.xpath("//*[@class='W_login_form']"));
- // Out.println(logDiv.getText());
- Constant.waitSecond(6);
- //填写登录用户名及点击登录按钮
- /***************************************************************************************************************/
- //*[@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();
- /***************************************************************************************************************/
- //等待10秒钟
- Constant.waitSecond(10);
- //此处登录成功
- {
- //可以做其它事情代码
- }
- //结束采集
- mainWindow.quit();
- Out.sprintln("****************Finish the Test****************");
- // mainWindow.navigate().refresh();
- // Cookie cookie=mainWindow.manage().getCookies();//
- // mainWindow.get(newUrl);
- // Constant.waitSecond(1);
- }
图2: 成功登录后的截图
乐意黎原创,禁止转载