2016-08-02 29 views
1

在Google和StackOverflow上做了很多R & D後發佈此查詢.. 我想在Facebook上輸入用戶名和密碼,只使​​用類名,我不用要使用的類型,名稱,ID爲這個..我試圖與所有可能的選擇,但沒有運氣能有人提供這種解決方案.. 這裏是我的代碼..如何在Facebook中使用類名稱輸入用戶名和密碼

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

    } 

} 

回答

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比其他定位器快得多。所以如果可能的話,先優先考慮。

希望它能幫助... :)

+0

感謝您的信息SAURABH,但我不希望使用的ID或名字,我想利用唯一的類名稱... –

+0

@SantoshGaddam更新與類的答案。試試吧.. :) –

+0

@SantoshGaddam但我建議你在這裏嘗試使用'By.id',因爲它比使用其他定位器要快得多... :)。 –

相關問題