import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys # 键盘
from selenium.webdriver.support.select import Select # 鼠标选择
driver = webdriver.Chrome()
driver.get("https://liushilive.github.io/html_example/index1.html")
#一个不错的练习地址
eleUser = driver.find_element_by_id("uid")
elePwd = driver.find_element_by_id("pwd")
eleUser.clear()
eleUser.send_keys("11111")
elePwd.send_keys("22222")
eleUser.send_keys(Keys.BACK_SPACE * 2) # 退格2次
elePwd.send_keys(Keys.ARROW_LEFT) # 光标移动
Button_Login = driver.find_element_by_xpath("//input[@type='submit']")
Button_Login.click()
P_List = driver.find_element_by_id("s3Id")
P_List_select = Select(P_List)
P_List_select.select_by_index(4)
P_List_select.select_by_value("6")
P_List_select.deselect_all() # 取消所有选择
Button1 = driver.find_element_by_id("b1")
Button2 = driver.find_element_by_id("b2")
Button1.click() # 弹出警告窗口
driver.switch_to.alert.accept() # 点击接受
driver.switch_to.alert.dismiss() # 点击取消
Button2.click() # 弹出输入窗口
driver.switch_to.alert.send_keys("Hello World!") # 输入内容
driver.switch_to.alert.accept() # 点击接受
###双击 属于 行动链对象
DB = driver.find_element_by_id("dblclick")
Ac = webdriver.ActionChains(driver) ############# 行动链对象
Ac.double_click(DB) ############# 双击
Ac.perform() # 执行
webdriver.ActionChains(driver).double_click(DB).perform() ###同上,一句执行
############## 悬浮在多级下拉菜单 #### 移动鼠标 ##############################
FenLei_Link = driver.find_element_by_link_text("分 类")
webdriver.ActionChains(driver).move_to_element(FenLei_Link).perform()
BianChengYuYan_Link = driver.find_element_by_link_text("编程语言")
webdriver.ActionChains(driver).move_to_element(BianChengYuYan_Link).perform()
PHP_Link = driver.find_element_by_link_text("PHP")
webdriver.ActionChains(driver).move_to_element(PHP_Link).perform()
webdriver.ActionChains(driver).move_by_offset(0, 44).perform() # 按偏移量移动
###################### 拖动滑块 #######################
HuaKuai = driver.find_element_by_xpath("//*[@id='drag']/div[3]") # 滑块
webdriver.ActionChains(driver).drag_and_drop_by_offset(HuaKuai, 300, 0).perform()
####################滑块拖动 模拟人为 ####################
HuaKuai = driver.find_element_by_xpath("//*[@id='drag']/div[3]") # 滑块
webdriver.ActionChains(driver).click_and_hold(HuaKuai).perform()
for i in range(50):
webdriver.ActionChains(driver).move_by_offset(6, 0).perform()
webdriver.ActionChains(driver).release().perform()
time.sleep(3)
ResetButton = driver.find_element_by_id("re_drag")
ResetButton.click() # 3秒后点击 重置 按钮
##################################################
#### 拖拽方块,把 方块1 移动到 方块5 ###############
ele1 = driver.find_element_by_xpath("//div[@data-dad-id='1']")
ele5 = driver.find_element_by_xpath("//div[@data-dad-id='5']")
webdriver.ActionChains(driver).click_and_hold(ele1).perform() # 鼠标点击左键不放
webdriver.ActionChains(driver).move_to_element(ele5).perform() # 移动到方块5上
webdriver.ActionChains(driver).release().perform() # 释放鼠标左键
#方法2:使用drag_and_drop , 一行代码搞定
webdriver.ActionChains(driver).drag_and_drop(ele1,ele5).perform()
####### 多选框 #######
C1 = driver.find_element_by_id("c1")
C2 = driver.find_element_by_id("c2")
C3 = driver.find_element_by_id("c3")
C4 = driver.find_element_by_id("c4")
if C1.is_selected():
print("已选择")
else:
print("没选择")
if C1.is_selected() is False:
print("没选择")
C1.click() # 勾上
########### 单选框 ############
Man = driver.find_element_by_id("man")
Woman = driver.find_element_by_id("woman")
if Man.is_selected():
print("当前选择男")
Woman.click()
###### 文件上传框 ####### 选择本地文件 ##############
CheckFile = driver.find_element_by_id("upload")
CheckFile.send_keys("C:\\111.txt") # 本地文件路径,需要双斜杠 \\
# input标签是可以直接send_keys的,其它不能
#####################################################
MyFrame = driver.find_element_by_name("frame1")
driver.switch_to.frame(MyFrame)
FHSY = driver.find_element_by_link_text("返回首页")
FHSY.click()
driver.switch_to.parent_frame() #切换回父框架
driver.switch_to.default_content() #切换到主页面
driver.switch_to.frame(None) #切换到主页面,同 driver.switch_to.default_content()