我正在编写一个自动化测试框架,并且已经编写了大量内容。但是,我遇到了一个我似乎无法修复或找不到解决方案的错误。
当 selenium 尝试回答日期问题并从日期下拉菜单中进行选择时,就会出现问题。我已经毫无问题地完成了这么多次,但由于某种原因,我得到了标题中显示的错误。在尝试在谷歌上找到解决方案后,我决定来这里,因为那里似乎没有类似的东西。
我的自动化测试中断的代码是:
new Select(driver.findElement(By.id("AMOUNT_OF_DHP_DATE_FROM_d"))).selectByVisibleText("4th");
new Select(driver.findElement(By.id("AMOUNT_OF_DHP_DATE_FROM_m"))).selectByIndex(4);
driver.findElement(By.id("AMOUNT_OF_DHP_DATE_FROM_y")).sendKeys("2017");
driver.findElement(By.id("AMOUNT_OF_DHP_WEEKLY")).sendKeys("50");
new Select(driver.findElement(By.id("AMOUNT_OF_DHP_DATE_TO_d"))).getFirstSelectedOption();
new Select(driver.findElement(By.id("AMOUNT_OF_DHP_DATE_TO_m"))).getFirstSelectedOption();
driver.findElement(By.id("AMOUNT_OF_DHP_DATE_TO_y")).sendKeys("2017");
clickNext();
错误发生在使用方法 selectByVisibleText
和 selectByIndex
的行上。我之前已经使用这些方法数百次,没有任何问题。当我使用 Selenium IDE 在浏览器中运行测试时,它执行时没有任何问题。但是一旦将代码传输到 Eclipse,它就无法工作。
我唯一可能想到的是我已经将项目从 JUnit 转换为 TestNG。这会有什么影响吗?我以前做过,但没有任何问题。
如有任何帮助,我们将不胜感激!
编辑
堆栈跟踪:
java.lang.NoSuchMethodError: org.openqa.selenium.WebElement.setSelected()V
at org.openqa.selenium.support.ui.Select.selectByVisibleText(Select.java:111)
at tns_automation.DHP_Forms.DhpAbstractTest.answerAmountOfDHPQuestion(DhpAbstractTest.java:348)
at tns_automation.DHP.KnowsleyDHPTest.completeAssessmentFormAndBudgetToolBeforeDownloadingPDFFromInTrayWithCookiesEnabled(KnowsleyDHPTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)
最佳答案
这个问题已经通过将最新的 selenium maven 依赖项导入到我的 pom.xml 中得到解决。我仍然不明白为什么会出现这个问题,因为我已经在其他项目中使用过它。解决问题的 Maven 依赖项:
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.3.1</version>
</dependency>
关于java - Selenium NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42997732/