【问题标题】:Selenium with maven cannot detect webelements it was able to detect without maven带有 maven 的 Selenium 无法检测到没有 maven 可以检测到的 webelements
【发布时间】:2020-07-14 06:36:37
【问题描述】:

我已将我的 selenium TestNG 项目转换为 maven,并在同一个 webelement 上编写了相同的命令,在非 maven 版本中检测到对象,但在 maven 版本中找不到它(java.lang.NoClassDefFoundError)

这是命令:

                        // Define Ad
                        wait.until(ExpectedConditions.visibilityOfElementLocated
                                (By.cssSelector("span.close.js-rebateHook")));
                        WebElement Ad_X = driver.findElement(By.cssSelector("span.close.js-rebateHook"));
                        
                        // Closes ad if exists
                        if (Ad_X.isDisplayed()) {
                            //act.doubleClick(Ad_X).perform();
                            Ad_X.click();
                            System.out.println("Closed Ad");
                        }
                        else {
                            ;
                        }
                        
                        

这是 maven 运行时的控制台结果:

[ERROR] Logo_Exsists(testcases.OpenZafulTest)  Time elapsed: 7.9 s  <<< FAILURE!
java.lang.NoClassDefFoundError: org/openqa/selenium/interactions/Coordinates
    at testcases.OpenZafulTest.Logo_Exsists(OpenZafulTest.java:53)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.interactions.Coordinates
    at testcases.OpenZafulTest.Logo_Exsists(OpenZafulTest.java:53)

这是非 maven 运行的控制台结果(打印,也关闭了广告):

Closed Ad

