0

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());
    }
}

Screen shot of the xpath given by Firepath

sophros
  • 14,672
  • 11
  • 46
  • 75
danger mouse
  • 1,457
  • 1
  • 18
  • 31
  • 1
    What is supposed to be returned by the XPath? – Florent B. May 24 '16 at 13:15
  • I want the XPath to return the web element of the first google search result link. (I would then expand the program to click on the web element.) – danger mouse May 24 '16 at 13:20
  • 1
    use html/body/div[1]/div[5]/div[4]/div[7]/div[1]/div[3]/div/div[3]/div[2]/div/div/div/div[2]/div[1]/div/h3/a this xpath uor xpath do not looks ok to me ,above xpath is also given by firebug – Rajnish Kumar May 24 '16 at 13:25
  • 1
    Using a Css selector would be simpler: By.cssSelector(".r"). Note that you need to wait for the first element to be present since the content is dynamic. – Florent B. May 24 '16 at 13:28
  • Thanks Florent B. I'm not as familiar with cssSelectors yet but this solution looks much simpler too. – danger mouse May 24 '16 at 18:07

4 Answers4

2

Try putting the following as xpath instead of the actual path: //*[@id="rso"]/div[2]/div[1]/div/h3/a

1
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")

That's wrong work with xpath, one little change on website and your code wouldn't work! try to do it more dynamic find the closest id or tag name and continue from there, can you share your html source?

Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
  • Thanks for the tip Leon Barkan. There's a lot of html for that google page so I haven't included it, but I have added a screenshot of the Firebug tool (including the Firepath tool) to show where I get the xpath. – danger mouse May 24 '16 at 18:04
1

I would use a simple xpath like html/body//h3/a. You can also use FirePath extension of FireBug to build and evaluate xpaths.

stan
  • 984
  • 10
  • 15
1

Simplest xpath I could come up with for first link in google search:

(//h3/a)[1]
Richard
  • 8,961
  • 3
  • 38
  • 47