#!/usr/bin/env python
# -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time,os
# about:addons 火狐浏览器安装组件,访问的地址
# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
# <input type="submit" value="百度一下" id="su" class="btn self-btn bg s_btn">
#id
keys="selenium自动化"
delay=3
url="https://www.baidu.com/"
driver=webdriver.Firefox()
driver.get(url)
#等待delay秒
driver.implicitly_wait(delay)
#输入字符串
driver.find_element_by_id("kw").send_keys(keys)
#清空输入的内容
driver.find_element_by_id("kw").clear()
#鼠标左键点击
driver.find_element_by_id("su").click()
driver.find_element_by_id("kw").send_keys(keys)
#模拟回车提交表单
driver.find_element_by_id("kw").submit()
driver.find_element_by_id("kw").send_keys(Keys.ENTER)
#复制 粘贴 全选 剪切 制表键 f11~f12
driver.find_element_by_id("kw").send_keys(keys,Keys.CONTROL,'c')
driver.find_element_by_id("kw").send_keys(keys,Keys.CONTROL,'v')
driver.find_element_by_id("kw").send_keys(keys,Keys.CONTROL,'a')
driver.find_element_by_id("kw").send_keys(keys,Keys.CONTROL,'x')
driver.find_element_by_id("kw").send_keys(Keys.TAB)
driver.find_element_by_id("kw").send_keys(Keys.F1)
#执行ActionChains中的所有行为
source=driver.find_element_by_link_text("设置")
target=driver.find_element_by_link_text("新闻")
# 鼠标悬停
ActionChains(driver).move_to_element(source).perform()
# 右击鼠标
ActionChains(driver).context_click(source).perform()
# 双击鼠标
ActionChains(driver).double_click(source).perform()
#鼠标移动到另一个位置
ActionChains(driver).drag_and_drop(source,target).perform()
# 鼠标左键
ActionChains(driver).click_and_hold(source).perform()
#等待delay秒
time.sleep(delay)
driver.quit()