虽然在WebPage中可见,但无法找到元素

时间:2015-07-02 10:39:34

标签: java html selenium-webdriver

我正在使用Java进行Selenium Webdriver自动化。

请查看网页http://1drv.ms/1CMtRxd的附加HTML摘录,并找到相同http://1drv.ms/1JzXarv的用户界面。

从html中,了解所有UI元素都是在表中构建的,并且此处确定的问题是,无法找到右侧表中存在的元素。

但是,在自动化脚本中查找和使用左侧表中的元素没有问题。

你能告诉我们这两张桌子之间有什么区别,以及为什么我在播放时无法找到右手边桌子的元素。

另外,请有人帮我解决这个问题。

请找到代码提取:

(使用网页对象模型编码)

在POM课程中:

@Test(description = "Test to Track the Order")
    public void searchOrder() throws Exception
    {
        SearchRequest searchReq = PageFactory.initElements(wd, SearchRequest.class);


        wd.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        searchReq.enterTrackingID("161471");
        searchReq.clickSearchButton();

    }

在页面工厂类中:

public class SearchRequest
{

    @FindBy(how = How.ID, using = "cpportal:searchByID")
    public WebElement   trackingID;

    public void enterTrackingID(String trackingID)
    {
        this.trackingID.clear();
        this.trackingID.sendKeys(trackingID);
    }

    @FindBy(how = How.NAME, using = " cpportal:j_idt56 ")
    public WebElement   search;

    public void clickSearchButton()
    {
        this.search.click();
    }
}

异常抛出:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"cpportal:searchByID"}
Command duration or timeout: 10.12 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-27 00:00:10'
System info: host: 'inl-279930-1', ip: '10.13.136.53', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'
*** Element info: {Using=id, value=cpportal:searchByID}
Session ID: 3c404b65-ba64-435d-8395-53c262eff602
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=37.0.2}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:393)
    at org.openqa.selenium.By$ById.findElement(By.java:214)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:59)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:37)
    at com.sun.proxy.$Proxy8.clear(Unknown Source)
    at com.cgi.hqn.liveref.pageobjects.SearchRequest.enterTrackingID(SearchRequest.java:17)
    at com.cgi.hqn.liveref.test.CreateSOSL.searchOrder(CreateSOSL.java:176)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"cpportal:searchByID"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-27 00:00:10'
System info: host: 'inl-279930-1', ip: '10.13.136.53', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/SUBRAM~1/AppData/Local/Temp/anonymous5667416437314785176webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10271)
    at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/SUBRAM~1/AppData/Local/Temp/anonymous5667416437314785176webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:603)

1 个答案:

答案 0 :(得分:1)

这可能是一个ajax元素。一段时间后元素可见。

- :

的返回值是多少
trackingID.isDisplayed();

Implicit wait

可以避免这种情况
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

或通过WebDriver等待

new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(trackingID));

希望有所帮助