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

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

被折叠的 条评论
为什么被折叠?



