Reputation: 803
The below code is fetching the emailId and password value from the source file and printing it on the console, but only pasting the emailId value in the web page, but not the password. I am getting an error message as
FAILED: doLogin("[email protected]", "password1") org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
This is my code :
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 {
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
XSSFRow row = null;
XSSFCell cell = null;
WebDriver driver = null;
@Test(dataProvider = "getData")
public void doLogin(String username, String password)
{
System.setProperty("webdriver.chrome.driver","C://testing/chromedriver_win32/chromedriver.exe" );
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://login.yahoo.com/config/login?.src=fpctx&.intl=in&.lang=en-IN&.done=https://in.yahoo.com/%3fp=us");
driver.findElement(By.xpath("//input[@id='login-username']")).sendKeys(username);
driver.findElement(By.xpath("//*[@id='login-signin']")).click();
driver.findElement(By.xpath("//*[@id='login-passwd']")).sendKeys(password);
}
@DataProvider
public Object[][] getData() throws IOException
{
FileInputStream fis = new FileInputStream("C://Users/Gaurav/Documents/testid.xlsx");
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheet("sheet1");
int rowCount = sheet.getFirstRowNum()+sheet.getLastRowNum()+1;
int colCount = sheet.getRow(0).getLastCellNum();
System.out.println("Row count is:" +rowCount+ "Col count is:" +colCount);
Object[][] data = new Object[rowCount-1][colCount];
for(int rNum = 2; rNum<=rowCount; rNum++)
for(int cNum = 0; cNum<colCount; cNum++)
{
System.out.println(getCellData("sheet1",cNum,rNum));
data[rNum-2][cNum]=getCellData("sheet1",cNum,rNum);
}
return data;
}
public String getCellData(String sheetName, int colNum, int rowNum)
{
try{
if(rowNum<=0)
return "";
int index = workbook.getSheetIndex(sheetName);
if(index == -1)
return "";
sheet =workbook.getSheetAt(index);
row = sheet.getRow(rowNum-1);
if(row==null)
return "";
cell = row.getCell(colNum);
if(cell==null)
return "";
else if(cell.getCellTypeEnum()==CellType.STRING)
return cell.getStringCellValue();
else if(cell.getCellTypeEnum()==CellType.NUMERIC||cell.getCellTypeEnum()==CellType.FORMULA)
{String CellText = String.valueOf(cell.getNumericCellValue());
return CellText;}
else if(cell.getCellTypeEnum()==CellType.BLANK)
return "";
else return String.valueOf(cell.getBooleanCellValue());
}
catch(Exception e)
{
e.printStackTrace();
return "row"+rowNum+"col"+colNum+"Does not exist";
}
}
}
Upvotes: 0
Views: 80
Reputation: 967
Add Explicit wait before entering password.
Try like below.
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");
It was working in my machine.Let me know if you have any queries
Upvotes: 1