Java Selenium webdriver代码未执行;密码字段保持为空
我尝试执行java webdriver代码,登录页面的selenium测试。我从excel中传递了输入,它正确地将值分配给用户名、密码。单击登录按钮后-Java Selenium webdriver代码未执行;密码字段保持为空,java,eclipse,excel,selenium-webdriver,apache-poi,Java,Eclipse,Excel,Selenium Webdriver,Apache Poi,我尝试执行java webdriver代码,登录页面的selenium测试。我从excel中传递了输入,它正确地将值分配给用户名、密码。单击登录按钮后- Till login button submit works perfectly. if once login button clicked.. password field in that page remain empty and login attempt failed i cant sort out the problem. 谢谢您
Till login button submit works perfectly. if once login button clicked.. password field in that page remain empty and login attempt failed i cant sort out the problem.
谢谢您的回复,谢谢
package exceltest;
import java.io.File;
import java.io.FileInputStream;
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.firefox.FirefoxDriver;
public class sample {
public static void main( String[] args) throws Exception {
String [][] data;
data = excelread();
String expectedtitle;
for (int i = 1; i < data.length; i++ )
{
expectedtitle = login(data[i][0],data[i][1]);
System.out.println("page title after login is" + expectedtitle );
if(expectedtitle.equalsIgnoreCase("homepage Title"))
{
System.out.println("PASSED");
}
else{
System.out.println("FAILED");
}
}
}
public static String login(String username,String password) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://localhost/xxx/Default.aspx");
Thread.sleep(1000);
//driver.findElement(By.id("LoginUserName")).clear();
driver.findElement(By.id("LoginUserName")).sendKeys(username);
//driver.findElement(By.id("LoginPassword")).clear();
driver.findElement(By.id("LoginPassword")).sendKeys(password);
driver.findElement(By.id("LoginLoginButton")).click();
Thread.sleep(1000);
String expectedtitle = driver.getTitle();
return expectedtitle;
}
public static String [][] excelread()throws Exception
{
File excel = new File("D:\\Book1.xlsx");
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheet("Sheet1");
int totrow = ws.getLastRowNum()+ 1;
int totcol = ws.getRow(0).getLastCellNum();
String [][] data = new String[totrow][totcol];
for (int i = 0 ; i < totrow ; i++) {
XSSFRow row = ws.getRow(i);
for (int j=0 ; j < totcol ; j++){
XSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[i][j] = value;
System.out.println("The value is " + value);
}
}
return data;
}
public static String cellToString(XSSFCell cell) {
int type;
Object result ;
type = cell.getCellType();
switch (type) {
case 0 :
result = cell.getNumericCellValue();
break;
case 1 :
result = cell.getStringCellValue();
break;
default :
throw new RuntimeException("There are no support for this type of cell");
}
return result.toString();
}
}
您没有使用相同的定位器类“By”来清除密码字段和输入密码。您确定两个定位器都正确吗?您的密码字段为空,因为您没有选择正确的元素。您必须选择html的输入标记 其他评论。您不应该使用thread.sleep使您的测试用例工作,您将有竞争条件。您应该保留对元素的引用。每次调用findElement都是无用的,会使您的测试变慢
WebElement e = driver.findElement(By.id("whatever"));
e.clear();
e.sendKeys(...);
答案就在这里。。。我在java eclipse中使用了webdriver代码,为.net web应用程序自动登录。xxx.aspx.cs页面中给出的默认密码无法执行。。原因:如果页面具有“提交”单击的功能,则会将登录请求发送到服务器并清除密码字段请描述您的问题所在,并添加有关环境的所有相关信息。如果我们无法重现您的问题,我们将无法帮助您@mschenk74,直到登录按钮提交完美工作。如果单击一次登录按钮。。该页面中的密码字段保持为空,并尝试登录failed@mschenk74此外,还需要注意:log4j:WARN找不到记录器org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager的附加程序。log4j:警告请正确初始化log4j系统。但我不认为这是相同的共振手动。。。在这种情况下,页面可能具有提交单击的功能,它将登录请求发送到服务器并清除密码字段。它与log4j无关。请共享登录url。定位器是完全正确的,并且也检索到了值。。您的代码也将保持与以前相同的结果,直到登录按钮submit完美工作。如果单击一次登录按钮。。该页面中的密码字段保持为空,登录尝试失败