我对硒很陌生。我试图编写一个脚本来打开一个应用程序,输入用户名和密码并登录。我尝试在firefox中使用selenium进行同样的操作。起作用了。但是,当我使用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复制代码并将其转换为java。XPATH正在从firebug中获取信息。
注意:表单元素在10秒前就可以看到。
更新:我尝试使用以下代码
WebElement myelement=driver.findElement(By.id("USER"));
String tag=myelement.getTagName();
System.out.println("====================="+tag);// ===============input
myelement.sendKeys("aaaa");
系统线正在打印。但是,当我使用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中。我试过用铬,它工作正常。
发布于 2016-10-05 05:39:27
我想,你需要增加等待时间。从您的堆栈跟踪来看,您似乎使用了10秒作为等待时间。在这么短的时间内,无法看到登录按钮。增加等候时间可以解决你的问题。
发布于 2016-10-05 07:26:01
@Nageswaranm,使用webdriver并使用id、name或cssSelector查找元素。在使用xpath查找webelement方面,IE不如其他浏览器好。它在IE中很慢。还增加了webdriver等待到30秒。希望这能解决问题。
https://stackoverflow.com/questions/39875224
复制