Selenium自动化测试框架基本操作

发布于:2024-05-21 ⋅ 阅读:(213) ⋅ 点赞:(0)

前言

自动化测试前端页面

参考资料:https://www.youtube.com/watch?v=0O8124NyowY

1.准备条件

  • Selenium
  • PhantomJS
  • Chromedriver

Selenium: 是一个Web自动化工具,用于网站自动化测试。
PhantomJS:基于Webkit的无界面浏览器,会把网站加载到内存并执行页面上的 JavaScript。
Chromedriver:也是一个能够被Selenium驱动的浏览器,但是和PhantomJS区别是它有界面。

2.PhantomJS 的安装和使用

  • 下载地址
  • 环境要求

下载地址:http://phantomjs.org/download.html
环境要求:Python 3.6;PyCharm;Selenium;

PyCharm 是一款由 JetBrains 公司开发的 Python 集成开发环境(IDE)。它提供了许多功能,包括代码编辑器、调试器、版本控制集成和其他开发工具,使开发者能够更轻松地编写、调试和管理 Python 项目。PyCharm 有两个版本:专业版和社区版。专业版提供了更多高级功能,适用于大型项目和专业开发者,而社区版则是免费的,适用于个人开发者和小型项目。

Selenium 是一个用于自动化 web 浏览器操作的工具,它最初是为了进行网页测试而开发的,但后来被广泛用于执行各种网页自动化任务,如数据抓取、自动化表单填写等

3.vscode 运行py文件总是自动关闭浏览器

  • 原因分析:高级语言一般都有自动关闭的,释放内存机制,或者是工具内部存在(猜测)
  • 解决办法:打开vscode调试侧菜单,选中py调试,在最后的代码上添加断点

如:
在这里插入图片描述

(实例一)简单的实力代码,打开页面,填写数据,执行搜索

实例代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def search_topic(topic):
    # 定义浏览器初始化的URL
    URL = "https://www.baidu.com/"
    # 定义搜索框和搜索按钮的id
    SEARCH_BOX_ID = 'kw'
    SEARCH_BUTTON_ID = 'su'

    try:
        driver = webdriver.Chrome()
        driver.get(URL)

        # 设置隐式等待时间
        driver.implicitly_wait(10)

        # 使用显式等待,等待搜索框出现
        search_box = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, SEARCH_BOX_ID))
        )

        search_box.send_keys(topic)

        # 使用显式等待,等待搜索按钮出现
        search_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.ID, SEARCH_BUTTON_ID))
        )

        search_button.click()

    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        # 确保在所有情况下都会关闭浏览器
        driver.quit()

if __name__ == '__main__':
    search_topic('福建号')

4.获取对应的标签对象总结

获取方式总结

备注:注意selenium版本,低版本使用的一般是“driver.find_element_by_id(element_id)”

from selenium.webdriver.common.by import By

# 根据xpath选择元素
driver.find_element(By.XPATH, '//*[@id="kw"]') 
# 根据css选择器选择元素
driver.find_element(By.CSS_SELECTOR, '#kw') 
# 根据name属性值选择元素
driver.find_element(By.NAME, 'wd') 
# 根据类名选择元素
driver.find_element(By.CLASS_NAME, 's_ipt') 
# 根据链接文本选择元素
driver.find_element(By.LINK_TEXT, 'hao123') 
# 根据包含文本选择
driver.find_element(By.PARTIAL_LINK_TEXT, 'hao') 
# 根据标签名选择
driver.find_element(By.TAG_NAME, 'title') 
# 根据id选择
driver.find_element(By.ID, 'su') 

(实例二)获取标签实例

示例代码:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

def initialize_driver():
    options = Options()
    driver = webdriver.Chrome(options=options)
    return driver

def find_element_by_id(driver, element_id):
    try:
        # element = driver.find_element_by_id(element_id)
        element = driver.find_element(By.ID, element_id) 
        print(element)
    except NoSuchElementException:
        print(f"Element with ID '{element_id}' not found.")

def find_elements_by_id(driver, element_id):
    try:
        # elements = driver.find_elements_by_id(element_id)
        elements = driver.find_elements(By.ID, element_id)
        print(elements)
    except NoSuchElementException:
        print(f"Elements with ID '{element_id}' not found.")

def find_element_by_class(driver, class_name):
    try:
        # element = driver.find_element_by_class_name(class_name)
        element = driver.find_element(By.CLASS_NAME, class_name)
        print(element)
    except NoSuchElementException:
        print(f"Element with class '{class_name}' not found.")

