0

While running the following code:

package prac.sample;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;


public class SelTestDemo {

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

    WebDriver test1 = new FirefoxDriver();
    test1.get("http://www.gmail.com");
    test1.manage().window().maximize();
    WebElement email = test1.findElement(By.id("Email"));
    email.sendKeys("srinivaspavan@gmail.com");
    WebElement next = test1.findElement(By.id("next"));
    next.click();
    WebElement password = test1.findElement(By.ById.id("Passwd"));
    password.click();
    password.sendKeys("tftctctc141414");
    WebElement signin = test1.findElement(By.id("signIn"));
    signin.click();
}

}

I see the following error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException:    
Unable to locate element: {"method":"id","selector":"Passwd"}
Command duration or timeout: 19 milliseconds
For documentation on this error, please visit: 
http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09  19:55:52'

Please help.

  • This works for me with Webdriver version 2.49 but one Q. Why do you need to do a password.click(); before typing the password? – ilm Feb 3 '16 at 4:44
  • 1
    Maybe do not post passwords in the code! :) – Niels van Reijmersdal Feb 3 '16 at 12:08
1

This happened because Selenium script was not able to find the element in the time duration mentioned.

This happens when your page is not loaded inside the time frame that Selenium has set for locating elements.

The best approach to handle this is to use the waits( ). You can use either implicit or explicit wait- the latter is preferable.

eg- If you use implicit wait, then it will wait for the duration for each element. You can do something like this

  driver.manage().timeouts().implicitylyWait(30, TimeUnit.SECONDS);

Other wise, you can use the explicit wait condition and then wait for desired element to be present or load and then carry on with your test. Something on the lines of-

WebDriverWait wait1 = new WebDriverWait();

Webelement ele = wait1.until(ExpectedConditions.elementToBeClickable(locator));

The elementToBeClickable() can be any one of the different conditions allowed by the expected conditions class.

Read this for a better understanding- link

0

You have written a wrong code for finding an password element

Your code is -

WebElement password = test1.findElement(By.ById.id("Passwd"));
password.click();
password.sendKeys("tftctctc141414");

And it should be as -

WebElement password = test1.findElement(By.id("Passwd"));
password.click();
password.sendKeys("tftctctc141414");

Mistake - you are using By.ById.id method

  • This actually does not answer the question and won't solve the problem. By.ById.id() is actually running By.id() static method, because By.ById inner class extends By abstract class and thus inherits id() method. I agree, writing By.ById.id() makes things less readable but it is not a reason of the problem. – dzieciou Mar 13 '16 at 10:59
-1

This issue is mainly because script is not giving time to load page. You have to use Thread.sleep(ms)

I have solved your problem and giving solution below.

Solution :

import java.util.concurrent.TimeUnit;  
import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.WebElement;  
import org.openqa.selenium.firefox.FirefoxDriver;  

public class SelTestDemo
{

public static void main(String[] args)
{

    try
    {

        // TODO Auto-generated method stub

        WebDriver test1 = new FirefoxDriver();
        test1.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        test1.get("http://www.gmail.com");
        test1.manage().window().maximize();
        WebElement email = test1.findElement(By.id("Email"));
        email.sendKeys("srinivaspavan@gmail.com");
        WebElement next = test1.findElement(By.id("next"));
        next.click();
        WebElement password = test1.findElement(By.id("Passwd"));
        //password.click(); no need to click here
        Thread.sleep(3000); // or you can use thread.sleep and wait.until(ExpectedConditions.elementToBeClickable(locator)); both
        password.sendKeys("12raopavan#");
        WebElement signin = test1.findElement(By.id("signIn"));
        signin.click();
    }
    catch (InterruptedException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} 
}

Let me know this solved your problem.

  • Using thread.sleep is considered a bad practice. Instead the use of Explicit or Implicit wait is recommended. – demouser123 Feb 8 '16 at 7:07
  • I do not think so it is bad practice at all. I have seen many web project when I must use thread.sleep. As per standard it is last option to pause webdriver process. – Sagar007 Feb 8 '16 at 8:43
  • blogs.msmvps.com/peterritchie/2007/04/26/… Please read this. – demouser123 Feb 8 '16 at 10:59
  • 1
    Then increase the waiting time. I have used up to 5000 seconds in my scripts for waiting. Using thread.sleep has many bad repercussions. Most of the waiting scenarios can be solved by waits. – demouser123 Feb 9 '16 at 7:47
  • 1
    Despite your defense I agree with log_file. Avoid fixed waits – Michael Durrant Mar 11 '16 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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