我们可以通过以下方法在Selenium中提交表单-
提交()
click()
submit()
方法的代码实现。
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class SubmitScripting { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "<<web url with a form submit feature>>"; driver.get(url); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //提交表单的方法 driver.findElement(By.id("<<id expression>>")).submit(); driver.close(); } }
用方法实现代码click()
。
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class ClickScripting { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "<<web url with a form submit feature>>"; driver.get(url); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //点击提交表格的方法 driver.findElement(By.id("<<id expression>>")).click(); driver.close(); } }