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?"); } }
This question is related to
java
selenium
testing
selenium-webdriver
automation
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.
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);
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. :)
Try this :
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("[email protected]");
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.
Source: Stackoverflow.com