How to type in textbox using Selenium WebDriver (Selenium 2) with Java?
Solution 1
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.
}
}
Solution 2
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.
Solution 3
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.
Solution 4
Try this :
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("[email protected]");
Naveen Chhaniwal
Naveen Chhaniwal , started his engineering education from 15 September 2008 with highly perfection and strategic passion of learning environment of Marwar Engineering College & Research Center. During Technical education he completes his 45 Days training from the Core infocom Pvt. Ltd. Jaipur , India. And Graduated in Computer Science in 2012. After it as my First Job I Joined Dev Technosys Pvt. Ltd. as Software Quality Analyst on 24 June 2012. Here I have a new learning environment with more energy and enthusiasm. I worked on new and old Web & Mobile Projects of .net, LAMP, ios & android plateform which currently running in the company with a highly energetic team , which makes knowledge in that field more efficiently. Now i am working with Neerja Software Pvt. Ltd. as Software Quality Analyst from jun 2013. Here i am working on different new and old Web Projects of Drupal platform Environment which currently running in the company.
Updated on July 05, 2022Comments
-
Naveen Chhaniwal almost 2 years
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?"); } }
-
Naveen Chhaniwal almost 11 yearsi am new on selenium and learning form web. can you customize me code?
-
aimbire almost 11 yearsI 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.
-
aimbire almost 11 yearsIf someone disagrees with me, feel free to edit my code and suit the user needs.
-
Naveen Chhaniwal almost 11 yearsNow i edit my code according to you but it still not working.
-
aimbire almost 11 yearsAre you still getting NullPointerException? Which line?
-
Naveen Chhaniwal almost 11 yearsthanks Aimbire, you give a way to solve my problem. i resolved this problem.
-
Naveen Chhaniwal almost 11 yearsThanks Omkar i resolved this problem. if you like me answer please upvote this.
-
Naveen Chhaniwal almost 11 yearsi can upvote your answer. It did not resolved my entire problem so, i did not accept it sorry...