The keys () class provides methods for almost all keys on the keyboard, which can be used to simulate keys on a keyboard, including various key combinations, such as CTRL + A, Ctrl+x,ctrl+c, Ctrl + V, and so on.
From selenium import WebdriverFrom Selenium.webdriver.common.keys import keysFrom time Import sleepdriver = Webdriver. Chrome () driver.get ("http://www.baidu.com") # input box enter the content driver.find_element_by_id ("kw").Send_keys("Seleniumm") Sleep (10) # Delete a mdriver.find_element_by_id ("kw") with multiple inputs. Send_keys (Keys.back_spaceSleep (10) # Enter the SPACEBAR + "tutorial" driver.find_element_by_id ("kw"). Send_keys (Keys.space) driver.find_element_by_id ("kw"). Send_keys ("tutorial") Sleep # Ctrl + A Select all input box contents driver.find_element_by_id ("kw"). Send_ KeysKeys.control, ' a') Sleep # ctrl+x cut the input box contents driver.find_element_by_id ("kw"). Send_keys (Keys.control, ' x ') Sleep # Ctrl + V paste the contents into the input box driver.find_element_by_id ("kw"). Send_keys (Keys.control, ' V 'Sleep (10) # driver.find_element_by_id ("su") using the ENTER key instead of the click operation. Send_keys (Keys.enter) Sleep (driver.quit) ()
In addition, there are simulations of other keys:
Send_keys (Keys.tab) Tab key (tab)
Send_keys (Keys.escape) fallback key (ESC)
Send_keys (KEYS.F1) keyboard F1
......
Send_keys (KEYS.F12) keyboard F12
Reference: http://www.testclass.net/selenium_python/keyword-event/
Selenium-webdriver-keys Class (keyboard operation)