案例代码
import time
from selenium import webdriver;
from selenium.webdriver.common.action_chains import ActionChains
# 获取谷歌驱动
drive = webdriver.Chrome();
# 2.打开百度
drive.get('http://www.baidu.com');
time.sleep(2)
# 窗口放大
drive.maximize_window()
# 3.找到百度的输入款
# bdEL = drive.find_element_by_id('kw')
# time.sleep(3)
action = ActionChains(drive);
# 4.右键 执行方法
# action.context_click(bdEL).perform()
# 5.双击事件
# bdEL.send_keys('tomcat8')
# action.double_click(bdEL).perform()
# 6.拖拽事件
# drive.get(r'C:\Users\Administrator\Desktop\软件测试\html\drop.html')
# 获取到盒子div的元素对象
# div1 = drive.find_element_by_id('div1')
# div2 = drive.find_element_by_id('div2')
# action = ActionChains(drive);
# 将div1拖拽到div2里面
# time.sleep(3)
# action.drag_and_drop(div1,div2).perform()
#鼠标悬停
gd = drive.find_element_by_id('s-usersetting-top')
action.move_to_element(gd).perform()
弹窗
drive.find_element_by_id('alerta').click()
time.sleep(2)
# drive.switch_to_alert()过时
# 获取到alert对象
alert = drive.switch_to.alert
# 点击弹框的确定
alert.accept()
执行js脚本
js2 = 'alert("哈哈")'
time.sleep(2)
# 执行js脚本
drive.execute_script(js)
在当前页面中找到其他页面
drive.get(r'C:\Users\Administrator\Desktop\素材\注册2实例.html')
time.sleep(2)
# 切换到a页面
drive.switch_to.frame('myframe1')
drive.find_element_by_id('userA').send_keys("admin")
# 切换到原来的页面
drive.switch_to.default_content()
drive.find_element_by_id("user").send_keys("大彭Sir")