二次封装selenium功能

文章介绍了如何更新SeleniumWebDriver的初始化方法,使用新的服务类ChromeService,EdgeService和FirefoxService,以及对BasePage类进行元素操作的封装,包括键盘、鼠标和窗口处理等。

selenium官网介绍和说明:https://www.selenium.dev/zh-cn/documentation/webdriver/getting_started/first_script/
webdriver驱动文件的路径是相对的,webdriver驱动文件的获取是自动下载的代码下载保存的,可参考:https://blog.csdn.net/weixin_45531218/article/details/133977104

初始化函数使用了新的写法,之前的用的旧的方法,要么就是报错,要么提示不适用。现在换成新版的写法,没有报错过。

当前初始化语句
chromeService = ChromeService(executable_path = ‘驱动路径’)
webdriver.Chrome(service = chromeService)

旧版本初始化语句
webdriver.Chrome(executable_path = ‘驱动路径’)

如下是服务初始化、访问页面及元素查找等基础方法的封装。

from selenium import webdriver
from selenium.common import NoSuchElementException, TimeoutException
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.actions.wheel_input import ScrollOrigin
from selenium.webdriver.edge.options import Options as edgeOptions
from selenium.webdriver.edge.service import Service as edgeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class BasePage():
    '''将selenium的功能二次封装成'''

   def __init__(self, browser_name=conf.BROWSER_NAME):
        self.browser_name = browser_name
        chromedriver_path = conf.getdriverpath(browser_name) #conf是一个配置类对象,其中配置了浏览器名称、对应的驱动文件完整路径获取方法
        if self.browser_name == 'chrome':
            service = ChromeService(executable_path=chromedriver_path)
            options = ChromeOptions()
            options.add_experimental_option('detach', True)
            self.driver = webdriver.Chrome(service=service, options=options)
        elif self.browser_name == 'edge':
            service = edgeService(executable_path=chromedriver_path)
            options = edgeOptions()
            options.add_experimental_option('detach', True)
            self.driver = webdriver.Edge(service=service, options=options
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值