java - 尽管元素在网页中可见,但无法找到该元素

标签 java html selenium-webdriver

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

请查找随附的网页 HTML 摘录 http://1drv.ms/1CMtRxd并找到相同的UI 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)

最佳答案

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

返回值是多少-:

trackingID.isDisplayed();

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

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

或者通过WebDriver等待

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

希望有帮助

关于java - 尽管元素在网页中可见,但无法找到该元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31182318/

相关文章:

java - Struts有什么好处

Java应用程序设计问题

HTML/CSS 背景问题

html - <a href ="cb289e02-ed2b-4daa-">pdf</a> 保存一个更具描述性的文件名?

java - 使用 Kafka Spout 集成 Kafka Storm

java - 在 Eclipse 中并行运行 JUnit 参数化测试

html - <div> 顶部的未知额外空间

python - 从页面获取链接 - python

python - Selenium webdriver python 从网站查找动态文本

java - Selenium WebDriver 在调试中通过,但在定期运行时则不然