def find_element_by_xpath(driver, xpath):
    try:
        # element = driver.find_element_by_xpath(By.XPATH, xpath)
        element = driver.find_element(By.XPATH, xpath)
        print(element)
    except NoSuchElementException:
        print(f"Element with XPath '{xpath}' not found.")

def find_element_by_link_text(driver, link_text):
    try:
        # element = driver.find_element_by_link_text(link_text)
        element = driver.find_element(By.LINK_TEXT, link_text)
        print(element)
    except NoSuchElementException:
        print(f"Element with link text '{link_text}' not found.")

def find_element_by_partial_link_text(driver, partial_link_text):
    try:
        # element = driver.find_element_by_partial_link_text(partial_link_text)
        element = driver.find_element(By.PARTIAL_LINK_TEXT, partial_link_text)
        print(element)
    except NoSuchElementException:
        print(f"Element with partial link text '{partial_link_text}' not found.")

def find_elements_by_tag_name(driver, tag_name):
    try:
        # elements = driver.find_elements_by_tag_name(tag_name)
        elements = driver.find_elements(By.TAG_NAME, tag_name)
        print(elements)
    except NoSuchElementException:
        print(f"Elements with tag name '{tag_name}' not found.")

def retrieve_tags():
    driver = initialize_driver()
    driver.get('https://www.douban.com')

    # 使用封装的函数进行元素查找
    find_element_by_id(driver, 'anony-nav')
    find_elements_by_id(driver, 'anony-nav')
    find_element_by_class(driver, 'anony-nav-links')
    find_element_by_xpath(driver, '//*[@id="anony-nav"]')
    find_element_by_link_text(driver, '下载豆瓣 App')
    find_element_by_partial_link_text(driver, '豆瓣')
    find_elements_by_tag_name(driver, 'div')
    find_element_by_link_text(driver, '下载豆瓣 App')

    # 关闭WebDriver
    driver.quit()

if __name__ == "__main__":
    retrieve_tags()

(实例三)切换窗口

实例:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException

def navigate_and_click_element(url, xpath):
    """
    打开给定URL,等待页面加载,然后定位并点击给定的XPath元素。
    """
    try:
        # 使用Chrome驱动,需要确保chromedriver.exe在系统PATH中或指定具体路径
        options = webdriver.ChromeOptions()
        # 假设chromedriver.exe在当前目录下
        options.add_argument("--webdriver.chrome.driver=./chromedriver")

        # 使用with语句确保资源正确释放
        with webdriver.Chrome(options=options) as browser:
            browser.get(url)

            # 添加等待,直到元素可见
            wait = WebDriverWait(browser, 10)
            element = wait.until(EC.visibility_of_element_located((By.XPATH, xpath)))
            element.click()

            # 获取当前页面
            current_window = driver.window_handles
            print(current_window)
			
			# 将当前的WebDriver对象切换到指定窗口进行操作。
            driver.switch_to_window(current_window[1])

    except TimeoutException:
        print("页面加载超时")
    except NoSuchElementException:
        print("无法找到指定的XPath元素")
    except Exception as e:
        print(f"发生意外错误: {e}")

if __name__ == "__main__":
    navigate_and_click_element("https://www.douban.com/", '//*[@id="anony-nav"]/div[1]/ul/li[2]/a')

5.登录操作实例代码

实例:

import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 假设从环境变量中获取用户名和密码
import os
username = os.getenv('DOUBAN_USERNAME', '登录账号xxxx')
password = os.getenv('DOUBAN_PASSWORD', '登录密码xxxx')

if __name__ == "__main__":
    driver = webdriver.Chrome()

    driver.get('https://www.douban.com/')

    try:
        # 尝试找到iframe,并切换到它
        driver.switch_to.frame(driver.find_element(By.TAG_NAME, 'iframe'))
    except NoSuchElementException:
        print("无法找到或切换到iframe")
        driver.quit()
    
    try:
        # 使用显式等待,等待登录按钮出现
        login_button = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[1]/ul[1]/li[2]'))
        )
        login_button.click()
        
        # 输入用户名和密码
        username_input = driver.find_element(By.XPATH, '//*[@id="username"]')
        username_input.send_keys(username)

        password_input = driver.find_element(By.XPATH, '//*[@id="password"]')
        password_input.send_keys(password)

        # 点击登录按钮
        login_submit = driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div[5]/a')
        login_submit.click()
        print('点击登录按钮')
    except NoSuchElementException as e:
        print(f"元素未找到: {e}")
        driver.quit()

    # 移除用户名和密码的硬编码,改为从环境变量读取,提高了代码的安全性和灵活性
    # 使用显式等待代替固定的time.sleep,提高了程序的效率和响应能力
    # 添加了异常处理,使程序在遇到错误时能够更加优雅地处理