Selenium NoSuchMethodError

戏子无情 提交于 2020-06-27 11:22:10

问题


I am writing an automated test framework and have already written a substantial amount. However, I have come across an error which I cannot seem to fix or find a solution for.

The problem occurs when selenium tries to answer a date question and select from a dropdown menu of dates. I have achieved this loads of times with no problems, but for some reason I get the error as displayed in the title. After trying to find a solution on google I decided to come here as there seems to be nothing out there that's similar.

The code where my automated test breaks is:

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();

The error occurs on the lines where the methods selectByVisibleText and selectByIndex is used. I have used these methods hundreds of times before with no issues.. When I run the tests in the browser with the Selenium IDE it executes with no problems. But as soon as the code is transferred across to Eclipse it doesnt work.

The only thing I can possibly think of is that I have converted the project from JUnit to TestNG.. Would this have any implications? I have done this before though without any issues.

Any help would be greatly appreciated!

EDIT

Stacktrace:

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)


回答1:


This issue has been resolved by importing the latest selenium maven dependency in to my pom.xml. I still can't understand why this issue occurred as I have used it in other projects. The maven dependency to fix the issue:

    <!-- 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>



回答2:


I had same problem then I try to add dependency like user3008643's answer, but it still no working.

user3008643 said it works for him, so I looked my pom.xml carefully, then I found out something weird:

I had imported 3 version of selenium dependencies.

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>3.11.0</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-support</artifactId>
    <version>2.0a7</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.12.0</version>
</dependency>

It's really nonsense, so I changed it to same version 3.12.

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>3.12.0</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-support</artifactId>
    <version>3.12.0</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.12.0</version>
</dependency>

After updated dependencies, the problem had been fixed.

I think the problem is about dependencies, you have to import the same version of dependencies.



来源:https://stackoverflow.com/questions/42997732/selenium-nosuchmethoderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!