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

时间:2017-04-16 10:17:43

标签: selenium selenium-webdriver webdriver selenium-firefoxdriver firefox-driver

我想打开网站www.flock.co并在文本字段中输入一封电子邮件。但是,该程序只会打开该网站并且不会在该字段中输入该电子邮件。

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

}

4 个答案:

答案 0 :(得分:1)

_g-s-wrap是包含更多功能的容器类(如“Get Started”按钮)。使用cssSelector找到<input>文本框

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

答案 1 :(得分:1)

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

  1. 使用id
  2. 使用名称
  3. 的xpath
  4. 使用css选择器
  5. 还有其他方法,但这些是非常可靠和经常使用的方法。

    方法的语法为:

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

答案 2 :(得分:1)

抱歉我粘贴错了请试试这个

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

答案 3 :(得分:0)

请使用以下代码: -

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