51Testing软件测试论坛

标题: selenium 跳转问题 [打印本页]

作者: honger08240127    时间: 2012-10-22 17:26
标题: selenium 跳转问题
要实现一个网站(www.koudai.com)的淘宝登陆,目前想实现的逻辑是  鼠标移登陆 点击淘宝登陆  跳转到淘宝登陆页  输入账号 密码  再授权,登陆成功    目前只考虑第一次登陆(即每次都要输入密码)

首页KoudaiIndexPage 类入下:

public class KoudaiIndexPage {

private WebDriver driver;
private final String url="http://www.koudai.com/";
public KoudaiIndexPage(WebDriver driver){
  this.driver=driver;
  driver.get(url);
  
}

//实现鼠标移并跳转到登陆页面
public WebDriver openLoginPage(WebDriver driver){
  
  Actions a= new Actions(driver) ;
  a.moveToElement(driver.findElement(By.xpath("//div[@id='J_cornerLogin' and @class='float-left']/span[1][@class='float-left pointer']"))).perform();
  driver.findElement(By.className("corner-login-taobao")).click();
      //driver.switchTo().defaultContent();
  System.out.println("pageSource>>"+driver.getPageSource());
  return driver;

  
}

//授权
public AuthorizeLogin authorize(WebDriver driver) throws Exception{
  this.login(driver, "honger08240129","a123456");
  driver.switchTo().defaultContent();
  driver.findElement(By.id("sub")).click();
  return new AuthorizeLogin(driver);
  
   
  
}
//登陆
public TaoBaoLogin login(WebDriver driver,String username,String passwd) throws Exception{
  this.openLoginPage(driver);
WebDriverWait wait=new WebDriverWait(driver,10);
                wait.until(new ExpectedCondition<WebElement>(){
                       
                        @Override
                        public WebElement apply(WebDriver dr) {
                                return dr.findElement(By.id("TPL_username_1"));
                }}).clear();
  System.out.println("pageSource>>"+driver.getPageSource());
//  driver.findElement(By.id("TPL_username_1")).clear();
  driver.findElement(By.id("TPL_username_1")).sendKeys("honger08240129");
  driver.findElement(By.id("TPL_password_1")).clear();
  driver.findElement(By.id("TPL_password_1")).sendKeys("a123456");
  driver.findElement(By.id("J_SubmitStatic")).click();
  return new TaoBaoLogin(driver);
  
}


TaoBaoLogin 类如下:
public class TaoBaoLogin {
      private WebDriver driver;
      public TaoBaoLogin(WebDriver driver){
      this.driver=driver;
      
      }
      public String getTitle(){
       return driver.getTitle();
      }
      public String getContent(){
       return driver.getPageSource();
      }
}



AuthorizeLogin 类如下:
public class AuthorizeLogin {
private WebDriver driver;
public  AuthorizeLogin(WebDriver driver){
  this.driver=driver;
}
public String getTitle(){
  return driver.getTitle();
}
public String getContext(){
  return driver.getPageSource();
}
}



目前能跳转到淘宝登陆页  但是在运行到openLoginPage(WebDriver driver)这个方法提示:org.openqa.selenium.WebDriverException: Unable to get browser (WARNING: The server did not provide any stacktrace information)
作者: yazi0127    时间: 2012-12-27 09:30
在“openLoginPage”使用selectWindow方法试试。




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2