使用 Selenium(Python)獲取輸入框的值
我們可以使用 Selenium webdriver 獲取輸入框的值。get_attribute() 方法能夠獲取我們在輸入框中輸入的值。為了獲取值,我們必須將值作為引數傳遞給方法。
首先,我們必須使用任何定位器(例如 id、class、name、css 或 xpath)來識別輸入框。然後我們必須使用 send_keys() 方法在其中鍵入一些值。
我們考慮一下下面的輸入框,我們將在其中輸入一些文字 - Selenium Python,然後使用 get_attribute() 獲取值。
示例
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.google.com/") #identify element l= driver.find_element_by_name("q") l.send_keys("q") #get_attribute() to get value of input box print("Value of input box: " + l.get_attribute('value')) driver.close()
輸出
廣告