【问题标题】:Unable to locate element though it is visible in WebPage尽管在网页中可见,但无法定位元素
【发布时间】:2015-07-03 17:17:37
【问题描述】:

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

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

从 html 中了解,所有 UI 元素都是在 Table 中构建的,这里发现的问题是,无法找到右侧 table 中存在的元素。

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

您能否告知这两个表格之间的区别以及为什么我在播放过程中无法找到右侧表格的元素。

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

请找到代码摘录:

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

在 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)

【问题讨论】:

    标签: java html selenium-webdriver


    【解决方案1】:

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

    -的返回值是多少:

    trackingID.isDisplayed();
    

    这可以通过隐式等待来避免

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    

    或者通过 WebDriver 等待

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

    希望有帮助

    【讨论】:

      猜你喜欢
      • 2019-11-02
      • 2021-07-23
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多