Java - Selenium WebElement isDisplayed() 方法不可用

标签 java maven selenium selenium-webdriver

我正在使用 Selenium (2.44.0) 进行 Java 单元测试,并且我正在尝试使用 WebElementisDisplayed() 方法,但它似乎不可用。我只能看到 isEnabled()isSelected() 方法。

(Eclipse 中的错误消息:“WebElement 类型的方法 isDisplayed() 未定义”)

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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.jenkins-ci.plugins</groupId>
    <artifactId>plugin</artifactId>
    <version>1.499</version>
    <!-- which version of Hudson is this plugin built against? -->
    <relativePath />
</parent>

<artifactId>extended-choice-parameter</artifactId>
<packaging>hpi</packaging>
<version>0.35-SNAPSHOT</version>
<name>Extended Choice Parameter Plug-In</name>
<url>http://wiki.jenkins-ci.org/display/JENKINS/Extended+Choice+Parameter+plugin</url>

<developers>
    <developer>
        <id>vimil</id>
        <name>Vimil Saju</name>
    </developer>
</developers>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>InjectedTest.java</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>            
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>net.sf.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit-dep</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.htmlunit</groupId>
        <artifactId>htmlunit</artifactId>
        <version>2.15</version>
        <scope>test</scope>
    </dependency> 
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.8.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <version>1.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.44.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-common</artifactId>
          <version>2.0b1</version>
    </dependency>
    <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-api</artifactId>
          <version>2.44.0</version>
    </dependency>       
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.1.1</version>
      <scope>test</scope>
    </dependency>
</dependencies> 

<!-- get every artifact through maven.glassfish.org, which proxies all the artifacts that we need -->
<repositories>
    <repository>
        <id>repo.jenkins-ci.org</id>
        <url>http://repo.jenkins-ci.org/public/</url>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>repo.jenkins-ci.org</id>
        <url>http://repo.jenkins-ci.org/public/</url>
    </pluginRepository>
</pluginRepositories>
<distributionManagement>
    <repository>
    <id>maven.jenkins-ci.org</id>
    <url>http://maven.jenkins-ci.org/content/repositories/releases/</url>
</repository>
</distributionManagement>
<scm>
    <connection>scm:git:ssh://git@github.com/jenkinsci/extended-choice-parameter-plugin.git</connection>
    <developerConnection>scm:git:ssh://git@github.com/jenkinsci/extended-choice-parameter-plugin.git</developerConnection>
    <url>https://github.com/jenkinsci/extended-choice-parameter-plugin</url>
</scm>

Java代码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

.......

    WebElement element;
    int size = driver.findElements(By.xpath("//select[@name='type']")).size();
    for (int i = 0; i < size; i++) {
        element = driver.findElements(By.xpath("//select[@name='type']")).get(i);
        if (element.isDisplayed()) {
            select = new Select(element);
            select.selectByVisibleText("Multi-Level Multi Select");
            break;
        }
    }

知道我可能会错过什么吗?

编辑 - 添加了完整的 pom.xml 和 eclipse 错误消息以澄清问题

编辑 (2) - 将 Java 代码更改为有关 @Vivek Singh 关于异常处理的第一个评论的实际(已使用)Java 代码

最佳答案

Selenium commons 在完全相同的包结构中引入了 WebElement.java,但没有 isDisplayed() 方法。所以你的引用资料被搞乱了。不要在 pom.xml 中包含 selenium-commons

关于Java - Selenium WebElement isDisplayed() 方法不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28435691/

相关文章:

Java递归返回 boolean 值

java - java程序中的"Cannot find symbol"

python - 单击带有 selenium 和 python 的 href 按钮?

python selenium send_keys等待

Python 爬虫找不到特定的 Xpath

java - Kafka 产生多值消息

java - 如何获取maven元数据?

maven - 如何获取使用fabric8 maven插件构建的Docker图像详细信息?

maven - 如何使用 maven 生成要包含在 Jar 文件中的资源?

maven - 我可以在 Gradle 中使用 Maven 插件(发音)吗?