## 鼠标悬停
```python
el = browser.find_element('xpath', "//a[contains(text(),'HTML / CSS')]")
# actionchains 下面动作方法必须要再加上 perform 才能生效
ActionChains(browser).move_to_element(el).perform()
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
def test_hover():
url = 'https://www.runoob.com/html/html-tutorial.html'
with webdriver.Chrome(executable_path='chromedriver_95.exe') as browser:
browser.implicitly_wait(5)
browser.get(url)
# 鼠标悬停(哪个元素)
el = browser.find_element('xpath', "//a[contains(text(),'HTML / CSS')]")
# actionchains 下面动作方法必须要再加上 perform 才能生效
ActionChains(browser).move_to_element(el).perform()
# 双击
# ActionChains(browser).double_click(el).perform()
# # 右击
# ActionChains(browser).context_click(el).perform()
# # 拖动
# ActionChains(browser).drag_and_drop(el, el).perform()
time.sleep(5)
# 双击
# ActionChains(browser).double_click(el).perform()
# # 右击
# ActionChains(browser).context_click(el).perform()
# # 拖动
# ActionChains(browser).drag_and_drop(el1, el2).perform()
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
def test_drapanddrop():
url = 'https://demos.telerik.com/kendo-ui/dragdrop/index'
with webdriver.Chrome(executable_path='chromedriver_95.exe') as browser:
browser.implicitly_wait(5)
browser.maximize_window()
browser.get(url)
el_start = browser.find_element('id', "draggable")
# 把该元素挪动到可视范围之内
el_start.location_once_scrolled_into_view
el_end = browser.find_element('id', "droptarget")
# actionchains 下面动作方法必须要再加上 perform 才能生效
# ActionChains(browser).move_to_element(el).perform()
# # 拖动
ActionChains(browser).drag_and_drop(el_start, el_end).perform()
time.sleep(5)
```
## toast
因为是通过 JS 动态加载的,过3s 消失,当出现时,使用 source 的暂停键,
debug,
注意设置等待。
## 用户输入,
- send_keys()
- 单选
- 多选 (找到元素,直接点击)
- select (直接找到 option 元素,点击)
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
def test_check():
url = 'file:///D:/vip%E7%8F%AD%E7%BA%A7/py44/day37_%E6%B5%8F%E8%A7%88%E5%99%A8%E6%93%8D%E4%BD%9C/radio.html'
with webdriver.Chrome(executable_path='chromedriver_95.exe') as browser:
browser.implicitly_wait(5)
browser.maximize_window()
browser.get(url)
browser.find_element('xpath', '//input[@value="a"]').click()
# time.sleep(5)
# 下拉框
# browser.find_element('name', 'mysel').click()
browser.find_element('xpath', '//option[text()="选项3"]').click()
time.sleep(5)
- 快捷键
```python
# 回车
browser.find_element('id', 'kw').send_keys('困困', Keys.ENTER)
import time
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
def test_kuaijiejian():
url = 'http://www.baidu.com'
with webdriver.Chrome(executable_path='chromedriver_95.exe') as browser:
browser.implicitly_wait(5)
browser.maximize_window()
browser.get(url)
browser.find_element('id', 'kw').send_keys('困困')
# 回车
# browser.find_element('id', 'kw').send_keys(Keys.ENTER)
# 第二种情况: Ctrl + c (+Ctrl松开) Ctrl + v
# 下面这个是先按control,松开后再v
# browser.find_element('id', 'kw').send_keys(Keys.CONTROL, 'v')
actions = ActionChains(browser)
# 3个步骤,组合键,先按control再按a再抬起control
actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
actions.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
time.sleep(5)