无法在文本框中输入字符串。它只是打开浏览器

Unable to enter a string in the text box. It just opens the browser

我想打开网站 www.flock.co 并在文本中输入电子邮件 field.However 程序只打开网站,不会在字段中输入电子邮件。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FindByClassName {

    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.gecko.driver", "C:\Automation\geckodriver-v0.15.0-win64\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://flock.co/");

        driver.findElement(By.className("_g-s-wrap")).sendKeys("farzan.s@flock.co");
    }

}

_g-s-wrap 是包含更多功能(如 "Get Started" 按钮)的容器的 class。使用 cssSelector 定位 <input> 文本框

driver.findElement(By.cssSelector("[type='email']")).sendKeys("farzan.s@flock.co");

请使用以下代码:-

  WebElement ele1 = driver.findElement(By.xpath("//input[@type='email']"));
   ele1.sendKeys("farzan.s@flock.co");

您可以通过多种方式将电子邮件输入文本字段。

  1. 使用 ID
  2. 使用姓名
  3. xpath
  4. 使用 css 选择器

还有其他方法,但这些是非常可靠且经常使用的方法。

这些方法的语法是:

    driver.findElement(By.xpath(".//*[@id='main-area']/div[2]/div[2]/form/div/div[1]/input")).sendkeys("abc@gmail.com");

    driver.findElement(By.id("give the id of the editbox by inspecting element")).sendkeys("abc@gmail.com");

    driver.findElement(By.name("give the name of the editbox by inspecting element")).sendkeys("abc@gmail.com");

抱歉我贴错了请试试这个

driver.findElement(By.xpath="//*[@id="main-area"]/div[3]/div‌​[2]/form/div/div[1]/‌​input").sendkeys("ex‌​ample@example.com");