2017-04-05 66 views
0

以下代碼從源文件中獲取emailId和密碼值並將其打印在控制檯上,但只粘貼網頁中的emailId值,但不是密碼。 我得到的錯誤消息作爲代碼沒有在WebPage的密碼字段中粘貼密碼值

FAILED:doLogin( 「[email protected]」, 「密碼1」) org.openqa.selenium.StaleElementReferenceException:陳舊元件 參考:元件不連接到頁面文件

這是我的代碼:

package excelSelenium; 
 

 
import java.io.FileInputStream; 
 
import java.io.FileNotFoundException; 
 
import java.io.IOException; 
 
import java.util.concurrent.TimeUnit; 
 

 
import org.apache.poi.ss.usermodel.CellType; 
 
import org.apache.poi.xssf.usermodel.XSSFCell; 
 
import org.apache.poi.xssf.usermodel.XSSFRow; 
 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
 
import org.openqa.selenium.By; 
 
import org.openqa.selenium.WebDriver; 
 
import org.openqa.selenium.chrome.ChromeDriver; 
 
import org.testng.annotations.DataProvider; 
 
import org.testng.annotations.Test; 
 

 
public class seleniumIntg { 
 
\t XSSFWorkbook workbook = null; 
 
\t XSSFSheet sheet = null; 
 
\t XSSFRow row = null; 
 
\t XSSFCell cell = null; 
 
\t WebDriver driver = null; 
 
\t 
 
\t 
 

 
\t @Test(dataProvider = "getData") 
 
\t public void doLogin(String username, String password) 
 
\t { 
 
\t \t System.setProperty("webdriver.chrome.driver","C://testing/chromedriver_win32/chromedriver.exe"); 
 
\t \t driver = new ChromeDriver(); 
 
\t 
 
\t \t driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
 
\t \t driver.get("https://login.yahoo.com/config/login?.src=fpctx&.intl=in&.lang=en-IN&.done=https://in.yahoo.com/%3fp=us"); 
 
\t \t driver.findElement(By.xpath("//input[@id='login-username']")).sendKeys(username); 
 
\t \t driver.findElement(By.xpath("//*[@id='login-signin']")).click(); 
 
\t \t driver.findElement(By.xpath("//*[@id='login-passwd']")).sendKeys(password); 
 
\t \t 
 
\t } 
 
\t @DataProvider 
 
\t public Object[][] getData() throws IOException 
 
\t { 
 
\t \t FileInputStream fis = new FileInputStream("C://Users/Gaurav/Documents/testid.xlsx"); 
 
\t \t workbook = new XSSFWorkbook(fis); 
 
\t \t sheet = workbook.getSheet("sheet1"); 
 
\t \t int rowCount = sheet.getFirstRowNum()+sheet.getLastRowNum()+1; 
 
\t \t int colCount = sheet.getRow(0).getLastCellNum(); 
 
\t \t System.out.println("Row count is:" +rowCount+ "Col count is:" +colCount); 
 
\t \t Object[][] data = new Object[rowCount-1][colCount]; 
 
\t \t for(int rNum = 2; rNum<=rowCount; rNum++) 
 
\t \t \t for(int cNum = 0; cNum<colCount; cNum++) 
 
\t \t \t { 
 
\t \t \t \t System.out.println(getCellData("sheet1",cNum,rNum)); 
 
\t \t \t \t data[rNum-2][cNum]=getCellData("sheet1",cNum,rNum); 
 
\t \t \t \t 
 
\t \t \t } 
 
\t \t return data; \t 
 
\t \t 
 
\t } 
 
\t 
 
\t 
 
\t 
 
\t public String getCellData(String sheetName, int colNum, int rowNum) 
 
\t { 
 
\t \t try{ 
 
\t \t if(rowNum<=0) 
 
\t \t \t return ""; 
 
\t \t int index = workbook.getSheetIndex(sheetName); 
 
\t \t if(index == -1) 
 
\t \t \t return ""; 
 
\t \t sheet =workbook.getSheetAt(index); 
 
\t \t row = sheet.getRow(rowNum-1); 
 
\t \t if(row==null) 
 
\t \t \t return ""; 
 
\t \t cell = row.getCell(colNum); 
 
\t \t if(cell==null) 
 
\t \t \t return ""; 
 
\t \t else if(cell.getCellTypeEnum()==CellType.STRING) 
 
\t \t \t return cell.getStringCellValue(); 
 
\t \t 
 
\t \t else if(cell.getCellTypeEnum()==CellType.NUMERIC||cell.getCellTypeEnum()==CellType.FORMULA) 
 
\t \t \t {String CellText = String.valueOf(cell.getNumericCellValue()); 
 
\t \t return CellText;} 
 
\t \t else if(cell.getCellTypeEnum()==CellType.BLANK) 
 
\t \t \t return ""; 
 
\t \t else return String.valueOf(cell.getBooleanCellValue()); 
 
\t } 
 
\t \t catch(Exception e) 
 
\t \t { 
 
\t \t \t e.printStackTrace(); 
 
\t \t \t return "row"+rowNum+"col"+colNum+"Does not exist"; 
 
\t \t } 
 
\t \t \t 
 
\t \t 
 
\t } 
 
\t 
 

 
}

回答

1

在輸入密碼前添加顯式等待。

請嘗試如下。

WebDriver driver = new ChromeDriver(); 

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 

driver.get("https://login.yahoo.com/"); 

driver.findElement(By.xpath("//input[@id='login-username']")).sendKeys("Username"); 
driver.findElement(By.xpath("//*[@id='login-signin']")).click(); 
WebDriverWait wait = new WebDriverWait(driver,20); 
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='login-passwd']"))); 
driver.findElement(By.xpath("//*[@id='login-passwd']")).sendKeys("Password"); 

這是我machine.Let工作我知道,如果您有任何疑問

+0

哇。公牛的眼睛。有效!非常感謝Akarsh。 –