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();
}
}
感謝您的信息SAURABH,但我不希望使用的ID或名字,我想利用唯一的類名稱... –
@SantoshGaddam更新與類的答案。試試吧.. :) –
@SantoshGaddam但我建議你在這裏嘗試使用'By.id',因爲它比使用其他定位器要快得多... :)。 –