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.


5 comments:

  1. Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete
  2. 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
  3. 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
  4. 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