【问题标题】:How to enter username and password in Facebook using class names如何使用类名在 Facebook 中输入用户名和密码
【发布时间】:2016-12-07 20:32:02
【问题描述】:

在 google 和 StackOverflow 上进行大量研发后,我发布了这个查询.. 我想只使用类名在 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();

   }

}

【问题讨论】:

    标签: selenium login webdriver classname


    【解决方案1】:

    如果您想使用他们的类输入用户名和密码,请尝试使用下面的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 比其他定位器快得多。因此,如果可能,请优先考虑。

    希望对你有帮助...:)

    【讨论】:

    • 感谢 Saurabh 提供的信息,但我不想使用 id 或名称,我只想使用 CLASS 名称...
    • @SantoshGaddam 用班级更新了答案。试试看..:)
    • @SantoshGaddam 但我建议您在这里尝试使用By.id,因为它比使用其他定位器要快得多...:)。
    猜你喜欢
    • 2016-06-25
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    相关资源
    最近更新 更多