如何使用类名在Facebook中输入用户名和密码

时间:2016-08-02 13:03:56

标签: selenium login webdriver classname

在谷歌和StackOverflow上做了很多研发后我发布了这个查询.. 我想在Facebook上使用ONLY类名输入用户名和密码,我不想使用类型,名称,id为此...我尝试了所有可能的选项但没有运气可以有人为此提供解决方案.. 这是我的代码..

package login;

import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

import org.testng.annotations.*;

import org.openqa.selenium.*;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class MercuryLogin1 {

  private WebDriver driver;

  private String baseUrl;

  @BeforeClass(alwaysRun = true)

  public void setUp() throws Exception {

    System.setProperty("webdriver.chrome.driver","C:\\Automation_Info\\Selenium\\chromedriver_win32\\chromedriver.exe");

    System.setProperty("webdriver.gecko.driver", "C:\\Automation_Info\\Selenium\\geckodriver-v0.9.0-win64\\geckodriver.exe");

    driver = new ChromeDriver();

    baseUrl = "http://facebook.com/";   

    //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  }

  @Test

  public void testMercuryLogin1() throws Exception {

    driver.get(baseUrl + "/");

    driver.findElement(By.xpath("//input[@class=inputtext][0]")).sendKeys("tutorial");

    driver.findElement(By.xpath("//input[@class='nputtext][1]")).sendKeys("tutorial");

  }

  @AfterClass(alwaysRun = true)

  public void tearDown() throws Exception {

    //driver.quit();

   }

}

1 个答案:

答案 0 :(得分:0)

如果您想使用他们的班级输入用户名和密码,请尝试使用以下By.xpath(): -

public void testMercuryLogin1() throws Exception {

    driver.get(baseUrl + "/");

    WebDriverWait wait = new WebDriverWait(driver, 10);

    WebElement user = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(.//input[@class = 'inputtext'])[1]")))

    user.sendKeys("tutorial");

    WebElement pass = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(.//input[@class = 'inputtext'])[2]")))

    pass.sendKeys("tutorial");

  }

注意: - 我不确定您为什么要使用class Name输入值,而只需使用By.id("email")By.id("pass")输入即可。我建议你从性能角度使用By.id比其他定位器快得多。因此,如果可能的话,首先将其优先考虑。

希望它有帮助...... :)