Selenium Webdriver submit() 與 click()。
click() 函式和 submit() 函式在功能上非常相似。但仍有一些細微差別。我們來討論一下它們之間的一些區別。
submit() 函式僅適用於 <form>,並且可以更容易地處理表單。它可以與表單內的任何元素一起使用。click() 僅適用於表單中帶有 submit 型別按鈕。
submit() 函式將等到頁面載入,但 click() 僅在提供了顯式等待條件時才等待。如果表單具有按鈕型別的 submit,則無法使用 submit() 方法。而且,如果按鈕位於 <form> 外部,那麼 submit() 將不起作用。
因此,我們看到 click() 適用於兩種型別的按鈕,無論按鈕位於 <form> 內部還是外部。我們以以下表單為例進行實現。

示例
使用 submit() 的程式碼實現。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class SubmitForm{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://#/";
driver.get(url);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// identify elements
driver.findElement(By.id("email")).sendKeys("abc@gmail.com");
driver.findElement(By.id("pass")).sendKeys("123456");
// submitting form with submit()
driver.findElement(By.id("pass")).submit();
driver.quit()
}
}示例
使用 click() 的程式碼實現。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class ClickForm{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://#/";
driver.get(url);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// identify elements
driver.findElement(By.id("email")).sendKeys("abc@gmail.com");
driver.findElement(By.id("pass")).sendKeys("123456");
// submitting form with click()
driver.findElement(By.name("login")).click();
driver.quit()
}
}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP