首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在TestNG中识别的表单元素

无法在TestNG中识别的表单元素
EN

Stack Overflow用户
提问于 2016-10-05 13:24:36
回答 2查看 65关注 0票数 0

我对硒很陌生。我试图编写一个脚本来打开一个应用程序,输入用户名和密码并登录。我尝试在firefox中使用selenium进行同样的操作。起作用了。但是,当我使用selenium和TestNG(JAVA)进行同样的操作时,我会得到以下错误:

代码语言:javascript
运行
复制
    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片段是:

代码语言:javascript
运行
复制
                            <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代码:

代码语言:javascript
运行
复制
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秒前就可以看到。

更新:我尝试使用以下代码

代码语言:javascript
运行
复制
 WebElement myelement=driver.findElement(By.id("USER"));
String tag=myelement.getTagName();
        System.out.println("====================="+tag);// ===============input
        myelement.sendKeys("aaaa");

系统线正在打印。但是,当我使用sendKeys()方法时,它会抛出异常。

代码语言:javascript
运行
复制
    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中。我试过用铬,它工作正常。

EN

回答 2

Stack Overflow用户

发布于 2016-10-05 13:39:27

我想,你需要增加等待时间。从您的堆栈跟踪来看,您似乎使用了10秒作为等待时间。在这么短的时间内,无法看到登录按钮。增加等候时间可以解决你的问题。

票数 0
EN

Stack Overflow用户

发布于 2016-10-05 15:26:01

@Nageswaranm,使用webdriver并使用id、name或cssSelector查找元素。在使用xpath查找webelement方面,IE不如其他浏览器好。它在IE中很慢。还增加了webdriver等待到30秒。希望这能解决问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39875224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档