1

Selenium 2.25, Firefox, OSX ML.

I have Selenium click a button that has an Ajax impact.

Then I want to interact with the results. Without the sleep below, I get an error that the click fails because it cannot scroll the click target into view. I don't get it. What's going on?

    WebElement sampleComboInput = driver.findElement(By.id("sampleCombo-inputEl"));
    sampleComboInput.click();
    sampleComboInput.sendKeys("English-03.txt");
    sampleComboInput.sendKeys(Keys.TAB, Keys.TAB);
    WebElement goButton = driver.findElement(By.id("inputDialogGoButton-btnInnerEl"));
    goButton.click();

    WebElement resultsSpan = driver.findElement(By.cssSelector("span.ne-type-node.PERSON"));
    assertTrue(resultsSpan.isDisplayed());
    assertEquals("PERSON (3)", resultsSpan.getText());
    WebElement parent = resultsSpan.findElement(By.xpath(".."));
    Thread.sleep(5000); // without sleep, get error unable to scroll
    Actions action = new Actions(driver);
    action.doubleClick(parent);
    action.perform();
bmargulies
  • 97,814
  • 39
  • 186
  • 310

2 Answers2

0

it needs to wait for the element to load, have you tried waitForVisible instead? It might be more efficent also using Sleep may cause your code to not function on other machines where the wait time can be longer

space ranger
  • 422
  • 1
  • 8
  • 21
  • The element is long ago loaded, since otherwise I wouldn't be able to find it. Also note the 'isDisplayed'. Could it be not visible but displayed? – bmargulies Aug 18 '12 at 23:49
  • from the wiki "Determines if the specified element is visible. An element can be rendered invisible by setting the CSS “visibility” property to “hidden”, or the “display” property to “none”, either for the element itself or one if its ancestors. This method will fail if the element is not present." – space ranger Aug 18 '12 at 23:55
  • waitForVIsible is not part of the Java API in 2.25.0. It was removed in favor of implicit waiting, which is turned on. – bmargulies Aug 19 '12 at 01:13
-1

You should try to avoid using Thread.Sleep as it will work inconsistently. Space ranger is right that it needs to wait for the element to load.

The c# version has the "WebDriverWait" class and the "Until" method. Using these two you can wait for elements to load.

Look at the answer by Loudenvier to this post: Selenium c# Webdriver: Wait Until Element is Present

This is the way I prefer. Simple to use, easily reusable, and avoids issues related to Thread.Sleep.

Community
  • 1
  • 1
Seth Micalizzi
  • 449
  • 6
  • 17