元素操作:点击 输入 清理
python
from selenium import webdriver
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
driver.find_element_by_css_selector("#userA").send_keys("admin")
driver.find_element_by_xpath("//*[@id='passwordA']").send_keys("123456")
driver.find_element_by_id("telA").send_keys("1861111")
driver.find_element_by_css_selector("#emailA").send_keys("123@qq.com")
sleep(3)
# 先清空 再输入
driver.find_element_by_id("telA").clear()
driver.find_element_by_id("telA").send_keys("1860000")
sleep(3)
# driver.find_element_by_css_selector("#zc > fieldset > button").click()
driver.find_element_by_css_selector("button").click()
# 提交后 所有内容清空
sleep(3)
driver.quit()
浏览器常用操作API
a链接属性设置:1.跳转 打开一个新窗口 2.在当前窗口跳转
python
# 在新窗口中打开
<a href="https://www.example.com" target="_blank">Click here</a>
# 当前窗口内跳转
<a href="https://www.example.com" target="_self">Click here</a>
# 参数为"_self"表示在当前窗口打开链接,而参数为"_blank"表示在新窗口打开链接。如果不设置target属性,默认情况下链接会在当前窗口打开。
back:是在当前窗口加载新内容才能后退,打开新窗口不行
forward:先执行后退后 再前进
访问历史!!!
前进必须放在后退操作后执行
python
from selenium import webdriver
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
driver.maximize_window()
sleep(2)
driver.set_window_size(300,200)
sleep(2)
driver.set_window_position(320,150)
sleep(2)
driver.maximize_window()
# driver.find_element_by_css_selector("#fwA").click()
driver.find_element_by_partial_link_text("访问").click()
driver.back()
sleep(2)
driver.forward()
sleep(2)
driver.quit()
quit() 关闭由程序启动的所有窗口
close() 关闭当前窗口
利用title切换当前窗口
url
//注册实例
title:
python
from selenium import webdriver
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册实例.html")
driver.find_element_by_id("user").send_keys("admin")
sleep(2)
driver.refresh()
print(driver.title)
print(driver.current_url)
sleep(2)
driver.find_element_by_link_text("注册A网页").click()
sleep(3)
# 关闭主窗口 默认启动的界面
driver.close() # 关掉了综合实例的
sleep(3)
# 关闭由driver启动的所有窗口
driver.quit()
获取元素信息
python
from selenium import webdriver
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
# 用户名文本框大小
size = driver.find_element_by_id("userA").size
print(size)
# 获取页面上第一个超文本连接内容 有多个 默认返回第一个
print(driver.find_element_by_css_selector("a").text)
# 获取获取页面上第一个超文本连接地址 输入元素a的属性名href 返回其属性值
print(driver.find_element_by_css_selector("a").get_attribute("href"))
# span是否可见
print(driver.find_element_by_css_selector("span").is_displayed())
# 取消是否可用
print(driver.find_element_by_css_selector("#cancelA").is_enabled())
# 是否被选中 购物的
# print(driver.find_element_by_css_selector("#gwA").is_selected())
# 先选中
driver.find_element_by_css_selector("#gwA").click()
sleep(3)
print(driver.find_element_by_css_selector("#gwA").is_selected())
driver.quit()
鼠标操作
python
from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
# 实例化
action = ActionChains(driver)
# 右击
action.context_click(driver.find_element_by_css_selector("#userA")).perform()
sleep(2)
driver.find_element_by_css_selector("#userA").send_keys("admin")
sleep(2)
# 双击 预期选中admin
action.double_click(driver.find_element_by_css_selector("#userA")).perform()
sleep(2)
driver.quit()
python
# 粘贴快捷键 可以实现快捷键的功能 但是Edge好像实现不了
from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
# 实例化
action = ActionChains(driver)
# 获取用户名元素 点击右键
action.context_click(driver.find_element_by_css_selector("#userA")).perform()
# 发送p 只发送了p 在火狐可以
driver.find_element_by_css_selector("#userA").send_keys("p")
# 123
sleep(2)
driver.quit()
python
# 悬停 注意要perform
from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
# 实例化
action = ActionChains(driver)
action.move_to_element(driver.find_element_by_css_selector("button")).perform()
sleep(3)
driver.quit()
python
# 拖拽和移动
from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\drop.html")
action = ActionChains(driver)
source = driver.find_element_by_id("div1")
target = driver.find_element_by_id("div2")
sleep(2)
action.drag_and_drop(source,target).perform()
sleep(2)
# 扩展 通过坐标偏移量移动
action.drag_and_drop_by_offset(source,xoffset=180,yoffset=360).perform()
sleep(2)
driver.quit()
包:集合
模块:一个个.py文件
python
from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
action = ActionChains(driver)
action.context_click("定位元素").perform()
action.double_click("定位元素").perform()
action.move_to_element("定位元素").perform()
action.drag_and_drop("source元素","target元素").perform()
# perform() 执行
键盘操作
python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
# 定位
user = driver.find_element_by_id("userA")
# 输入
user.send_keys("admin1")
sleep(2)
# 删除一个
user.send_keys(Keys.BACK_SPACE)
# 全选 注意双引号
user.send_keys(Keys.CONTROL,"a")
# 复制
user.send_keys(Keys.CONTROL,"c")
driver.find_element_by_id("passwordA").send_keys(Keys.CONTROL,"v")
sleep(3)
driver.quit()
python
user.send_keys(Keys.CONTROL,"c") #组合键
user.send_keys(Keys.BACK_SPACE) #单键
元素等待
python
from selenium import webdriver
from time import sleep
# 获取浏览器驱动对象
driver = webdriver.Edge()
# 最大化浏览器
driver.maximize_window()
# 设置元素等待 隐式等待
driver.implicitly_wait(3)
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
driver.find_element_by_id("user").send_keys("admin")
sleep(3)
driver.quit()
python
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
# 返回的是元素
# element = WebDriverWait(driver,timeout=3,poll_frequency=0.5).until(lambda x:x.find_element_by_id("user"))
# element.send_keys("admin")
# x 为driver
WebDriverWait(driver,timeout=3,poll_frequency=0.5).until(lambda x:x.find_element_by_id("user")).send_keys("admin")
sleep(3)
driver.quit()
两者异常类型不同
显示等待:
隐式等待:
键盘:组合 单键 send_keys(Keys.xxx,"a") Keys 需要导包
测试上传文件
python
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
# driver.find_element_by_css_selector("[name='upfilea']").click()
# 使用send_keys("文件路径)
driver.find_element_by_css_selector("[name='upfilea']").send_keys(r"D:\WORK\1.png")
sleep(10)
driver.quit()
测试下拉框
python
from selenium import webdriver
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
driver.maximize_window()
driver.implicitly_wait(30)
driver.find_element_by_css_selector("[value='sh']").click()
sleep(2)
driver.find_element_by_css_selector("[value='bj']").click()
sleep(2)
driver.find_element_by_css_selector("[value='gz']").click()
sleep(2)
driver.quit()