在 Selenium 中使用 python 有哪些提交表单的方式?
在 Selenium 中有多种提交表单的方式。一种方法是直接在表单提交按钮上使用 click() 方法。另一种方法是在表单页面上使用 submit() 方法。
使用 submit() 方法。
在表单页面上输入所需数据后,此方法将简单地提交该表单。
语法 −
driver.find_element_by_xpath("//input[class ='gsc-search']").submit()使用 click() 方法。
在表单页面上输入所需数据后,此方法将单击表单的提交按钮。
语法 −
driver.find_element_by_xpath("//button[id ='value']").click()举例
使用 submit() 方法进行的代码实现。
from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://tutorialspoint.com/index.htm")
#to refresh the browser
driver.refresh()
# identifying the edit box with the help of id and enter text
driver.find_element_by_id("gsc-i-id1").send_keys("Selenium")
# submit the text contents
driver.find_element_by_id("gsc-i-id1").submit()
#to close the browser
driver.close()使用 click() 方法进行表单提交的代码实现。
from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://tutorialspoint.com/index.htm")
#to refresh the browser
driver.refresh()
# identifying the edit box with the help of id and enter text
driver.find_element_by_id("gsc-i-id1").send_keys("Selenium")
# identifying the button then using click() method
driver.find_element_by_xpath("//button[contains(@class,'gsc-search')]") .click()
#to close the browser
driver.close()
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP