【软件测试】4.自动化测试常见函数(4)

发布于:2025-07-10 ⋅ 阅读:(16) ⋅ 点赞:(0)


6. 浏览器导航

6.1 打开网站

driver.get("https://tool.lu/")

6.2 浏览器的前进、后退、刷新

driver.back()
driver.forward()
driver.refresh()

7f3c6d38961bdfa4569bd134df3117d5

import time
import datetime

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
# 1.打开浏览器
chrome_driver_path = r"D:\Program Files\Python39\Scripts\chromedriver.exe"
# 1.打开浏览器
driver = webdriver.Chrome(service=ChromeService(executable_path=chrome_driver_path))

driver.get("https://www.baidu.com")
driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("迪丽热巴")
driver.find_element(By.CSS_SELECTOR, "#su").click()
time.sleep(2)

# 后退,回到百度首页
driver.back()
time.sleep(2)
# 前进,回到迪丽热巴
driver.forward()
time.sleep(2)
# 刷新
driver.refresh()
time.sleep(2)

driver.quit()

7. 文件上传

文件上传的html:upload.html

<html>
    <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <title>upload_file</title>
        <script type="text/javascript">
        </script>
    </head>
    <body>
        <div class="row-fluid">
            <div class="span6 well">
                <h3>upload_file</h3>
                <input type="file" name="file" />
            </div>
        </div>
    </body>
</html>

b5004e93cbed86a2acba81ab726f1089

473bf928a450e3f46e54c068bf8c8d3a

这个弹窗不是控件,无法识别窗口元素。

点击文件上传的场景下会弹窗系统窗口,进行文件的选择。

selenium无法识别非web的控件,上传文件窗口为系统自带,无法识别窗口元素。

但是可以使用sendkeys来上传指定路径的文件,达到的效果是一样的。

driver.get("file:///D:/file/%E6%AF%94%E7%89%B9%E6%95%99%E5%8A%A1/%E6%B5%8B%E8%AF%95/selenium4html/selenium-html/upload.html")
ele = driver.find_element(By.CSS_SELECTOR,"body > div > div > input[type=file]")
ele.send_keys("D:\\file\\test.txt")

html写完后用浏览器打开 ,复制网址黏贴到driver.get()里面就行了

6097fea3bdc91cabb3c3196294132399

import time
import datetime

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
# 1.打开浏览器
chrome_driver_path = r"D:\Program Files\Python39\Scripts\chromedriver.exe"
# 1.打开浏览器
driver = webdriver.Chrome(service=ChromeService(executable_path=chrome_driver_path))

# 上传本地文件
driver.get("file:///D:/%E4%B9%A6/114%E5%BD%95%E6%92%AD%E8%AF%BE%E4%BB%B6/%E6%B5%8B%E8%AF%95%E8%AF%BE%E4%BB%B62024-C++/upload.html")
ele = driver.find_element(By.CSS_SELECTOR, "body > div > div > input[type=file]").send_keys("D:\\file\\test.txt")
time.sleep(2)

driver.quit()

上传前:

4b32c7dea203dd46ae2258378b8441c7

上传后:

9c726bebbaf756285ca5469529d1aa95


8. 浏览器参数设置

8.1 设置无头模式

options = webdriver.ChromeOptions()
options.add_argument("-headless") # -headless:表示无头模式
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()),options=options)

无头模式:无头模式:程序在后端运行,界面看不到页面的表现。

自动化打开浏览器默认情况下为有头模式。

import time
import datetime

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
# 设置 浏览器驱动
chrome_driver_path = r"D:\Program Files\Python39\Scripts\chromedriver.exe"
# 浏览器参数配置
options = webdriver.ChromeOptions()
# 添加无头模式
options.add_argument("-headless")
# 1.打开浏览器
driver = webdriver.Chrome(service=ChromeService(executable_path=chrome_driver_path), options=options)
driver.get("https://www.baidu.com/")
print(driver.title)

driver.quit()

打印:

"D:\Program Files\Python39\python.exe" D:/code/python/测试/images/_9test.py
百度一下,你就知道

进程已结束,退出代码0

8.2 页面加载策略

options.page_load_strategy = '加载方式'

页面加载方式主要有三种类型:

策略 说明
normal 默认值, 等待所有资源下载
eager DOM 访问已准备就绪, 但诸如图像的其他资源可能仍在加载,建议选
none 完全不会阻塞WebDriver,不建议选
options = webdriver.ChromeOptions()
options.page_load_strategy = 'eager'
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()),options=options)

driver.get默认情况下等待所有的资源加载完成之后才能继续往下执行,但是实际上主页面加载完成之后我们就可以继续执行自动化,若一直等待的话可能会造成页面超时、元素找不到等问题…

normal策略:

import time
import datetime

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
# 设置 浏览器驱动
chrome_driver_path = r"D:\Program Files\Python39\Scripts\chromedriver.exe"
# 浏览器参数配置
options = webdriver.ChromeOptions()

# 添加页面加载策略
options.page_load_strategy = 'normal'

# 1.打开浏览器
driver = webdriver.Chrome(service=ChromeService(executable_path=chrome_driver_path), options=options)
driver.get("https://haokan.baidu.com/")
print(driver.title)

driver.quit()

eager策略:

options.page_load_strategy = 'eager'

none策略:

options.page_load_strategy = 'none'

网站公告

今日签到

点亮在社区的每一天
去签到