我正在使用 Selenium 2。 但是在运行以下代码后,我无法在文本框中输入。
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?"); } }
最佳答案
如果您只使用 Selenium WebDriver 而忘记 Selenium-RC 的用法,这很简单。我会这样走。
WebDriver driver = new FirefoxDriver();
WebElement email = driver.findElement(By.id("email"));
email.sendKeys("your@email.here");
NullPointerException
的原因然而,你的变量是driver
一直没启动,你启动FirefoxDriver
在变量中 wb
从未使用过。
关于java - 如何使用带有 Java 的 Selenium WebDriver (Selenium 2) 在文本框中键入内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16481418/