【问题标题】:Form elements not identifiable in TestNG在 TestNG 中无法识别的表单元素
【发布时间】: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() 会抛出异常

标签: java selenium testng


【解决方案1】:

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

【讨论】:

  • :此问题仅在 IE11 中出现。我在 chrome 中尝试过它可以正常工作l
【解决方案2】:

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

【讨论】:

  • 我也尝试过使用 id 选择器。它也会引发同样的错误。但是 chrome 中的相同代码可以完美运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多