WebDriver HTMLUnit与Ajax有关

Krz*_*zys 5 java ajax webdriver htmlunit

我想使用selenium webdriver(java)测试一个站点,但该站点包含ajax,HTMLUnit没有看到ajax内容.

有解决方法吗?

例:

WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
//login into your account
this.login(driver);
//click on edit Profile Link into an ajax-loaded tab
driver.findElement(By.id("editProfile")).click();
//Result: org.openqa.selenium.NoSuchElementException 
Run Code Online (Sandbox Code Playgroud)

Pav*_*rin 5

在与元素交互之前使用等待条件必须在ajax响应之后出现.

WebDriverWait wait = new WebDriverWait(webDriver, 5);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_element")));
Run Code Online (Sandbox Code Playgroud)

这使得webDriver在5秒内等待你的元素.之前曾问过这个问题.

  • WebDriver是一个界面.HtmlUnitDriver等是它的实现.它以同样的方式工作.别担心. (2认同)