【发布时间】:2016-10-05 23:26:01
【问题描述】:
我是硒的新手。我正在尝试编写一个脚本来打开一个应用程序,输入用户名和密码并登录。我尝试在 Firefox 中使用 selenium IDE 做同样的事情。它的工作。但是当我使用 selenium 和 TestNG(JAVA) 尝试相同时,我收到以下错误:
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: /html/body/form[1]/div[2]/table[2]/tbody/tr[4]/td/button (tried for 10 second(s) with 500 MILLISECONDS interval)
Build info: version: 'unknown', revision: '3169782', time: '2016-09-29 10:24:50 -0700'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, ie.enableFullPageScreenshot=true, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.forceShellWindowsApi=false, pageLoadStrategy=normal, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:9907/, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss}]
Session ID: a36f7287-5912-48c0-87b2-3d974262c634
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:265)
at com.nagest.nrt.NewTest.userLogin(NewTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
我页面的 HTML 片段是:
<table>
<tr>
<td>User Id: </td>
<td><input type="text" name="USER" id="USER" onfocus="this.style.border='2px solid #3BB9FF';" onblur="this.style.border='2px solid #737373'" /></td>
</tr>
<tr>
<td>Password: </td>
<td><input type="password" name="PASSWORD" id="PASSWORD" onfocus="this.style.border='2px solid #3BB9FF';" onblur="this.style.border='2px solid #737373'" /></td>
</tr>
<tr>
<td><input type="hidden" name="TARGET" id="TARGET" value="/epsweb/view/private/search/search.jsf" /></td>
</tr>
<tr>
<td><button name="Logon" type="submit" onfocus="this.style.border='2px solid #3BB9FF';" onblur="this.style.border='2px solid #737373'">Logon</button></td>
</tr>
</table>
我的 TESTNG java 代码:
public void userLogin(){
driver.get(baseURL + "login url");
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//driver.findElement(By.id("USER")).sendKeys("aaa");
waitVar.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"USER\"]"))).sendKeys("aaa");
waitVar.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"PASSWORD\"]"))).sendKeys("123");;
// driver.findElement(By.name("USER")).sendKeys("aaa");
// driver.findElement(By.name("PASSWORD")).sendKeys("@123");
waitVar.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/form[1]/div[2]/table[2]/tbody/tr[4]/td/button"))).click();
String strPageTitle = driver.getTitle();
}
我尝试使用多种组合,但现在都可以使用。我什至尝试从 firefox IDE 复制代码并将其转换为 java。 XPATH 是从萤火虫那里得到的。
注意:表单元素在 10 秒前可见。
更新:我尝试使用以下代码
WebElement myelement=driver.findElement(By.id("USER"));
String tag=myelement.getTagName();
System.out.println("====================="+tag);// ===============input
myelement.sendKeys("aaaa");
正在打印 sysout 行。但是,当我使用 sendKeys() 方法时,它会抛出异常。
org.openqa.selenium.WebDriverException: Element is not displayed (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 234 milliseconds
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, ie.enableFullPageScreenshot=true, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.forceShellWindowsApi=false, pageLoadStrategy=normal, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:13799/, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss}]
Session ID: 1c79492b-0fab-497e-9866-cbba060f6c8a
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:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:636)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:284)
...........
此问题仅在 IE11 中出现。我在 chrome 中尝试过它可以正常工作。
【问题讨论】:
-
对于登录按钮,请尝试直接 xpath --- "//button[@name='Logon']" 并检查它是否有效...假设您只有一个具有该名称的按钮或它是页面中的第一个
-
@Grasshopper:更新了代码。请立即查看
-
更新后的代码出现什么异常?
-
@Grasshopper:如果我使用 driver.findElement,我不会收到任何错误。但是使用 sendKeys() 会抛出异常