java - 如何使用带有 Java 的 Selenium WebDriver (Selenium 2) 在文本框中键入内容?

标签 java selenium testing selenium-webdriver automation

enter image description here 我正在使用 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/

相关文章:

java - 如何通过我的应用程序通过 Intent 将视频数据共享/发送到 Tik-Tok 应用程序,就像画廊共享一样

java - JsonParseException : Invalid UTF-8 middle byte 0x2d

java - 为什么 getConfiguration 偶尔会抛出 File不能为空?

C# Webdriver - 页面标题断言在页面加载前失败

python 单元测试 assertRaises

java - 有没有办法复合拦截器

java - 使用 Selenium 迭代表中的链接列表 - 第一次迭代工作正常,然后失败

javascript - 使用 nightwatch.js 处理具有相同选择器的多个元素

java - JUnit 可以用作 Spring 中浏览器测试的替代品吗?

testing - Thread.Sleep() 和 selenium.setSpeed ("2000"之间有什么区别?