13

enter image description here I am using Selenium 2. But after running following code, i could not able to type in textbox.

    package Actor;
import org.openqa.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.junit.*;
import com.thoughtworks.selenium.*;
//import org.junit.Before;
public class Actor {
  public Selenium selenium;
  public WebDriver driver;

  @Before
  public void setup() throws Exception{
  driver = new FirefoxDriver();
      driver.get("http://www.fb.com");
  }
  @Test
  public void Test() throws Exception{
      //selenium.type("id=gs_htif0", "test");
      System.out.println("hi");
      // driver.findElement(By.cssSelector("#gb_1 > span.gbts")).click();
          selenium.waitForPageToLoad("300000000");

          WebElement email=driver.findElement(By.id("email"));

          email.sendKeys("[email protected]");
          driver.findElement(By.id("u_0_b")).click();
  }
  @After
  public void Close() throws Exception{
      System.out.println("how are you?");
  }

}
6
  • What goes wrong? Does it throw an exception - if so what, and from which line? Commented May 10, 2013 at 11:49
  • yes it's throwing java.lang.NullPointerException, and i could not see any thing happening in webdriver's firefox browser Commented May 10, 2013 at 11:56
  • Can you paste the full error info into your question? Commented May 10, 2013 at 12:12
  • 3
    Why are you mixing up Selenium-RC with Selenium WebDriver? I'd go WebDriver full way. Commented May 10, 2013 at 13:12
  • Since your edit, you "selenium" variable isn't started, that could pull of a NullPointerException, also you don't need it, since WebDriver waits for the page to load before doing any action. Commented May 10, 2013 at 14:31

6 Answers 6

18

This is simple if you only use Selenium WebDriver, and forget the usage of Selenium-RC. I'd go like this.

WebDriver driver = new FirefoxDriver();
WebElement email = driver.findElement(By.id("email"));
email.sendKeys("[email protected]");

The reason for NullPointerException however is that your variable driver has never been started, you start FirefoxDriver in a variable wb thas is never being used.

Sign up to request clarification or add additional context in comments.

6 Comments

i am new on selenium and learning form web. can you customize me code?
I could, but that's not the point of learning. The answer is right there, all you need to do is imply that in your code. Also i would (once again) advise you to drop SeleniumRC, as it's outdated.
If someone disagrees with me, feel free to edit my code and suit the user needs.
Now i edit my code according to you but it still not working.
Are you still getting NullPointerException? Which line?
|
8

Thanks Friend, i got an answer. This is only possible because of your help. you all give me a ray of hope towards resolving this problem.

Here is the code:

package facebook;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Facebook {
    public static void main(String args[]){
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.facebook.com");
        WebElement email= driver.findElement(By.id("email"));
        Actions builder = new Actions(driver);
        Actions seriesOfActions = builder.moveToElement(email).click().sendKeys(email, "[email protected]");
        seriesOfActions.perform();
        WebElement pass = driver.findElement(By.id("pass"));
        WebElement login =driver.findElement(By.id("u_0_b"));
        Actions seriesOfAction = builder.moveToElement(pass).click().sendKeys(pass, "naveench").click(login);
        seriesOfAction.perform();
        driver.
    }    
}

Comments

5

You should replace WebDriver wb = new FirefoxDriver(); with driver = new FirefoxDriver(); in your @Before Annotation.

As you are accessing driver object with null or you can make wb reference variable as global variable.

2 Comments

Thanks Omkar i resolved this problem. if you like me answer please upvote this.
i can upvote your answer. It did not resolved my entire problem so, i did not accept it sorry...
1

Try this :

    driver.findElement(By.id("email")).clear(); 
driver.findElement(By.id("email")).sendKeys("[email protected]");

Comments

0

Another way to solve this using xpath

WebDriver driver =  new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath(//*[@id='email'])).sendKeys("[email protected]");

Hope that will help. :)

Comments

0

You can use JavaScript as well, in case the textfield is dithered.

WebDriver driver=new FirefoxDriver();
driver.get("http://localhost/login.do");
driver.manage().window().maximize();
RemoteWebDriver r=(RemoteWebDriver) driver;
String s1="document.getElementById('username').value='admin'";
r.executeScript(s1);

Comments

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.