如何用 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.tw/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
廣告