web自动化测试浏览器操作
WebDriver中提供了一系列的浏览器操作,使用户能更好的操作浏览器,更好的进行自动化操作。下面对一些浏览器操作方法进行了介绍和使用,希望能帮助到你…导航
启动浏览器后,您要做的第一件事就是打开您的网站
driver.get('https://www.baidu.com/')获取当前url
使用以下方法从浏览器的地址栏中读取当前URL
注意current_url后面是不带括号的,使用的话是会报错的
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
url = driver.current_url
print(url)url显示在控制台上:
/Users/lifeng/python-virtualenv/venv/bin/python3 /Users/lifeng/python-projects/test-python/selenium_script.py
https://www.baidu.com/
Process finished with exit code 0后退
浏览器的后退按钮
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
driver.find_element_by_id("kw").send_keys('查找')
time.sleep(5)
driver.back()前进
浏览器的前进按钮
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
driver.find_element_by_id("kw").send_keys('查找')
time.sleep(5)
driver.forward()刷新
刷新当前页面
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
driver.find_element_by_id("kw").send_keys('查找')
time.sleep(5)
driver.refresh()获取标题
从浏览器中读取当前页面标题
title不能加括号,否则会报错
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
driver.find_element_by_id("kw").send_keys('查找')
time.sleep(5)
driver.titletitle名称显示在控制台:
http://www.51testing.com/attachments/2021/04/15326880_202104021559201qU49.png
如果title加了括号,就报如下错误:‘str’ object is not callable
/Users/lifeng/python-virtualenv/venv/bin/python3 /Users/lifeng/python-projects/test-python/selenium_script.py
Traceback (most recent call last):
File "/Users/lifeng/python-projects/test-python/selenium_script.py", line 9, in <module>
driver.title()
TypeError: 'str' object is not callable
Process finished with exit code 1 获取窗口句柄
WebDriver不会在窗口和选项卡之间进行区分。如果您的站点打开一个新的选项卡或窗口,Selenium将允许您使用窗口句柄来使用它。每个窗口都有一个唯一的标识符,该标识符在单个会话中保持不变。
获取当前窗口的窗口句柄
切记current_window_handle后面没有括号,加了会报错的
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
time.sleep(1)
handle_one = driver.current_window_handle
print(handle_one)
driver.find_element_by_xpath('//*').click()
handle_two = driver.current_window_handle
print(handle_two)句柄ID显示在控制台上:
/Users/lifeng/python-virtualenv/venv/bin/python3 /Users/lifeng/python-projects/test-python/selenium_script.py
CDwindow-7B9B39D222DFB38B060ECEE7C7781C60
CDwindow-7B9B39D222DFB38B060ECEE7C7781C60
Process finished with exit code 0句柄切换-切换窗口
点击一个链接,该链接会在新窗户聚焦在屏幕上,但是WebDriver将不知道操作系统认为哪个窗口处于活动状态。要使用新窗口,您将需要切换到它。window_handles是获取多个句柄,返回的是一个列表,可以使用下标获取。
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
time.sleep(1)
handle_one = driver.current_window_handle
driver.find_element_by_xpath('//*').click()
wins = driver.window_handles
print(wins)以一个列表结果显示在控制台上:
/Users/lifeng/python-virtualenv/venv/bin/python3 /Users/lifeng/python-projects/test-python/selenium_script.py
['CDwindow-AF8997BAD711CC3594401F925514090B', 'CDwindow-FEDA1D318C16D669C57446ECA6296709']
Process finished with exit code 0再用switch_to.window()用来获取指定窗口,也就是切换窗口
http://www.51testing.com/attachments/2021/04/15326880_2021040215594414QYW.png
页面显示结果,获取的句柄是第一个,也就是wins获取的那个句柄。
关闭窗口
当完成一个窗口并且它不是浏览器中最后一个打开的窗口时,应将其关闭并切换回以前使用的窗口。
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
time.sleep(1)
handle_one = driver.current_window_handle
driver.find_element_by_xpath('//*').click()
wins = driver.window_handles
driver.switch_to.window(wins)
driver.close()退出浏览器
完成浏览器会话后,应调用quit而不是close
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
time.sleep(1)
handle_one = driver.current_window_handle
driver.find_element_by_xpath('//*').click()
wins = driver.window_handles
driver.switch_to.window(wins)
time.sleep(2)
driver.close()
time.sleep(2)
driver.quit()退出将:
1.关闭与该WebDriver会话关联的所有窗口
2.关闭浏览器进程
3.关闭后台驱动程序
我们也可以使用 try / finally ,这样出现异常仍会退出浏览器会话,finally号称流氓,也就是不管怎么样,最后都会执行 finally 。
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
try:
time.sleep(1)
handle_one = driver.current_window_handle
driver.find_element_by_xpath('//*').click()
wins = driver.window_handles
driver.switch_to.window(wins)
time.sleep(2)
driver.close()
time.sleep(2)
finally:
driver.quit()当然现在的WebDriver也是支持python上下文管理器的,当使用with关键字时,它可以在执行结束时自动退出驱动程序。
import time
from selenium import webdriver
with webdriver.Chrome() as driver:
driver.get('https://www.baidu.com/')
time.sleep(2)
driver.find_element_by_xpath('//*').click()
time.sleep(2)框架和内联框架(frames and Iframes)
如果是iframe框架,我们就要先进入框架再进行元素操作
iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
driver.switch_to.frame(iframe)
driver.find_element(By.CLASS_NAME, 'button').click()进入框架后,操作完成后也要记得离开框架,不然框架外的元素就无法定位了,离开框架就要使用下面的方法:
driver.switch_to.default_content()获取窗口大小
获取浏览器窗口的大小(以像素为单位)
import time
from selenium import webdriver
with webdriver.Chrome() as driver:
driver.get('https://pan.baidu.com/')
time.sleep(2)
driver.get_window_size().get('width')
driver.get_window_size().get('height')也就可以这样操作:
import time
from selenium import webdriver
with webdriver.Chrome() as driver:
driver.get('https://pan.baidu.com/')
time.sleep(2)
size = driver.get_window_size()
width = size.get('width')
height = size.get('height')
print(f'宽:{width}, 高:{height}')
driver.find_element_by_xpath('//*[@placeholder="手机/邮箱/用户名"]').send_keys('19999999999')
time.sleep(2)把宽度和高度显示在控制台:
/Users/lifeng/python-virtualenv/venv/bin/python3 /Users/lifeng/python-projects/test-python/selenium_script.py
宽:1200, 高:897
Process finished with exit code 0设定窗口大小
恢复窗口并设置窗口大小
driver.set_window_size(1024, 768)获取窗口位置
获取浏览器窗口左上角的坐标
import time
from selenium import webdriver
with webdriver.Chrome() as driver:
driver.get('https://pan.baidu.com/')
time.sleep(2)
position = driver.get_window_position()
x = position.get('x')
y = position.get('y')
print(f'坐标 x:{x}, 坐标 y:{y}')
driver.find_element_by_xpath('//*[@placeholder="手机/邮箱/用户名"]').send_keys('19999999999')
time.sleep(2)也可以这样操作:
import time
from selenium import webdriver
with webdriver.Chrome() as driver:
driver.get('https://pan.baidu.com/')
time.sleep(2)
x = driver.get_window_position().get('x')
y = driver.get_window_position().get('y')
print(f'坐标 x:{x}, 坐标 y:{y}')
driver.find_element_by_xpath('//*[@placeholder="手机/邮箱/用户名"]').send_keys('19999999999')
time.sleep(2)把坐标x和坐标y显示在控制台:
/Users/lifeng/python-virtualenv/venv/bin/python3 /Users/lifeng/python-projects/test-python/selenium_script.py
坐标 x:22, 坐标 y:47
Process finished with exit code 0设定视窗位置
将窗口移到所选位置
driver.set_window_position(0, 0)最大化窗口
该窗口将填满整个屏幕,且不会阻塞操作系统自己的菜单和工具栏
driver.maximize_window()
最小化窗口
最小化窗口通常会将窗口隐藏在系统托盘中
driver.minimize_window()全屏窗口
填充整个屏幕,类似于在大多数浏览器中按F11键
driver.fullscreen_window()截屏
捕获当前浏览上下文的屏幕截图。返回以Base64格式编码的屏幕截图。
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://pan.baidu.com/')
driver.save_screenshot('./image.png')
driver.quit()屏幕截图
捕获当前浏览上下文的元素的屏幕快照。返回以Base64格式编码的屏幕截图
import time
from selenium import webdriver
with webdriver.Chrome() as driver:
driver.get('https://pan.baidu.com/')
time.sleep(2)
ele = driver.find_element_by_xpath('//*[@placeholder="手机/邮箱/用户名"]')
ele.screenshot('../image.png')
time.sleep(2)
页:
[1]