这是我的 pom.xml(此时非常混乱,我被困在这个问题上很长一段时间,并根据我在网上看到的许多示例尝试添加、删除和更改我的许多依赖项):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ZafulSelMaven4</groupId>
  <artifactId>ZafulSelMaven4</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <name>ZafulSelMaven4</name>
    <url>http://maven.apache.org</url>
    
 <licenses>
        <license>
            <name>Apache 2</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
            <comments>A business-friendly OSS license</comments>
        </license>
    </licenses>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.argument.target>${maven.compiler.target}</maven.compiler.argument.target>
        <maven.compiler.argument.source>${maven.compiler.source}</maven.compiler.argument.source>
        <appium-java-client.version>7.1.0</appium-java-client.version>
        <spring-boot.version>2.1.6.RELEASE</spring-boot.version>
        <slf4j.version>1.7.25</slf4j.version>
        <logback.version>1.2.3</logback.version>
        
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <suiteXmlFile>testng.xml</suiteXmlFile>
        <java.version>1.8</java.version>
        <!--Dependency Versions-->
        <selenium.version>3.141.59</selenium.version>
        <assertj-core.version>3.15.0</assertj-core.version>
        <testng.version>7.1.0</testng.version>
        <query.version>2.0.0-BETA3</query.version>
        <!--Plugin Versions-->
        <driver-binary-downloader-maven-plugin.version>1.0.18</driver-binary-downloader-maven-plugin.version>
        <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
        <maven-failsafe-plugin.version>2.22.2</maven-failsafe-plugin.version>
        <!--Configuration Properties-->
        <overwrite.binaries>false</overwrite.binaries>
        <read.timeout>30000</read.timeout>
        <connection.timeout>40000</connection.timeout>
        <retry.attempts>4</retry.attempts>
        <browser>chrome</browser>
        <threads>1</threads>
        <remote>false</remote>
        <headless>true</headless>
        <seleniumGridURL/>
        <platform/>
        <browserVersion/>
        <proxyEnabled>false</proxyEnabled>
        <proxyHost/>
        <proxyPort/>
        <proxyUsername/>
        <proxyPassword/>
        
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>${appium-java-client.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
            
        <dependency>
              <groupId>org.hamcrest</groupId>
              <artifactId>hamcrest</artifactId>
              <version>2.2</version>
        </dependency>
        
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version><!--$NO-MVN-MAN-VER$-->
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version><!--$NO-MVN-MAN-VER$-->
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
            <version>${selenium.version}</version><!--$NO-MVN-MAN-VER$-->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version><!--$NO-MVN-MAN-VER$-->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>${assertj-core.version}</version><!--$NO-MVN-MAN-VER$-->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.lazerycode.selenium</groupId>
            <artifactId>query</artifactId>
            <version>${query.version}</version>
            <scope>test</scope>
        </dependency>
         <dependency>
          <groupId>org.json</groupId>
          <artifactId>json</artifactId>
          <version>20170516</version>
          <scope>compile</scope>
        </dependency>
                
        <dependency>
         <groupId>com.googlecode.json-simple</groupId>
         <artifactId>json-simple</artifactId>
         <version>1.1.1</version>
         <scope>compile</scope>
       </dependency> 
       
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>



        
    </dependencies>

    <profiles>
        <profile>
            <id>selenium-tests</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>3.0.0-M3</version>
                        <configuration>                                          
                         <suiteXmlFiles>
                          <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>               
                         </suiteXmlFiles>   
                         <suitename>Test_Suite</suitename>     
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <configuration>
                            <source>${java.version}</source>
                            <target>${java.version}</target>
                        </configuration>
                        <version>${maven-compiler-plugin.version}</version>
                    </plugin>
                    <plugin>
                        <groupId>com.lazerycode.selenium</groupId>
                        <artifactId>driver-binary-downloader-maven-plugin</artifactId>
                        <version>${driver-binary-downloader-maven-plugin.version}</version>
                        <configuration>
                            <rootStandaloneServerDirectory>${project.basedir}/src/test/resources/selenium_standalone_binaries</rootStandaloneServerDirectory>
                            <downloadedZipFileDirectory>${project.basedir}/src/test/resources/selenium_standalone_zips</downloadedZipFileDirectory>
                            <customRepositoryMap>${project.basedir}/src/test/resources/RepositoryMap.xml</customRepositoryMap>
                            <overwriteFilesThatExist>${overwrite.binaries}</overwriteFilesThatExist>
                            <onlyGetDriversForHostOperatingSystem>false</onlyGetDriversForHostOperatingSystem>
                            <fileDownloadRetryAttempts>${retry.attempts}</fileDownloadRetryAttempts>
                            <fileDownloadReadTimeout>${read.timeout}</fileDownloadReadTimeout>
                            <operatingSystems>
                                <windows>true</windows>
                                <linux>true</linux>
                                <mac>true</mac>
                            </operatingSystems>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>selenium</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>${maven-failsafe-plugin.version}</version>
                        <configuration>
                            <parallel>methods</parallel>
                            <threadCount>${threads}</threadCount>
                            <systemPropertyVariables>
                                <browser>${browser}</browser>
                                <screenshotDirectory>${project.build.directory}/screenshots</screenshotDirectory>
                                <remoteDriver>${remote}</remoteDriver>
                                <gridURL>${seleniumGridURL}</gridURL>
                                <desiredPlatform>${platform}</desiredPlatform>
                                <desiredBrowserVersion>${browserVersion}</desiredBrowserVersion>
                                <proxyEnabled>${proxyEnabled}</proxyEnabled>
                                <proxyHost>${proxyHost}</proxyHost>
                                <proxyPort>${proxyPort}</proxyPort>
                                <proxyUsername>${proxyUsername}</proxyUsername>
                                <proxyPassword>${proxyPassword}</proxyPassword>
                                <headless>${headless}</headless>

                                <!--Set properties passed in by the driver binary downloader-->
                                <webdriver.chrome.driver>${webdriver.chrome.driver}</webdriver.chrome.driver>
                                <webdriver.ie.driver>${webdriver.ie.driver}</webdriver.ie.driver>
                                <webdriver.opera.driver>${webdriver.opera.driver}</webdriver.opera.driver>
                                <webdriver.gecko.driver>${webdriver.gecko.driver}</webdriver.gecko.driver>
                                <webdriver.edge.driver>${webdriver.edge.driver}</webdriver.edge.driver>
                            </systemPropertyVariables>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <!--This goal makes the build fail if you have test failures-->
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

如果有人知道这个问题的原因或如何解决它,那将有很大帮助

【问题讨论】:

  • 你是如何运行测试的?单纯的跑课?还是 mvn 命令?
  • 我用“Run as maven test”运行 pom.xml,它引用了 testng.xml 文件
  • 尝试一次 mvn clean 和 mvn compile。
  • 现在试了,没有任何变化,同样的问题又发生了
  • 所以你没有看到任何编译问题..当你运行测试时粘贴完整的控制台异常。它与你的 Maven 配置相关。找不到那个类

标签: java html selenium maven selenium-webdriver


【解决方案1】:

您的项目在转换时损坏了,右键单击该项目,转到 maven 并更新该项目

【讨论】:

  • 我已经更新了did the maven>更新项目,很多次都没有解决
  • 好的,如果没有看到你的实际项目,这将很难说,参考这个可能会有所帮助 - javarevisited.blogspot.com/2011/06/…
猜你喜欢
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-20
相关资源
最近更新 更多