gpt4 book ai didi

java - 代码未将密码值粘贴到网页的密码字段中

转载 作者:行者123 更新时间:2023-12-02 13:29:49 25 4
gpt4 key购买 nike

下面的代码从源文件中获取emailId和密码值并将其打印在控制台上,但只将emailId值粘贴到网页中,而不粘贴密码。我收到一条错误消息,如下所示

FAILED: doLogin("gaurav_thantry@yahoo.com", "password1") org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

这是我的代码:

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";
}


}


}

最佳答案

在输入密码之前添加显式等待。

尝试如下。

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");

它在我的机器上运行。如果您有任何疑问,请告诉我

关于java - 代码未将密码值粘贴到网页的密码字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43236940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com