2

How to use $x("//input[@id='searchInput']") xpath in webdriver as it gives error to complete statement?

import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Locaters {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        FirefoxDriver driver = new FirefoxDriver();

        driver.get("http://www.wikipedia.org/");
    //getting error with this xpath,so how can we work with these type of statements
        driver.findElement(By.xpath("$x("//input[@id='searchInput']")"));
    }
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
David
  • 21
  • 1
  • 2

2 Answers2

2

Not familiar with Selenium, but

$x("//input[@id='searchInput']")

is not a valid XPath expression. One without $ is:

driver.findElement(By.xpath("//input[@id='searchInput']"));

The documentation also suggests there is no such thing as an XPath expression with $ in Selenium.

EDIT: As suggested by @Arran, it is very likely that the confusion stems from the way XPath expressions can be used in the Chrome Console.

Community
  • 1
  • 1
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
  • 1
    You are right and I suspect his confusion is merely because you can execute XPath queries within Chrome's dev tools by doing `$x(query)`. It seems obvious he may have not known the extra `$x` is specific to Chrome. – Arran Jan 04 '15 at 22:43
  • @Arran Ah, that makes a lot of sense. Thanks for pointing it out! – Mathias Müller Jan 04 '15 at 22:46
1

just give copied xpath

$x("//*[@id='searchInput']")
bb1301
  • 36
  • 1