When I run the following program, why is '0' printed to the console? I expected '1' to be printed since I expected the findElements()
method to find a link using the xpath. Is the xpath expression incorrect? I got the expression using Firefox, Firebug, and Firepath, by selecting the link element and copying the given xpath.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import java.util.List;
public class SeleniumSearch {
static WebDriver driver = new FirefoxDriver();
public static void main(String[] args) {
try {
driver.get("http://www.google.co.uk/");
submitSearch("selenium");
getHit();
}
finally {
driver.close();
}
}
static void submitSearch(String search) {
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys(search);
searchBox.submit();
}
static void getHit() {
List<WebElement> hits = driver.findElements(By.xpath("html/body/div[5]/div[4]/div[9]/div[1]/div[3]/div/div[3]/div[2]/div/div/div/div[2]/div[1]/div/h3/a"));
System.out.println(hits.size());
}
}