代码未粘贴WebPage的密码字段中的密码值

时间:2017-04-05 16:38:37

标签: java selenium selenium-webdriver

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

  

失败:doLogin(" gaurav_thantry@yahoo.com"," password1")   org.openqa.selenium.StaleElementReferenceException:陈旧元素   reference:元素未附加到页面文档

这是我的代码:



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

}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

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

尝试如下。

   PLATFORM VERSION INFO
    Windows             : 6.1.7601.65536 (Win32NT)
    Common Language Runtime     : 4.0.30319.42000
    System.Deployment.dll       : 4.6.1590.0 built by: NETFXREL2
    clr.dll             : 4.6.1590.0 built by: NETFXREL2
    dfdll.dll           : 4.6.1590.0 built by: NETFXREL2
    dfshim.dll          : 4.0.41209.0 (Main.041209-0000)

SOURCES
    Deployment url          : file:///C:/Users/Coppy/Documents/visual%20studio%202015/Projects/Game2/Game2/publish/Frogger.application

IDENTITIES
    Deployment Identity     : Frogger.application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1c233c28f0ad663b, processorArchitecture=x86

APPLICATION SUMMARY
    * Installable application.

ERROR SUMMARY
    Below is a summary of the errors, details of these errors are listed later in the log.
    * Activation of C:\Users\Coppy\Documents\visual studio 2015\Projects\Game2\Game2\publish\Frogger.application resulted in exception. Following failure messages were detected:
        + Exception reading manifest from file:///C:/Users/Coppy/Documents/visual%20studio%202015/Projects/Game2/Game2/publish/Application%20Files/Frogger_1_0_0_0/Frogger.exe.manifest: the manifest may not be valid or the file could not be opened.
        + Parsing and DOM creation of the manifest resulted in error. Following parsing errors were noticed: 
            -HRESULT:   0x8007001f
             Start line:    0
             Start column:  0
             Host file:     
        + Ena od naprav, priključenih na sistem, ne deluje. (Exception from HRESULT: 0x8007001F)

COMPONENT STORE TRANSACTION FAILURE SUMMARY
    No transaction error was detected.

WARNINGS
    There were no warnings during this operation.

OPERATION PROGRESS STATUS
    * [5.4.2017 21:48:49] : Activation of C:\Users\Coppy\Documents\visual studio 2015\Projects\Game2\Game2\publish\Frogger.application has started.
    * [5.4.2017 21:48:49] : Processing of deployment manifest has successfully completed.
    * [5.4.2017 21:48:49] : Installation of the application has started.

ERROR DETAILS
    Following errors were detected during this operation.
    * [5.4.2017 21:48:49] System.Deployment.Application.InvalidDeploymentException (ManifestParse)
        - Exception reading manifest from file:///C:/Users/Coppy/Documents/visual%20studio%202015/Projects/Game2/Game2/publish/Application%20Files/Frogger_1_0_0_0/Frogger.exe.manifest: the manifest may not be valid or the file could not be opened.
        - Source: System.Deployment
        - Stack trace:
            at System.Deployment.Application.ManifestReader.FromDocument(String localPath, ManifestType manifestType, Uri sourceUri)
            at System.Deployment.Application.DownloadManager.DownloadApplicationManifest(AssemblyManifest deploymentManifest, String targetDir, Uri deploymentUri, IDownloadNotification notification, DownloadOptions options, Uri& appSourceUri, String& appManifestPath)
            at System.Deployment.Application.ApplicationActivator.DownloadApplication(SubscriptionState subState, ActivationDescription actDesc, Int64 transactionId, TempDirectory& downloadTemp)
            at System.Deployment.Application.ApplicationActivator.InstallApplication(SubscriptionState& subState, ActivationDescription actDesc)
            at System.Deployment.Application.ApplicationActivator.PerformDeploymentActivation(Uri activationUri, Boolean isShortcut, String textualSubId, String deploymentProviderUrlFromExtension, BrowserSettings browserSettings, String& errorPageUrl)
            at System.Deployment.Application.ApplicationActivator.ActivateDeploymentWorker(Object state)
        --- Inner Exception ---
        System.Deployment.Application.InvalidDeploymentException (ManifestParse)
        - Parsing and DOM creation of the manifest resulted in error. Following parsing errors were noticed: 
            -HRESULT:   0x8007001f
             Start line:    0
             Start column:  0
             Host file:     
        - Source: System.Deployment
        - Stack trace:
            at System.Deployment.Application.Manifest.AssemblyManifest.LoadCMSFromStream(Stream stream)
            at System.Deployment.Application.Manifest.AssemblyManifest..ctor(FileStream fileStream)
            at System.Deployment.Application.ManifestReader.FromDocument(String localPath, ManifestType manifestType, Uri sourceUri)
        --- Inner Exception ---
        System.Runtime.InteropServices.COMException
        - Ena od naprav, priključenih na sistem, ne deluje. (Exception from HRESULT: 0x8007001F)
        - Source: System.Deployment
        - Stack trace:
            at System.Deployment.Internal.Isolation.IsolationInterop.CreateCMSFromXml(Byte[] buffer, UInt32 bufferSize, IManifestParseErrorCallback Callback, Guid& riid)
            at System.Deployment.Application.Manifest.AssemblyManifest.LoadCMSFromStream(Stream stream)

COMPONENT STORE TRANSACTION DETAILS
    No transaction information is available.

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