在我的webdriver脚本中,我正在尝试获取一个webElement [恰好是一个页面标题]&取得定位器&通过 .properties 文件的位置。
第一&最重要的查询是,如果你的xpath在语法上是正确的,你会得到这样的异常[下面给出例外]?我在这一点上感到困惑,因为附加的消息以及无法评估或在WebElement中没有结果的异常
设置详情,
OS => Windows 7
浏览器=> IE 9
Webdriver version => 2.44.0.0
IE驱动程序服务器=> 32位
以下是xpath [从属性文件中复制],
.//*[@id='frmPDAuditReports']//table[@class='PageHeading']/tbody/tr/td
获取webElement的代码是,
public WebElement pageHeading() throws Exception
{
boolean elementFoundMessage = CreatePolicyApprovalDeviationRequestPageObjectRetriever.customElementWaitWithTimeoutWithProperties(driver,CreatePolicyApprovalDeviationRequestPageObjectRetriever.getLocator("pageHeading", "Page Heading"),elementTimeout);
if(elementFoundMessage)
{
element = CreatePolicyApprovalDeviationRequestPageObjectRetriever.returnWebElement(driver, CreatePolicyApprovalDeviationRequestPageObjectRetriever.getLocator("pageHeading", "Page Heading"));
}
else
{
log.error("PageHeading():Element not found: Null Returned");
}
return element;
}
属性文件的有关部分看起来像
pageHeading = xpath : .//*[@id='frmPDAuditReports']//table[@class='PageHeading']/tbody/tr/td
现在,xpath在语法上是正确的&我已经通过firepath验证了它。为此,在我的大多数执行过程中,xpath实际上工作正常。只有抛出异常,如下所示,
org.openqa.selenium.InvalidSelectorException: The xpath expression './/*[@id='frmPDAuditReports']//table[@class='PageHeading']/tbody/tr/td' cannot be evaluated or does notresult in a WebElement (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 47 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'
System info: host: 'OTINWISRCDT050', ip: '172.24.185.103', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_71'
*** Element info: {Using=xpath, value=.//*[@id='frmPDAuditReports']//table[@class='PageHeading']/tbody/tr/td}
Session ID: c613b73a-e941-4847-ae49-06bcae108cdd
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=1, ignoreZoomSetting=true, enablePersistentHover=false, ie.ensureCleanSession=true, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=9, ie.usePerProcessProxy=false, ignoreProtectedModeSettings=true, cssSelectorsEnabled=true, requireWindowFocus=true, initialBrowserUrl=http://localhost:47437/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
ERROR[CreatePolicyApprovalDeviationRequestPageObjects]: PageHeading():Element not found: Null Returned
ERROR[CreatePolicyApprovalDeviationRequestPageServices]: PageHeading(): Page Heading element not found !!
ERROR[CreatePolicyApprovalDeviationRequestPageServices]: PageHeading(): Failed
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:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:383)
at org.openqa.selenium.remote.RemoteWebDriver.findElementsByXPath(RemoteWebDriver.java:459)
at org.openqa.selenium.By$ByXPath.findElements(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:346)
at Utilities.ObjectRetriever.customElementWaitWithTimeoutWithProperties(ObjectRetriever.java:172)
at PageObjects.CreatePolicyApprovalDeviationRequestPageObjects.pageHeading(CreatePolicyApprovalDeviationRequestPageObjects.java:34)
at ServicesOfPage.CreatePolicyApprovalDeviationRequestPageServices.getPageHeading(CreatePolicyApprovalDeviationRequestPageServices.java:58)
at Tests.isNavigatedToCreatePolicyApprovalDeviationRequestPage.compareHomePageHeading(isNavigatedToCreatePolicyApprovalDeviationRequestPage.java:32)
at Execution.TestExecution.TC(TestExecution.java:254)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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)
我发现很难弄清楚这个特定执行可能出现的问题是什么,帮助我!
任何帮助将不胜感激!!
答案 0 :(得分:0)
我可以看到它无效的三个原因。
1)错误的XPath : 我建议安装Google Chrome。在Chrome中,当您检查元素时,可以右键单击元素,然后直接复制XPath。
2)页面标题异步加载: 您确定网站不会异步加载您的元素吗?尝试禁用JavaScript并加载网站以查看元素是否存在。
3)浏览器错误?: 这有时会发生,IE对Selenium来说不是一个很好的浏览器。您可以尝试使用Firefox或像PhantomJS这样的无头浏览器。