How to enter text box using Selenium WebDriver (Selenium 2) with Java?

enter image description here I am using Selenium 2 . But after running the following code, I was not able to enter a text box.

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(" nshakuntalas@gmail.com "); driver.findElement(By.id("u_0_b")).click(); } @After public void Close() throws Exception{ System.out.println("how are you?"); } } 
+7
source share
6 answers

Thanks, friend, I got a response. This is only possible because of your help. you all give me hope for a solution to 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, " gati.naveen@gmail.com "); 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. } } 
+7
source

It is simple if you use only Selenium WebDriver and forget to use Selenium-RC. I would go like that.

 WebDriver driver = new FirefoxDriver(); WebElement email = driver.findElement(By.id("email")); email.sendKeys(" your@email.here "); 

However, the reason for NullPointerException is because your driver variable has never been run, you are running FirefoxDriver in a wb variable that is never used.

+12
source

You must replace WebDriver wb = new FirefoxDriver(); on driver = new FirefoxDriver(); in your @Before annotation.

When you access the driver object with a null value or you can make the wb reference variable as a global variable.

+5
source

Try the following:

  driver.findElement(By.id("email")).clear(); driver.findElement(By.id("email")).sendKeys(" emal@gmail.com "); 
0
source

Another way to solve this problem is with 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(" your@email.here "); 

Hope this helps. :)

0
source

You can also use JavaScript if the text field fades.

 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); 
0
source

All Articles