2

The command driver.findElement(By.xpath("xpath"). click(); is not working in Web driver. Kindly check the below code written in selenium RC and webdriver.

Selenium RC :

public void courselibrary_click() throws Exception {

        selenium.open("/ksdlms/");
        //selenium.setSpeed(null);
         selenium.windowMaximize();
         selenium.windowFocus();

         selenium.type("name=userID", "asha");

            selenium.type("name=password", "welcome");
            selenium.click("id=loginbutton");
            //selenium.waitForPageToLoad("30000");
            Thread.sleep(30000);
            System.out.println("ok");

            selenium.click("xpath=html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td[1]/table/tbody/tr[283]/td[2]/a");
            System.out.println("ok ok");
}

Webdriver :

public void courselibrary_click() throws Exception {
          driver.get(baseurl);
        driver.manage().window().maximize();
        //driver.switchTo().window("KESDEE Learning Management System.");
        driver.findElement(By.name("userID")).sendKeys("asha");
        driver.findElement(By.name("password")).sendKeys("welcome");
        driver.findElement(By.id("loginbutton")).click();
        System.out.println("ok");
        Thread.sleep(3000);
        System.out.println("ok ok");
       // driver.implicitly_wait(300);
        //System.out.println("ok");
        **driver.findElement(By.xpath("html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td[1]/table/tbody/tr[283]/td[2]/a")).click();**
                                //selenium.click("xpath=html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td[1]/table/tbody/tr[6]/td[2]/a");
       //element.click();
        Thread.sleep(1000);
}

After login to the application, driver is unable to find an element and throwing exception:

FAILED: courselibrary_click
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td[1]/table/tbody/tr[283]/td[2]/a"}
Command duration or timeout: 44 milliseconds
  • Do you mean it works in RC and doesn't in Webdriver? Add snippet of HTML – olyv Jun 20 '14 at 11:07
  • 1
    All Se experts recommend to use id > name > link text > class > xpath. Can you use other, more preferred way to locate your element? – Peter M. Jun 20 '14 at 14:00
1

There are perhaps three reasons why this might fail:

  1. The page hasn't finished loading, so the element isn't there
  2. The page has finished loading, and the page simply doesn't contain the item -- perhaps because you spelled it wrong in the test or the developer spelled it wrong in the code
  3. The page contains embedded frames, and the element you seek is in an embedded frame.

Based on your comments to another anwer, it appears that the problem is 3 - the table was in an embedded frame. If that's the case, you need to switch to the frame before locating the element.

  • I would add another possible reason: 4. the xpath is fragile. – Mate Mrše Oct 18 '18 at 12:03
0

XPath locator should begin with //.

So try

    driver.findElement(By.xpath("html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td[1]/table/tbody/tr[283]/td[2]/a")).click();*
  • 2
    Tried with Xpath locator "//" also, but it was not working. After using the command driver.switchTo().frame(1); it started working. Thanks – user2787080 Jun 20 '14 at 12:07

protected by Community Sep 2 '16 at 15:46

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.