2015-02-10 26 views
0

我正在使用Selenium(2.44.0)進行Java單元測試,並試圖使用WebElementisDisplayed()方法,但它似乎不是可用。我只能看到isEnabled()和​​方法。Java - Selenium WebElement isDisplayed()方法不可用

(在Eclipse 錯誤消息: 「isDisplayed()的方法,是未定義的類型WebElement」

的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://[email protected]/jenkinsci/extended-choice-parameter-plugin.git</connection> 
    <developerConnection>scm:git:ssh://[email protected]/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; 
     } 
    } 

任何想法我可能會失蹤?

EDIT - 加入充分的pom.xml和蝕錯誤消息,以澄清問題

EDIT(2) - 改變Java代碼來關於實際的(使用的)的Java代碼@Vivek辛格的第一關於異常處理的意見

+0

不知道這裏發生了什麼。但出於好奇,如果找不到元素,找不到元素會引發異常?那麼在這種情況下不會顯示爲無法訪問的行嗎?任何意見都是值得歡迎的。 – 2015-02-10 15:55:58

+2

你有什麼版本的selenium-api.jar? – minion 2015-02-10 15:58:36

+0

「看起來不可用」是什麼意思?有錯誤嗎?什麼? – SiKing 2015-02-10 16:02:29

回答

1

Selenium commons將WebElement.java引入到沒有isDisplayed()方法的完全相同的包結構中。所以你的參考很混亂。不要在您的pom.xml中包含硒共享物

1

這就像@tome所說的。 selenium-common.jar是2.0b1版本。在Selenium 2.0(final)中添加了isDisplayed()。在此之前它位於其他類。所以selenium-common口罩selenium-api這就是你無法編譯的原因。

+0

在我使用Selenium 2.44.0和commons-lang 3.x的經驗中,我沒有任何問題。它工作正常。 – djangofan 2015-02-10 17:03:12

相關問題