如何用 Python 在 Selenium 中清除输入的文本?
我们可以在 Selenium 的任何字段上输入文本。输入文本后,可能需要删除或清除在该字段中输入的文本。clear() 方法有助于与这些 Web 元素进行交互。
因此,clear() 方法用于清除或重置属于表单/编辑框的输入字段。它替换了页面上该特定元素上的文本内容。
语法
driver.find_element_by_xpath("//input[class ='gsc-search']").clear()示例
使用 clear() 方法实现代码。
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/current_affairs.htm")
#to refresh the browser
driver.refresh()
# identifying the edit box and entering text in edit box
driver.find_element_by_xpath("//*[@class='gsc-input']"). send_keys("Selenium")
# clears the content
driver.find_element_by_xpath("//*[@class='gsc-input']").clear()
#to close the browser
driver.close
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP