如何在 Selenium 中提交表单?
我们可以通过以下方法在 Selenium 中提交表单 −
submit()
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); // submit method to submit the form 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); // click method to submit the form driver.findElement(By.id("<<id expression>>")).click(); driver.close(); } }
广告