2

The following is the snippet of WebDriver code using Java:

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.pl/");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
        WebElement element = driver.findElement(By.name("q")); 
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement query = driver.findElement(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input"));

query.sendKeys("asd");

After execution of code I got the following exception:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input"} System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_24' Driver info: driver.version: RemoteWebDriver at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:409) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:192) at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:265) at org.openqa.selenium.By$6.findElement(By.java:205) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:184) at test.main(test.java:24)

What's the wrong in my code?

5
  • 1
    The XAPTH expression doesn't evaluate to an existing element, thats all. You have to change the expression. Commented Apr 28, 2011 at 10:43
  • What is the xpath supposed to show on that site? Commented Apr 28, 2011 at 10:59
  • I am testing a web application and I can identify elements by xpath only. So I try to do example on google. In both cases it doesnt work. That xpath is to that input where you type words and then click search. Could you tell me to waht expression should I change above one ? Commented Apr 28, 2011 at 12:59
  • Depends on your HTML, you have provided an invalid xpath, there is no problem with Selenium here. It is working exactly as it should do. Commented May 9, 2011 at 13:22
  • Your xpath is too long (absolute xpath) which is bad practice. You should try with short xpath (relative xpath). CssSelector is more faster than xpath. And it is better to use id or name rather than xpath. Is there any id or name in your HTML code? If yes, please use ID or Name Commented Jan 24, 2013 at 8:32

6 Answers 6

5

Your xpath expression:

WebElement query = driver.findElement(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input"));

looks correct but if you still are facing the issue please check the correctness of xpath again. If it fails again increase the time for Wait as:

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

or you can use explicit wait for the specific element as below:

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input")));
Sign up to request clarification or add additional context in comments.

2 Comments

I used a slightly different syntax. WebElement email = webdriver.findElement(By.xpath("//input[@id='signin_username'][2]")); It's not finding the [2] though
If it fails after using explicit wait, please recheck the correctness of xpath. Or, you can use id or name instead of xpath
3

Since you want the Polish Google site the

    //input[@title='Google Search']

will not work for you. Instead use

    //input[@title='Szukaj w Google']

Comments

2

@user729076: The xpath "//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input" you wrote for google text field is not right. The HTML for google text field is as follows:

<input type="text" value="" autocomplete="off" name="q" class="gbqfif" id="gbqfq" style="border: medium none; padding: 0pt; margin: 0pt; height: auto; width: 100%; background: url(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) repeat scroll 0% 0% transparent; position: absolute; z-index: 6; left: 0px; outline: medium none;" dir="ltr" spellcheck="false">

Based on the above HTML, you can use simply id or xpath as below: By id:

driver.findElement(By.id("gbqfq")).sendKeys("some text");

By xpath:

driver.findElement(By.xpath("//input[@id='gbqfq']")).sendKeys("some text");

Comments

2

In this case the XPath expression you want is:

//html/body/center/form/table/tbody/tr/td[2]/div/input

Or you could use this (a little more intuitive):

//input[@title='Google Search']

Keep in mind that if you will be identifying a lot of elements by XPath it would be advisable to become fluent in XPath, you could start here: Xpath Tutorial

In the meantime, use Firefox and install the following plugins:

Firebug

FirePath or Firefinder

These will help you easily identify valid XPath expressions to use for your website.

Comments

0

If you want to find elements by XPath. Then do the following:

WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.google.com";
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("http://www.google.com");
selenium.isElementPresent(XPath Variable);

Also find more help on this site

1 Comment

There's really no reason to fall back to old Selenium RC if OP writes his tests in the newer WebDriver.
0

The XPath Used is Incorrect Here Directly Id is there so no need to use XPath.

driver.findElement(By.id("gbqfq")).sendKeys("xyz");

2 Comments

The element to be find may not have any Id and it it may not be appropriate to add it only for the Selenium tests.
Anyway, thanks for contributing to StackOverflow :). The question you are answering is old and already answered, though - we'll be glad if you can give your effort to more pressing questions too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.