如何使用 class 个名字在 Facebook 中输入用户名和密码

How to enter username and password in Facebook using class names

在 google 和 Whosebug 上做了很多研发之后,我发布了这个查询.. 我只想使用 class 名称在 Facebook 上输入用户名和密码,我不想为此使用类型、名称、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();

   }

}

如果您想使用他们的 class 输入用户名和密码,请尝试使用以下 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 比其他定位器快得多。因此,如果可能,请将其放在首位。

希望对您有所帮助...:)