Keyboard events are some of the actions on the keyboard, such as Ctrl +c,ctrl+v,ctrl+x, and so on.
The operation on the keyboard requires a library to be imported into another keyboard:
from selenium.webdriver.common.keys import Keys
For example, you want to enter the "automated test" in the search box, but now you want to search "automated testing", is to delete a word, we know, is to press the BACKSPACE key on the keyboard can be, this time you need keyboard operation:
driver.find_element_by_xpath("xpath的定位").send_keys(Keys.BACK_SPACE)
That's all you can do.
The next piece of code, you'll remember:
# coding:utf-8from Selenium import webdriverfrom time import Sleepfrom Selenium.webdriver.common.keys im Port Keysdriver = Webdriver. Firefox () driver.get ("http://www.baidu.com") # Enter the contents of the Input box driver.find_element_by_id ("kw"). Send_keys ("Selenium") sleep ( 3) # Delete a mdriver.find_element_by_id ("kw") with multiple inputs. Send_keys (Keys.back_space) Sleep (3) # Continue typing tutorial driver.find_element_by_ ID ("kw"). Send_keys (U "tutorial") Sleep (3) # CTRL + A Select all input box content driver.find_element_by_id ("kw"). Send_keys (Keys.control, ' a ') Sleep (3) # ctrl+x cut input Box contents driver.find_element_by_id ("kw"). Send_keys (Keys.control, ' X ') sleep (3) # input box re-enter content, Search driver.find_element_by_id ("kw"). Send_keys (Keys.control, ' V ') Sleep (3) # Use the Enter keyboard instead of the Click action driver.find_element_by_id (" Su "). Send_keys (Keys.enter) sleep (3) driver.quit ()
?
Understand the above content is almost, if the new needs only need Baidu, to know that we can not put all things in mind, as long as the introduction, know how Baidu, that the purpose is reached, of course, if you can remember, then you are more powerful, anyway, I can't remember.
Public search " Automated test practice" or scan the QR code below to add attention ~ ~ ~
"Selenium2 Python Automation Test" (5)--keyboard events