2017-02-16 62 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

相关问题