Tuesday, July 26, 2011

Unable to locate element in Selenium 2

There is new version of Selenium, which is very good and easy to use, unfortunately there is too little documentation yet. After I started to use it, one of the first issues I found is this:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Home"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.2.0', revision: '13073', time: '2011-07-26 00:54:48'
System info: os.name: 'Windows 7', os.arch: 'amd64', 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(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        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:405)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:193)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:218)
        at org.openqa.selenium.By$ByLinkText.findElement(By.java:223)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:185)
        at org.openqa.selenium.WebDriver$findElement.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
        at selenium.run(selenium.groovy:14)
        at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:266)
        at groovy.lang.GroovyShell.run(GroovyShell.java:229)
        at groovy.lang.GroovyShell.run(GroovyShell.java:159)
        at groovy.ui.GroovyMain.processOnce(GroovyMain.java:514)
        at groovy.ui.GroovyMain.run(GroovyMain.java:329)
        at groovy.ui.GroovyMain.process(GroovyMain.java:315)
        at groovy.ui.GroovyMain.processArgs(GroovyMain.java:112)
        at groovy.ui.GroovyMain.main(GroovyMain.java:93)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
        at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"link text","selector":"Home"}
Build info: version: '2.2.0', revision: '13073', time: '2011-07-26 00:54:48'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_24'
Driver info: driver.version: unknown

even when element is present on the page. After some googling I found easy solution to this issue:

    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

This is default period which Selenium will try to wait for element to appear on the page, before throwing error, and this works perfectly for me.


4 comments:

  1. Hi,goys i have done the code for update the naukri.com in selenium rc and now i am trying to write the code in selenium web driver same senario, but i can't run the code.
    any body help me.
    This is my selenium Rc code.so, help me in selenium webdriver
    package Selenium;


    import com.thoughtworks.selenium.*;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;


    public class Sample {

    Selenium selenium;
    @Before
    public void setUp() throws Exception {
    selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.naukri.com/");
    selenium.start();
    }

    @Test
    public void testNrc() throws Exception {
    try{
    selenium.open("/");
    }
    catch (Exception e) {
    // TODO: handle exception
    //selenium.waitForPageToLoad("30000");
    //selenium.setTimeout("0");
    e.printStackTrace();
    Thread.sleep(30000);
    }

    try{

    selenium.type("id=username", "Enter your email");
    selenium.type("id=password", "Enter your password");
    selenium.click("name=Login");
    System.out.println("suri1");

    }
    catch (Exception e) {
    // TODO: handle exception
    selenium.setTimeout("0");

    }

    try{
    Thread.sleep(30000);
    selenium.click("css=button.w205");
    System.out.println("suri2");

    }
    catch (Exception e) {
    // TODO: handle exception
    //selenium.setTimeout("0");
    e.printStackTrace();
    }

    try{
    Thread.sleep(30000);
    selenium.click("link=Edit");
    System.out.println("suri3");

    }
    catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    //selenium.setTimeout("0");

    }
    try{
    Thread.sleep(10000);

    selenium.click("css=button.w150bt.fl");
    System.out.println("suri4");

    }
    catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    //selenium.setTimeout("300000");

    }
    try{
    Thread.sleep(10000);
    selenium.click("link=Logout");
    System.out.println("suri5");

    }
    catch (Exception e) {
    // TODO: handle exception
    //selenium.setTimeout("300000");
    e.printStackTrace();
    }

    }

    @After
    public void tearDown() throws Exception {
    //selenium.stop();
    }
    }

    ReplyDelete
  2. The Information shared was Really helpful My sincere thanks for sharing this post Please Continue to share this post
    Selenium Training in Bangalore
    Selenium Training in BTM Layout

    ReplyDelete
  3. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any python Related information please visit our website Python Training In Bangalore page!

    ReplyDelete