如何使用 Selenium WebDriver 以 Java 語言對 Gmail 登入流程實現自動化?


我們可以在 Java 中使用 Selenium Webdriver 對 Gmail 登入流程進行自動化。要執行此任務,首先我們必須啟動 Gmail 登入頁面,然後使用 findElement 方法找到電子郵件、密碼和其他元素,再對它們執行操作。

讓我們來看看 Gmail 登入頁面 −

程式碼實現

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
public class GmailLogin{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
      "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://#/signin");
      //identify email
      WebElement l = driver
      .findElement(By.name("identifier"));
      l.sendKeys("abc@gmail.com");
      WebElement b = driver
      .findElement(By.className("VfPpkd-LgbsSe"));
      b.click();
      //identify password
      WebElement p = driver
      .findElement(By.name("password"));
      p.sendKeys("123456");
      b.click();
      //close browser
      driver.close();
   }
}

更新於: 25-Jun-2021

14K+ 瀏覽

開啟你的 職業生涯

完成課程以獲得認證

立即開始
廣告
© . All rights reserved.