0


【Selenium】常用的Selenium基础使用模板和简单封装

在这里插入图片描述

前言

近来又用上了 Selneium ,因为反复用到,所以在这里将一些常用的方法封装起来,方便后续的使用。
在这篇文章中,我们将探讨 Selenium 的基础模板和基础封装,以便更好地理解 Selenium 的使用方法。

Selenium的使用过程中,创建浏览器定位节点等是最常见的操作,
这里将常用的方法记录下来,以其减少后续在使用过程中的烦恼。

知识点📖📖

作用链接WebDriver文档https://www.selenium.dev/zh-cn/documentation/webdriver

实现

基础使用模板

Selenium4创建浏览器需要传入Service

在使用 Selenium 时候,有时候需要隐藏一些Selenium的特征。
参考这篇文章:【Selenium】Selenium绕过检测 & 隐藏特征

代码

代码释义

  • 使用了 ChromeDriverManager 来处理 chromedriver.exe 驱动的问题
  • 考虑到驱动 和 js文件的问题,webdriver_pathjs_file_path 都是可选参数,不传也不会报错
# -*- coding: utf-8 -*-# Name:         model_main.py# Author:       小菜# Date:         2023/5/20 10:45# Description:from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

definit_driver(webdriver_path=None, js_file_path=None)-> webdriver.Chrome:"""
    初始化浏览器驱动.

    Args:
        webdriver_path(str):浏览器驱动路径
        js_file_path(str):js文件路径,用于隐藏Selenium的特征

    Returns:
        driver: 浏览器驱动对象

    """
    driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager(path=webdriver_path).install()))if js_file_path:
        js =open(file=js_file_path, encoding='utf-8')
        driver.execute_cdp_cmd(
            cmd_args={'source': js},
            cmd="Page.addScriptToEvaluateOnNewDocument",)return driver

使用

然后再使用一个或多个显示等待,优雅极了!!

driver = init_driver()
wait_5 = WebDriverWait(driver,5)
wait_10 = WebDriverWait(driver,10)

简单封装

这份封装,称得上是优雅。使用了显示等待,兼容Selenium支持的各种定位方式!!!
可定位一个&多个节点、点击节点、文本输入、判断元素是否存在等~

代码

代码释义

  • waitvalue 为必选参数,其余的都是可选参数;
  • by 默认的定位方式是 XPATH,可以传入其它定位方式,如 id, name 等;
  • methodclickinput,再加上默认的 定位,已经可以覆盖99%的节点操作;
  • keyinput 时候的文本内容,使用pyperclipkey复制到剪切板,然后执行键盘事件进行 Ctrl + V 黏贴,效率更高;
  • 其余的看下面的 代码使用
import time

import pyperclip
from selenium.webdriver import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.common.exceptions import(
    TimeoutException,
    NoSuchElementException,
    InvalidSelectorException,)deflocate_and_operate_element(wait: WebDriverWait, by='xpath', value=None, method=None, key=None, is_more=False,
                               check_visibility=False):"""
     定位元素并执行操作。

     Args:
         wait (WebDriverWait): WebDriverWait 对象,用于显示等待元素出现
         by: 元素定位方式(例如 By.ID,By.XPATH)。
         value: 元素定位值(例如元素的 ID,XPath 表达式)。
         method (str): 操作方法(默认为 None)。
         key: 输入文本的值(默认为 None)。
         is_more (bool): 是否返回多个元素,默认为 False。
         check_visibility (bool): 是否检查元素可见性(默认为 False)。

     Returns:
         WebElement: 元素对象(当 method 参数为 None 且 is_more 参数为 False 时);
         None: 表示元素未在指定时间内出现或无法定位到指定的元素;
         List[WebElement]: 多个元素对象列表(当 method 参数为 None 且 is_more 参数为 True 时)。

     Raises:
         TimeoutException: 超时异常,表示元素未在指定时间内出现。
         NoSuchElementException: 未找到元素异常,表示无法定位到指定的元素。
         InvalidSelectorException: 选择器无效异常,表示使用了无效的选择器。
     """assertisinstance(wait, WebDriverWait),"wait 参数必须是 WebDriverWait 类型。"assert value,f"{value} 不能为空."try:
        element = wait.until(ec.presence_of_element_located((by, value)))# 根据指定的操作方法执行相应操作# 如果不指定方法, 默认为寻找对象是否存在ifnot method:if is_more:return wait.until(ec.presence_of_all_elements_located((by, value)))if check_visibility:return wait.until(ec.visibility_of_element_located((by, value)))return wait.until(ec.presence_of_element_located((by, value)))if method =='click':
            wait.until(ec.element_to_be_clickable((by, value))).click()
            time.sleep(2)elif method =='input':assert key
            wait.until(ec.visibility_of_element_located((by, value)))
            pyperclip.copy(key)
            time.sleep(0.2)
            element.send_keys(Keys.CONTROL,'A')
            time.sleep(0.2)
            element.send_keys(Keys.DELETE)
            time.sleep(0.2)
            element.send_keys(Keys.CONTROL,'V')
            time.sleep(0.2)except TimeoutException:print(f"超时:{by}={value}")returnNoneexcept NoSuchElementException:print(f"无法找到元素:{by}={value}")returnNoneexcept InvalidSelectorException:print(f"选择器无效:{by}={value}")returnNone

使用

结合上面的 基础使用模板

driver = init_driver()
wait_5 = WebDriverWait(driver,5)"""默认定位方式为xpath"""# 定位某个节点
locate_and_operate_element(wait_5, value='specify_node')# 定位多个节点
locate_and_operate_element(wait_5, value='specify_nodes', is_more=True)# 判断某个节点是否可见
locate_and_operate_element(wait_5, value='specify_node', check_visibility=True)# 点击指定节点
locate_and_operate_element(wait_5, value='specify_node', method='click')# 输入内容
locate_and_operate_element(wait_5, value='specify_node', method='input')

总结🎈🎈

本文介绍了在 Selenium使用中的基础使用模板和简单封装。

  • 基础使用模板,针对隐藏chromedriver.exe驱动和隐藏Selenium特征做了通用适配,从此再也不用担心驱动出错和Selenium被网站发现的问题;
  • 简单封装,针对定位节点、点击节点、输入文本等操作做了封装,并且针对代码给出了详细的注释。

优雅,实在是优雅!!!

后话

本次分享到此结束,
see you~~🐱‍🏍🐱‍🏍

标签: selenium python chrome

本文转载自: https://blog.csdn.net/weixin_45081575/article/details/130892147
版权归原作者 是小菜欸 所有, 如有侵权,请联系我们删除。

“【Selenium】常用的Selenium基础使用模板和简单封装”的评论:

还没有评论