如何在 Python 中的 Selenium 中輸入編輯框的值?
我們可以藉助下面列出的方法在 Selenium 中輸入編輯框的值 -
使用 send_keys 方法。
此方法可以透過利用鍵類向編輯框傳送任何文字或按下鍵。
使用 Javascript 執行程式。
Javascript 文件物件模型可以處理頁面上的任何元素。Javascript 運行於客戶端,對網頁執行操作。Selenium 能夠藉助 execute_script() 方法執行 Javascript 指令碼。我們可以藉助此方法輸入任何編輯框的值。
示例
使用 send_keys 方法進行程式碼實現。
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/index.htm") #to refresh the browser driver.refresh() # identifying the edit box and entering text with send_keys method driver. find_element_by_css_selector("input[class='gsc-input']"). send_keys("Selenium") #to close the browser driver.close()
使用 Javascript 執行程式進行程式碼實現。
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/index.htm") #to refresh the browser driver.refresh() # enter text with the Javascript executor driver.execute_script( "document.getElementsByName('search')[0].value = 'Selenium' ;") #to close the browser driver.close()
廣告