下拉选择框
from selenium import webdriver
from time import sleep
# 导包
from selenium.webdriver.support.select import Select
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
driver.maximize_window()
driver.implicitly_wait(30)
# 匿名
# Select(element).select_by_index()
# Select(element).select_by_value()
# Select(element).select_by_visible_text()
# 实名
# select = Select(element) element 只能是select标签
Select(driver.find_element_by_css_selector("#selectA")).select_by_value("bj")
sleep(2)
Select(driver.find_element_by_css_selector("#selectA")).select_by_index(1)
sleep(2)
Select(driver.find_element_by_css_selector("#selectA")).select_by_visible_text("A广州")
sleep(2)
#通过css
driver.find_element_by_css_selector("[value = 'cq']").click()
sleep(2)
driver.quit()
弹出框
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("#alerta").click()
# 切换 处理
# driver.switch_to.alert.accept()
# driver.switch_to.alert.dismiss()
print(driver.switch_to.alert.text) # 这个之后还是得处理同意或取消 不然会报错
# 我被被点击啦!
driver.switch_to.alert.dismiss()
sleep(2)
driver.find_element_by_css_selector("#userA").send_keys("admin")
sleep(2)
driver.quit()
at = driver.switch_to.alert
# 切换 处理
# at.accept()
print(at.text) # 这个之后还是得处理同意或取消 不然会报错
# 我被被点击啦!
at.dismiss()
滚动条操作
一个水平滑动条 一个垂直滑动条
from selenium import webdriver
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册A.html")
sleep(2)
js = "window.scrollTo(0,10000)"
driver.execute_script(js)
sleep(2)
driver.quit()
frame表单切换
首先在注册实例 切换到注册A
恢复默认页面 再切换到注册B
因为在注册A的源代码中是没有iframe的
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_css_selector("#user").send_keys("admin")
driver.find_element_by_css_selector("#password").send_keys("123456")
driver.find_element_by_css_selector(".tel").send_keys("123451432")
driver.find_element_by_css_selector("#email").send_keys("ww@qq.com")
# 通过id进行切换
driver.switch_to_frame("idframe1")
driver.find_element_by_css_selector("#userA").send_keys("admin")
driver.find_element_by_css_selector("#passwordA").send_keys("123456")
driver.find_element_by_css_selector(".telA").send_keys("123451432")
driver.find_element_by_css_selector("#emailA").send_keys("ww@qq.com")
sleep(2)
# 切换到默认页面
driver.switch_to_default_content()
# 通过name进行切换
# driver.switch_to_frame("myframe2")
# 通过元素定位
driver.switch_to_frame(driver.find_element_by_css_selector("[name = 'myframe2']"))
driver.find_element_by_css_selector("#userB").send_keys("admin")
driver.find_element_by_css_selector("#passwordB").send_keys("123456")
driver.find_element_by_css_selector(".telB").send_keys("123451432")
driver.find_element_by_css_selector("#emailB").send_keys("ww@qq.com")
sleep(2)
driver.quit()
多窗口切换
_blank 跳转到新窗口打开
所以需要切换窗口
from selenium import webdriver
from time import sleep
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册实例.html")
cur = driver.current_window_handle
print("当前窗口句柄", cur)
driver.find_element_by_partial_link_text("注册A网页").click()
handles = driver.window_handles # 获取所有的
# 当前窗口句柄 764CA7D94866BF4B7633F4F42D931775
# 764CA7D94866BF4B7633F4F42D931775
# 58B964F25D36A08C1004B1C0504248F4
for handle in handles:
print(handle)
if handle != cur:
# 切换
driver.switch_to.window(handle)
sleep(2)
driver.find_element_by_css_selector("#userA").send_keys("admin")
driver.find_element_by_css_selector("#passwordA").send_keys("123456")
driver.find_element_by_css_selector(".telA").send_keys("123451432")
driver.find_element_by_css_selector("#emailA").send_keys("ww@qq.com")
sleep(2)
driver.quit()
窗口截图、验证码处理
from selenium import webdriver
from time import sleep
import time
driver = webdriver.Edge()
driver.get(r"D:\WORK\ww\web自动化_day01_课件+笔记+资料+代码\web自动化_day01_课件+笔记+资料+代码\02_其他资料\注册实例.html")
driver.find_element_by_css_selector("#user").send_keys("admin")
driver.find_element_by_css_selector("#password").send_keys("123456")
driver.find_element_by_css_selector(".tel").send_keys("123451432")
driver.find_element_by_css_selector("#email").send_keys("ww@qq.com")
sleep(2)
# 截图方式 时间戳
# driver.get_screenshot_as_file("./image/info_1.png")
driver.get_screenshot_as_file("./image/%s.png"%(time.strftime("%Y_%m_%d %H_%M_%S")))
# s.png s.jpg 都行
driver.quit()
get_cookie(name)的底层为get_cookies()
对于百度网址来说 cookie的键名BDUSS 对应的值 登录的时候抓包
from selenium import webdriver
from time import sleep
import time
driver = webdriver.Edge()
# 先请求网址
driver.get(r"https://www.baidu.com/")
# 再设置cookie
driver.add_cookie({"name": "BDUSS",
"value": ""})
sleep(2)
# 一定要刷新
driver.refresh()
sleep(2)
driver.quit()
cookies = driver.get_cookies()
for co in cookies:
print("内容为", co)
cookies = driver.get_cookies()
for co in cookies:
print(co.get('name'))
底层原理:
cookies = driver.get_cookies()
for co in cookies:
if co.get('name') == 'BDUSS':
print(co.get('value'))
弹出框 frame表单切换 多窗口切换
cookie
cookie就是能实现绕过验证码的功能 感觉就是不用用户自己重复登录
用户第一次通过输入用户名 密码 验证码给服务器进行登录
服务器验证通过生成cookie 一串字符串 将其标为验证成功 登录成功
响应时把这个cookie一并给浏览器 浏览器以后就可以自行设置
浏览器就自己设置好了并保存
然后后续当我们再次用这个浏览器访问这个网页时
浏览器网页自己将保存的cookie给服务器 然后就是自动登录了 显示登录成功的状态