表格元素在TestNG中无法识别

时间:2016-10-05 13:24:36

标签: java selenium testng

我是硒的新手。我正在尝试编写一个脚本来打开一个应用程序,输入用户名和密码并登录。我尝试在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它可以正常工作。

2 个答案:

答案 0 :(得分:0)

我认为,你需要增加等待时间。从您的堆栈跟踪,您似乎使用10秒作为等待时间。在这么短的时间内,登录按钮不可见。增加等待时间可以解决您的问题。

答案 1 :(得分:0)

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