Skip to content

Instantly share code, notes, and snippets.

@Ardesco
Created January 27, 2013 20:38
Show Gist options
  • Save Ardesco/4650349 to your computer and use it in GitHub Desktop.
Save Ardesco/4650349 to your computer and use it in GitHub Desktop.
Solution for bensullivan
import org.openqa.selenium.By
import org.openqa.selenium.StaleElementReferenceException
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.support.ui.ExpectedCondition
import org.openqa.selenium.support.ui.WebDriverWait
import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable
import static org.openqa.selenium.support.ui.ExpectedConditions.findElement
def browser = new FirefoxDriver()
browser.manage().window().maximize()
browser.get('http://translate.google.com/')
def wait = new WebDriverWait(browser, 10)
def languageButton = browser.findElementById('gt-sl-gms')
languageButton.click()
def norwegianLanguageLinkLocator = By.xpath("//div[text()='Norwegian']")
wait.until(elementToBeClickable(norwegianLanguageLinkLocator)).click()
println languageButton.text
browser.findElementById('source').sendKeys('ost')
def resultTextBoxLocator = By.cssSelector('#result_box span')
def result_box = wait.until(textToBePopulatedInElement(resultTextBoxLocator))
println "Result text: \"${result_box.text}\""
browser.quit()
public static ExpectedCondition<WebElement> textToBePopulatedInElement(
final By locator) {
return new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
try {
WebElement element = findElement(locator, driver)
if (!element.getText().empty) {
return element
}
} catch (StaleElementReferenceException ignored) {
return null;
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment