从其他终端运行时找不到元素

时间:2017-07-14 11:46:25

标签: java selenium jxl

尝试在其他系统上运行我的可执行jar,打开URL,打印标题但无法找到该元素。给出以下错误。我无法理解我哪里出错了。需要大家的帮助。感谢。

C:\Users\temp.BWT05574003\Documents\Sel Test Result>java -jar UserAccess_V02.jar
Enter User EIN of System ==> BWT05574003

611066167 - Login ID for T3
Started InternetExplorerDriver server (32-bit)
3.4.0.0
Listening on port 29701
Only local connections are allowed
Jul 14, 2017 11:53:45 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Page Title -- 21C Authentication: Login
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to find element with name == USER (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 30.04 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/
no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'BWT05574003', ip: '10.52.132.19', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{se:ieOptions={browserAttachTimeout=0.0,ie.enableFullPageScreenshot=true, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.forceShellWindowsApi=false, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000.0, ie.useLegacyFileUploadDialogHandling=false, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0.0, ie.browserCommandLineSwitches=, requireWindowFocus=false, initialBrowserUrl=http://localhost:29701/, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true}, browserName=internet explorer, pageLoadStrategy=normal, javascriptEnabled=true, version=11, platform=WINDOWS, unexpectedAlertBehaviour=dismiss}]
Session ID: 5d86998b-5b43-49f8-884f-e4a21c457a5d
*** Element info: {Using=name, value=USER}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:485)
    at org.openqa.selenium.By$ByName.findElement(By.java:303)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
    at UserAccess.main(UserAccess.java:95)
    ... 5 more

我的代码

public static void main(String args[]) throws BiffException, IOException{

sc = new Scanner(System.in);
System.out.print("Enter User EIN of System ==>  ");
EIN = sc.next();
System.out.println();

FilePath = "C:\\Users\\" + EIN + "\\Documents\\Sel Test Result\\UserAccess.xls";
fs = new FileInputStream(FilePath);
wb = Workbook.getWorkbook(fs);
sh = wb.getSheet("Sheet1");

int totalNoOfRows = sh.getRows();
EIN1 = readExcel(1,0);
Pwd = readExcel(1,1);

System.out.println(EIN1+" - Login ID for T3");

try
{
  Runtime.getRuntime().exec("taskkill /F /IM IEDriverServer.exe");
}
catch (IOException e)
{
  e.printStackTrace();
}

System.setProperty("webdriver.ie.driver", "C:\\Users\\" + EIN + "\\Documents\\Sel Test Result\\IEDriverServer\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
baseUrl = "http://bwt05574004.itsm.nat.bt.com:8080/arsys/home";

//baseUrl = "http://itsm.nat.bt.com:8080/arsys";

projectPath = "C:\\Users\\" + EIN + "\\Documents\\Sel Test Result";

driver.navigate().to(baseUrl);
driver.manage().timeouts().implicitlyWait(30L, TimeUnit.SECONDS);

System.out.println("Page Title -- " + driver.getTitle()); // Till Here Working

WaitFor(5000);

WebElement myElement = driver.findElement(By.name("USER"));
myElement.sendKeys(new CharSequence[] { EIN1 });
WebElement myElement1 = driver.findElement(By.name("PASSWORD"));
myElement1.sendKeys(new CharSequence[] { Pwd });code here

HTML代码

<table width="100%" cellspacing="0" cellpadding="5">
  <tbody>
    <tr>
      <th>21C Authentication: Login</th>
    </tr>
    <script>
      errorMsg("true");
    </script>
    <tr>
      <td align="center">
    </tr>
    <tr>
      <td align="left">
        <form class="form" autocomplete="off" method="post" name="Login">
          <span class="label">
          <br>
          <input name="USER" maxlength="70" tabindex="1" accesskey="U">
          <br>
          <span class="label">
            <u>P</u>
            assword:
          </span>
          <br>
          <input type="password" name="PASSWORD" maxlength="70" tabindex="2" accesskey="P">
          <a href="javascript:openPopUpWindow('http://passwords.intra.bt.com/ewebpass/input_form.cfm?preprocessed=SITEMINDER',670,530,true)" tabindex="5" title="Reset your password with eWebPass">Forgotten your password?</a>

1 个答案:

答案 0 :(得分:0)

尝试使用WebDriverWait:

,而不是硬编码的5秒等待
WebDriverWait wait = new WebDriverWait(driver,20);
WebElement userField = wait.until(ExpectedConditions.elementToBeClickable(By.name("USER")));

页面完全可能在不同的计算机上加载速度较慢。