Example of using pageObjects to grab elements:
public class Grabber {
/*
* There exists a plugin in firefox to right click an element, inspect it, then right clicking the element
* and copying the xpath and pasting it here.
*/
private static WebElement element = null;
public static WebElement input_box(WebDriver driver, WebDriverWait wait) {
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("XPATH HERE")));
//Used if element is a button or needs to bet clicked
//wait.until(ExpectedConditions.elementToBeClickable(By.xpath("XPATH HERE")));
element = driver.findElement(By.xpath("XPATH HERE"));
return element;
}
}
How to use it :
EDIT: Initialize, NavigateTo, and Dispose will give you an error since they must be static, I wrote this quickly to give an example and you should edit it as you see fit to get what you want working. I hope I pointed you the right direction to solving your problem.
EDIT: The dispose here is to get rid of the driver when it is complete or an exception has been thrown. To remove the Temp files that are left out.
public class Test {
private WebDriver driver;
private WebDriverWait wait;
public static void main(String[] args) {
try {
initialize();
navigateTo("www.somewhere.com");
Grabber.input_box(driver, wait).sendKeys("I want to send these keys");
} catch (Exception e) {
e.printStackTrace();
} finally {
dispose();
}
}
private void initialize() {
driver = new FirefoxDriver();
wait = new WebDriverWait(driver, 15);
}
private void navigateTo(String url) {
driver.get(url);
}
private void dispose() {
RemoteWebDriver cRDriver = (RemoteWebDriver) driver;
while (cRDriver.getSessionId() != null) {
driver.quit();
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…