有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

enter image description here 我正在使用硒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?");
  }

}

共 (2) 个答案

  1. # 1 楼答案

    另一种解决方法是使用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("your@email.here");
    

    希望这会有所帮助。:)

  2. # 2 楼答案

    谢谢朋友,我得到了答案。这只有在你的帮助下才有可能。你们给了我解决这个问题的一线希望

    以下是代码:

    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, "gati.naveen@gmail.com");
            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.
        }    
    }