我对 Selenium 有疑问。在下面的测试中,它应该访问网站,获取产品,将其添加到购物车并转到购物车。在这之间,它等待一些元素出现。
在我的机器上,测试工作正常,直到将产品添加到购物车。之后,div 被 ajax 填充,测试应该等待其中的元素。该元素出现在浏览器中,但测试仍在等待。甚至超时似乎也没有足够有趣来打扰它。
如果我用 wait.until(pause(2));
等待或设置 driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
一切正常,但这不是正确的解决方案。
有什么想法吗?
我的系统:
MacOS X 10.8.2 或 Windows 7(64 位)
火狐17.0.1
Java 1.6、JUnit 4.10、selenium-api-2.25.0、selenium-support-2.25.0、selenium-firefox-driver-2.25.0、guava-12.0
我的测试:
import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable;
import javax.annotation.Nullable;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.base.Function;
public class TestSomeWebsite {
@Test
public void test() throws Exception {
FirefoxDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("http://www.obi.de");
// take first product
driver.findElement(By.cssSelector("p.product_desc > a")).click();
// add to cart
wait.until(elementToBeClickable(By.id("addtocart")));
driver.findElement(By.id("addtocart")).submit();
// go to cart
/** everything fine by now **/
wait.until(elementToBeClickable(By.partialLinkText("Zum Warenkorb")));
/** we never come to this point :( **/
driver.findElement(By.partialLinkText("Zum Warenkorb")).click();
}
private Function<WebDriver, Object> pause(final int time) {
return new Function<WebDriver, Object>() {
int count = 0;
@Nullable
public Object apply(@Nullable WebDriver input) {
count++;
return count >= time;
}
};
}
}
最佳答案
@snieke,尝试将您的 selenium 版本升级到 2.28.0。我 99% 确信这对您有用。
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.28.0</version>
</dependency>
关于java - WebDriverWait.until(ExpectedConditions.elementToBeClickable(someAjaxMagic)) 永远不会返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13860812/