[乐意黎原创] 使用Selenium webdriver+Firefox浏览器来登录新浪微博

原文章 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的方法

[java]  view plain  copy
 print ?
  1. /** 
  2. * for init the webdriver 
  3. * @return webdriver 
  4. * author: aerchi 
  5. * 2015-07-14 
  6. */  
  7. public static WebDriver initWebDriver()  
  8. {  
  9. WebDriver curDriver=null;  
  10. try{      
  11.     if(!browserPath.equals("") && browserPath.toLowerCase().endsWith(".exe"))  
  12.     {  
  13.     if(browserPath.toLowerCase().contains("firefox"))  
  14.     {  
  15.     closeProcess.killProcess("firefox.exe");  
  16.     System.setProperty("webdriver.firefox.bin",browserPath);  
  17.        FirefoxProfile profile = new FirefoxProfile();  
  18.        profile.setEnableNativeEvents(true);  
  19.        curDriver=new FirefoxDriver();   
  20.     }  
  21.     if(browserPath.toLowerCase().contains("iedriverserver"))  
  22.     {  
  23.     closeProcess.killProcess("iexplore.exe");  
  24.     closeProcess.killProcess("IEDriverServer.exe");  
  25.     System.setProperty("webdriver.ie.driver",browserPath);   
  26.     DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();  
  27.     capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);  
  28.     curDriver = new InternetExplorerDriver(capabilities);      
  29.     }      
  30.     }else  
  31.     {  
  32.     curDriver=new FirefoxDriver();  
  33.     }  
  34. }catch(Exception ex){}  
  35. return curDriver;  
  36. }  


2.登录新浪微博

部分代码如下
[java]  view plain  copy
 print ?
  1. /** 
  2. * @param args 
  3. * 登录新浪微博 
  4. * author: aerchi 
  5. * www.aerchi.com 
  6. * 2015-07-14 
  7. */  
  8. public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.           
  11.          closeProcess.killProcess("firefox.exe");  
  12.          //closeProcess.killProcess("EXCEL.EXE");  
  13.          Constant.waitSecond(1);  
  14.          //Firefox的完整路径  
  15.          browserPath="C:\\Program Files (x86)\\Mozilla Firefox\\firefox32\\firefox.exe";  
  16.            
  17.          mainWindow=initWebDriver();  
  18.          Out.println("Ok");            
  19.            
  20.          mainWindow.navigate().refresh();  
  21.          Constant.waitSecond(3);  
  22.           
  23.          String loginPrefixUrl = "", refWeiBoUrl="", loginUrl = "", refWeiBoAuthorEn ="";  
  24. //       loginPrefixUrl ="http://weibo.com/login.php";  
  25.            
  26.          loginPrefixUrl ="http://weibo.com/login?url=";  
  27.            
  28.          //aerchi  
  29.          refWeiBoUrl = "http://weibo.com/aerchi";  
  30.          refWeiBoAuthorEn ="aerchi";  
  31.           
  32.            
  33.          loginUrl = loginPrefixUrl + refWeiBoUrl.trim();  
  34.          mainWindow.get(loginUrl);  
  35.            
  36.          //登录的用户名和密码  
  37.          String userName="aerchi", passWord="******";  
  38.                    
  39.          WebElement logDiv = mainWindow.findElement(By.xpath("//*[@class='W_login_form']"));           
  40. //       Out.println(logDiv.getText());  
  41.          Constant.waitSecond(6);           
  42.            
  43.            
  44.         //填写登录用户名及点击登录按钮  
  45.          /***************************************************************************************************************/  
  46.         //*[@id="pl_login_form"]/div[5]/div[1]/div/input  
  47. //       mainWindow.findElement(By.xpath("//*[@name='username']")).sendKeys(userName);         
  48.          mainWindow.findElement(By.xpath("//*[@id='pl_login_form']/div[5]/div[1]/div/input")).sendKeys(userName);          
  49.          mainWindow.findElement(By.xpath("//*[@id='pl_login_form']/div[5]/div[2]/div/input")).sendKeys(passWord);          
  50.          mainWindow.findElement(By.xpath("//*[@id='pl_login_form']/div[5]/div[6]/div[1]/a")).click();          
  51.            
  52.          /***************************************************************************************************************/  
  53.          //等待10秒钟  
  54.          Constant.waitSecond(10);  
  55.            
  56.            
  57.          //此处登录成功  
  58.                  {  
  59.                     //可以做其它事情代码  
  60.                  }   
  61.            
  62.            
  63.          //结束采集  
  64.          mainWindow.quit();  
  65.            
  66.          Out.sprintln("****************Finish the Test****************");   
  67.            
  68. //       mainWindow.navigate().refresh();          
  69. //       Cookie cookie=mainWindow.manage().getCookies();//        
  70. //       mainWindow.get(newUrl);  
  71. //       Constant.waitSecond(1);  
  72.     }  


图1: 登录前的截图





图2: 成功登录后的截图




乐意黎原创,禁止转载

本文地址:  http://blog.csdn.net/aerchi/article/details/51966571

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值