如何使用 Java 在 Selenium 中單擊註冊按鈕,我能開啟頁面,但無法單擊?
我們可以使用 Java 在 Selenium 中單擊註冊按鈕。首先,我們必須使用 id、類名、名稱、連結文字、xpath、css 或部分連結文字等任意定位器來識別該註冊按鈕。在識別之後,我們必須使用 click 方法來單擊註冊按鈕。
語法
WebElement m=driver. findElement(By.id("loc-txt"));
m.click();示例
使用 click 程式碼實現
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class SignIn{
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://www.linkedin.com/");
// identify element with class name then use click method
WebElement m=driver.
findElement(By.className("sign-in-form__submit-button"));
m.click();
driver.close();
}
}此外,我們還可以使用 sendKeys 方法單擊註冊按鈕,並傳遞 Keys.ENTER 作為該方法的引數。
語法
WebElement m=driver. findElement(By.id("loc-txt"));
m.sendKeys(Keys.ENTER);示例
使用 sendKeys 程式碼實現
import org.openqa.selenium.By;
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.Keys;
public class SignInSendKeys{
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://www.linkedin.com/");
// identify element with class name then use sendKeys method
WebElement m=driver.
findElement(By.className("sign-in-form__submit-button"));
m.sendKeys(Keys.ENTER);
driver.close();
}
}
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP