selenium.webdriver.common.keys
时间: 2023-09-16 11:06:40 浏览: 139
The `selenium.webdriver.common.keys` module provides a set of keyboard keys that can be used to simulate keyboard actions in Selenium tests. This module contains the following classes:
- Keys: This class contains constants for all the keyboard keys that can be used for keyboard actions in Selenium. Some of the commonly used keys are ENTER, TAB, SPACE, SHIFT, CONTROL, ALT, BACKSPACE, DELETE, ARROW_UP, ARROW_DOWN, ARROW_LEFT, and ARROW_RIGHT.
- ActionChains: This class provides methods to perform keyboard actions like pressing a key, releasing a key, typing a string, and performing a sequence of actions in a specific order.
Here's an example of how to use these classes to simulate a keyboard action in Selenium:
```python
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
# create a webdriver instance and navigate to a webpage
driver = webdriver.Chrome()
driver.get("https://www.google.com")
# find the search box and enter a query
search_box = driver.find_element_by_name("q")
search_box.send_keys("selenium")
# press Enter key to submit the search query
search_box.send_keys(Keys.ENTER)
# perform a sequence of actions - type a string, press TAB key, type another string
action = ActionChains(driver)
action.send_keys("hello").send_keys(Keys.TAB).send_keys("world").perform()
# close the browser window
driver.quit()
```
阅读全文