如何使用带有 Java 的 Selenium WebDriver (Selenium 2) 在文本框中输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16481418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:06:47  来源:igfitidea点击:

How to type in textbox using Selenium WebDriver (Selenium 2) with Java?

javaseleniumtestingselenium-webdriverautomation

提问by Naveen Chhaniwal

enter image description hereI am using Selenium 2. But after running following code, i could not able to type in textbox.

在此处输入图片说明我正在使用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("[email protected]");
          driver.findElement(By.id("u_0_b")).click();
  }
  @After
  public void Close() throws Exception{
      System.out.println("how are you?");
  }

}
    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?");
  }

}

采纳答案by Naveen Chhaniwal

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.
    }    
}

回答by Omkar

You should replace WebDriver wb = new FirefoxDriver();with driver = new FirefoxDriver();in your @BeforeAnnotation.

您应该在注释中替换WebDriver wb = new FirefoxDriver();为。driver = new FirefoxDriver();@Before

As you are accessing driverobject with null or you can make wbreference variable as global variable.

当您driver使用 null访问对象时,或者您可以将wb引用变量设为全局变量。

回答by aimbire

This is simple if you only use Selenium WebDriver, and forget the usage of Selenium-RC. I'd go like this.

如果您只使用 Selenium WebDriver 而忘记使用 Selenium-RC,这很简单。我会这样。

WebDriver driver = new FirefoxDriver();
WebElement email = driver.findElement(By.id("email"));
email.sendKeys("[email protected]");

The reason for NullPointerExceptionhowever is that your variable driverhas never been started, you start FirefoxDriverin a variable wbthas is never being used.

NullPointerException然而,原因是您的变量driver从未启动过,您从FirefoxDriver一个wb从未使用过的变量开始。

回答by Julien P.

Try this :

尝试这个 :

    driver.findElement(By.id("email")).clear(); 
driver.findElement(By.id("email")).sendKeys("[email protected]");

回答by Kumrun Nahar Keya

Another way to solve this using xpath

使用 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. :)

希望这会有所帮助。:)

回答by Vikas

You can use JavaScript as well, in case the textfield is dithered.

您也可以使用 JavaScript,以防文本字段抖动。

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