1.案例
以下案例是一个登录的完整过程
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Edge()
url = "http://localhost:8080/login.jsp"
driver.get(url)
driver.find_element(By.CSS_SELECTOR, value="#username").clear()
# id
driver.find_element(By.CSS_SELECTOR, value="#username").send_keys("zhangsan")
sleep(1)
# 属性
driver.find_element(By.CSS_SELECTOR, value="[name='password']").send_keys("123")
sleep(1)
# 元素
driver.find_element(By.CSS_SELECTOR, value="#remember").click()
sleep(1)
# 元素+属性
driver.find_element(By.CSS_SELECTOR, value="input[value='登陆']").click()
sleep(3)
driver.quit()
2.窗口调整
2.1max,setsize,setposition,back
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
# 浏览器常用操作API
# set_window_size
# set_window_position
# maximize_window()
# driver.back()
# driver.forward()
# refresh()刷新
# close()关闭当前窗口
# quit()关闭所有窗口
# title获取当前页的title
# current_url获取当前页面的url
driver = webdriver.Edge()
url = "http://localhost:8080/login.jsp"
driver.get(url)
# 设置大小
driver.set_window_size(900, 700)
sleep(1)
driver.set_window_position(320,150)
sleep(1)
# 窗口最大化
driver.maximize_window()
sleep(1)
#
driver.find_element(By.CSS_SELECTOR, value="#username").clear()
# id
driver.find_element(By.CSS_SELECTOR, value="#username").send_keys("zhangsan")
sleep(1)
# 属性
driver.find_element(By.CSS_SELECTOR, value="[name='password']").send_keys("123")
sleep(1)
# 元素
driver.find_element(By.CSS_SELECTOR, value="#remember").click()
sleep(1)
# 元素+属性
driver.find_element(By.CSS_SELECTOR, value="input[value='登陆']").click()
sleep(1)
# 后退
driver.back()
sleep(1)
# 前进
driver.forward()
sleep(1)
driver.quit()
2.2refresh,close,quit,title,back,forward
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
# 浏览器常用操作API
# set_window_size
# set_window_position
# maximize_window()
# driver.back()
# driver.forward()
# refresh()刷新
# close()关闭当前窗口
# quit()关闭所有窗口
# title获取当前页的title
# current_url获取当前页面的url
driver = webdriver.Edge()
url = "http://localhost:8080/login.jsp"
driver.get(url)
# 设置大小
driver.set_window_size(900, 700)
sleep(1)
driver.set_window_position(320, 150)
sleep(1)
# # 窗口最大化
# driver.maximize_window()
sleep(1)
# id
driver.find_element(By.CSS_SELECTOR, value="#username").send_keys("zhangsan")
sleep(1)
# # 刷新
# driver.refresh()
# sleep(1)
# 输出title
title = driver.title
print(title)
# 获取当前url
current_url = driver.current_url
print(driver.current_url)
driver.find_element(By.PARTIAL_LINK_TEXT, "注册").click()
sleep(1)
# 关闭当前窗口
driver.close()
sleep(2)
# # 后退
# driver.back()
# sleep(1)
# # 前进
# driver.forward()
# sleep(1)
driver.quit()
3.元素常用信息操作方法
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Edge()
url = "http://localhost:8080/login.jsp"
driver.get(url)
size = driver.find_element(By.CSS_SELECTOR, "#username").size
print("用户名大小:",size)
text = driver.find_element(By.CSS_SELECTOR, "a").text
print("页面第一个a标签名为:",text)
addr = driver.find_element(By.CSS_SELECTOR, "a").get_attribute("href")
print("页面第一个a标签为href属性值为:",addr)
display = driver.find_element(By.CSS_SELECTOR, "h1").is_displayed()
print("h1元素是否可见:",display)
enable = driver.find_element(By.CSS_SELECTOR, "input[value='登陆']").is_enabled()
print("登录按钮是否可以:",enable)
selected = driver.find_element(By.CSS_SELECTOR, "#remember").is_selected()
print("记住是否被选中:",selected)
sleep(1)
driver.quit()
4.鼠标操作
4.1鼠标操作_右键_双击_执行
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
# context_click()右击
# double_click()双击
# drag_and_drop()拖拽
# move_to_element()悬停
# perform()执行以上事物
driver = webdriver.Edge()
url = "http://localhost:8080/login.jsp"
driver.get(url)
# 实例化ActionChains
action = ActionChains(driver)
# context_click为右击(预期:粘贴),CSS定位#username,perform()执行
username = driver.find_element(By.CSS_SELECTOR, "#username")
action.context_click(username).perform()
sleep(2)
# 发送zhangsan并进行双击,预期:选中zhangsan
username.send_keys("zhangsan")
action.double_click(username).perform()
sleep(2)
driver.quit()
4.2鼠标操作_拖拽
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
# context_click()右击
# double_click()双击
# drag_and_drop()拖拽
# move_to_element()悬停
# perform()执行以上事物
driver = webdriver.Edge()
url = "http://localhost:8080/login.jsp"
driver.get(url)
# 实例化ActionChains
action = ActionChains(driver)
# 获取源元素(没有案例)
source = driver.find_element(By.CSS_SELECTOR, "目标1")
# 获取目标元素(没有案例)
target = driver.find_element(By.CSS_SELECTOR, "目标2")
sleep(2)
# 这种会直接覆盖
# action.drag_and_drop(source, target).perform()
# 这种是需要自己调配
action.drag_and_drop_by_offset(source,xoffset=300,yoffset=300).perform()
sleep(2)
driver.quit()
5.键盘操作
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Edge()
url = "http://localhost:8080/login.jsp"
driver.get(url)
# 选中
username = driver.find_element(By.CSS_SELECTOR, "#username")
sleep(1)
# 输入值
username.send_keys("zhangsan1")
sleep(1)
# 删除一个
username.send_keys(Keys.BACK_SPACE)
sleep(1)
# 全选control
username.send_keys(Keys.CONTROL, "a")
sleep(1)
# 复制
username.send_keys(Keys.CONTROL, "c")
sleep(1)
# 选中
password = driver.find_element(By.CSS_SELECTOR, "#password")
# 粘贴
password.send_keys(Keys.CONTROL, "v")
sleep(1)
# 全选
password.send_keys(Keys.CONTROL, "a")
sleep(1)
# 将选中的删除
password.send_keys(Keys.BACK_SPACE)
sleep(1)
# 输入值
password.send_keys("123")
sleep(2)
driver.quit()
6.等待操作
6.1隐性等待
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Edge()
# url = "http://localhost:8080/login.jsp"
url = "https://mail.qq.com/"
driver.get(url)
# 设置隐式等待10秒
driver.implicitly_wait(10)
driver.find_element(By.CSS_SELECTOR,"#switcher_plogin").click()
driver.implicitly_wait(5)
driver.find_element(By.CSS_SELECTOR, "#name").send_keys("zhangsan")
sleep(2)
driver.quit()
6.2显性等待
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Edge()
url = "http://localhost:8080/login.jsp"
driver.get(url)
# timeout超时时间
# poll_frequency访问频率
# x:x为driver
# WebDriverWait(driver,timeout=10,poll_frequency=0.5).until(lambda x:x.find_element(By.CSS_SELECTOR,"username"))返回的是一个元素
WebDriverWait(driver,timeout=10,poll_frequency=0.5).until(lambda x:x.find_element(By.CSS_SELECTOR,"#username")).send_keys("zhangsan")
# 显示等待:针对单个元素生效
# 隐式等待:针对全局元素生效
sleep(2)
driver.quit()