Two things:
1) Are you sure that the element has actually appeared on the page?
Particularly in a real browser (I notice that you're using HtmlUnit)
2) The wait uses generics to make the common case situation you're
running into more efficient:
WebElement data = wait.until(visibilityOfElementLocated(By.id("data")));
The error you're seeing indicates that the element hasn't been
inserted into the DOM, so it's possible that the AJAX call hasn't
returned yet.
Simon
On Fri, Sep 28, 2012 at 11:12 PM, Tracey Kirk <
trace...@gmail.com> wrote:
> Hi,
>
> I'm using Selenium WebDriver with JUnit to write a simple test to get an
> element in HTML that uses an Ajax script. The error (below) is that the
> element located by By.id: data can't be found. I've googled Ajax and
> WebDriver and found that waiting for Ajax to return is an issue, so I've
> already changed my wait to use WebDriverWait and ExpectedConditions but this
> did not help.
> Here is the test:
> @Test
> public void AjaxTest() throws Exception {
> driver.get(baseUrl + "/WebDriver.php");
> WebDriverWait wait = new WebDriverWait(driver, 10);
> wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("data")));
> WebElement data = driver.findElement(By.id("data"));
> String text = new String(data.getText());
> System.out.println(text);
> }
>
> The HTML:
> <!DOCTYPE html>
> <html>
> <head>
> <meta charset="utf-8">
> <script type="text/javascript" src="js/libs/jquery-1.7.2.min.js">
> <script>
> $(document).ready(function() {
> $.ajax({
> url: '/data/30day.csv',
> type: 'GET',
> dataType: 'text',
> success: function(result) { $('#content').html('<div id="data">' + result +
> '</div>')},
> error: function() { alert('error') }
> });
> })
> </script>
> </head>
> <body>
> <div id="content" style="width: 400px; height: 400px; border: 1px solid
> red;">
> </body>
> </html>
>
> The error is:
>
> [junit] Testcase: AjaxTest took 30.025 sec
> [junit] Caused an ERROR
> [junit] Timed out after 10 seconds waiting for visibility of element
> located by By.id: data
> [junit] Build info: version: '2.24.1', revision: '17205', time:
> '2012-06-19 16:53:24'
> [junit] System info:
os.name: 'Windows 7', os.arch: 'amd64', os.version:
> '6.1', java.version: '1.6.0_27'
> [junit] Driver info: driver.version: unknown
> [junit] org.openqa.selenium.TimeoutException: Timed out after 10 seconds
> waiting for visibility of element located by By.id: data
> [junit] Build info: version: '2.24.1', revision: '17205', time:
> '2012-06-19 16:53:24'
> [junit] System info:
os.name: 'Windows 7', os.arch: 'amd64', os.version:
> '6.1', java.version: '1.6.0_27'
> [junit] Driver info: driver.version: unknown
> [junit] at
> org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:251)
> [junit] at
> org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:220)
> [junit] at
> com.auto.tests.PMModuleListLastReportedPollingTest.AjaxTest(Unknown Source)
> [junit] Caused by: org.openqa.selenium.NoSuchElementException: Unable to
> locate element with ID: data
> [junit] For documentation on this error, please visit:
>
http://seleniumhq.org/exceptions/no_such_element.html
> [junit] Build info: version: '2.24.1', revision: '17205', time:
> '2012-06-19 16:53:24'
> [junit] System info:
os.name: 'Windows 7', os.arch: 'amd64', os.version:
> '6.1', java.version: '1.6.0_27'
> [junit] Driver info: driver.version: HtmlUnitDriver
> [junit] at
> org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementById(HtmlUnitDriver.java:693)
> [junit] at
org.openqa.selenium.By$ById.findElement(By.java:215)
> [junit] at
> org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1243)
> [junit] at
> org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
> [junit] at
> org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:983)
> [junit] at
> org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1240)
> [junit] at
> org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:395)
> [junit] at
> org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:421)
> [junit] at
> org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:419)
> [junit] at
> org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:121)
> [junit] at
> org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:1)
> [junit] at
> org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:1)
> [junit] at
> org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:200)
> [junit]
>
> --
> You received this message because you are subscribed to the Google Groups
> "Selenium Users" group.
> To post to this group, send email to
seleniu...@googlegroups.com.
> To unsubscribe from this group, send email to
>
selenium-user...@googlegroups.com.
> To view this discussion on the web visit
>
https://groups.google.com/d/msg/selenium-users/-/bEztt1erThYJ.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>