2017-02-16 82 views
0

我正在使用Selenium Standalone Server 3.0.1。我試圖在我的代碼中添加一個Explicit Wait,以便在元素變爲可見時通過xpath檢測元素。爲了獲得一些Java幫助,我查找了Selenium Standalone Server 3.0.1的源代碼,但無法找到它。我發現selenium-java-2.53.1版本的源代碼。我下載了它並發現了selenium-java-2.53.1-srcs並添加到了我的Eclipse IDE。在FluentWait的幫助下,我簡單地複製粘貼了我的Eclipse IDE中的代碼並更改了變量名稱。Selenium Webdriver 3.0.1- [Eclipse-Java-Chrome]:Selenium顯示FluentWait類錯誤

在文檔中的示例代碼是這樣的:

// Waiting 30 seconds for an element to be present on the page, checking 
    // for its presence once every 5 seconds. 
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) 
     .withTimeout(30, SECONDS) 
     .pollingEvery(5, SECONDS) 
     .ignoring(NoSuchElementException.class); 

    WebElement foo = wait.until(new Function<WebDriver, WebElement>() { 
    public WebElement apply(WebDriver driver) { 
     return driver.findElement(By.id("foo")); 
     } 
    }); 

但是,當我實現這個代碼,只需複製粘貼:

 Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) 
      .withTimeout(30, TimeUnit.SECONDS) 
      .pollingEvery(5, TimeUnit.SECONDS) 
      .ignoring(NoSuchElementException.class); 

     WebElement element = wait.until(new Function<WebDriver, WebElement>()  { 
     public WebElement apply(WebDriver driver) { 
      return driver.findElement(By.xpath("//p[text()='WebDriver']")); 
     } 
     }); 

我對FluentWait類得到一個錯誤,因爲The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver>

以下是我進口的清單:

import java.util.concurrent.TimeUnit; 
    import org.apache.log4j.Logger; 
    import org.apache.log4j.PropertyConfigurator; 
    import org.openqa.selenium.By; 
    import org.openqa.selenium.NoSuchElementException; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.WebElement; 
    import org.openqa.selenium.chrome.ChromeDriver; 
    import org.openqa.selenium.support.ui.Wait; 
    import com.google.common.base.Function; 

任何人都可以幫助我嗎?

@blalasaadri @Mira從你的最後有什麼建議嗎?

+1

你嘗試了文檔的例子嗎? https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html – JeffC

+0

@JeffC謝謝。我可以看到selenium-java-2.53.1 src代碼提供的文檔與您共享的URL中提供的文檔有一些區別。在selenium-java-2.53.1 src代碼中,創建一個對象是<< - 等待 wait = new FluentWait (驅動程序) - >>但鏈接文檔將其描述爲<< - Wait wait = new FluentWait (driver) - >> – DebanjanB

回答

1

您需要在等待下面指定預期條件,下面是可以解決您的問題的修改後的代碼。

代碼: 我的代碼

import java.util.concurrent.TimeUnit; 

import org.junit.Test; 

import org.openqa.selenium.By; 

import org.openqa.selenium.NoSuchElementException; 

import org.openqa.selenium.WebDriver; 

import org.openqa.selenium.WebElement; 

import org.openqa.selenium.support.ui.ExpectedConditions; 

import org.openqa.selenium.support.ui.FluentWait; 

import org.openqa.selenium.support.ui.Wait; 

public class DummyClass 

{ 

    WebDriver driver; 
    @Test 
    public void test() 
{ 

      Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) 
        .withTimeout(30, TimeUnit.SECONDS) 
        .pollingEvery(5, TimeUnit.SECONDS) 
        .ignoring(NoSuchElementException.class); 

       until(new Function<WebElement , Boolean>() 
       { 
      public Boolean apply(WebElement element) 
      { 
       return element.getText().endsWith("04"); 
      //WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Sample Post2"))); 
      } 

private void until(Function<WebElement, Boolean> function) { 
    driver.findElement(By.linkText("Sample Post2")); 

} 
+0

感謝您的建議。我可以從你的代碼中看到你已經使用了ExpectedConditions,但是我認爲這就是我們在實現Explicit Wait的時候這樣做的方式。但是,webdriver在你的案例中每隔5秒輪詢元素?到目前爲止,我不確定輪詢是如何在Fluent Wait中實現的,但我只能實現這一點。任何其他建議對我來說? – DebanjanB

+0

我忘了提及,Eclipse IDE在爲類「等待」創建對象「等待」時顯示錯誤<< - 等待 wait = new FluentWait (驅動程序) - >> – DebanjanB

+0

我已編輯代碼或檢查下面的代碼是否可以幫助? FluentWait 等待=新FluentWait (駕駛員) \t .withTimeout(100,TimeUnit.SECONDS) \t .pollingEvery(50,TimeUnit.MILLISECONDS); \t \t \t \t //開始等待指定元素 \t \t等待。直到(new ElementWaitCondition(browser,query)); – Dharam