利用AI优化论文写作,10款实用工具提升效率。

核心内容摘要

万和制药和安®复方氨基酸胶囊(8-11):换季时的优质健康伴侣
Z-Image-GGUF文生图模型5分钟快速上手:阿里通义开源AI绘画一键部署教程

基于Spring Boot的智能客服问答系统:架构设计与工程实践

点击文末小卡片免费获取软件测试全套资料资料在手涨薪更快概述selenium是网页应用中最流行的自动化测试工具可以用来做自动化测试或者浏览器爬虫等。

官网地址为相对于另外一款web自动化测试工具QTP来说有如下优点免费开源轻量级不同语言只需要一个体积很小的依赖包支持多种系统包括WindowsMacLinux支持多种浏览器包括ChromeFireFoxIEsafariopera等支持多语言包括JavaCpythonc#等主流语言支持分布式测试用例执行pythonselenium环境安装首先需要安装python推荐

7环境然后直接用pip install selenium安装依赖包即可。

另外还需要下载浏览器相应的webdriver驱动程序注意下载的驱动版本一定要匹配浏览器版本。

Firefox浏览器驱动Chrome浏览器驱动IE浏览器驱动Edge浏览器驱动Opera浏览器驱动下载以后可以把驱动程序加到环境变量这样使用时就不用手动指定驱动程序路径。

使用selenium启动浏览器可以在python中使用下面的代码启动一个Chrome浏览器然后控制这个浏览器的行为或者读取数据。

from selenium import webdriver # 启动Chrome浏览器要求chromedriver驱动程序已经配置到环境变量 # 将驱动程序和当前脚本放在同一个文件夹也可以 driver webdriver.Chrome() # 手动指定驱动程序路径 driver webdriver.Chrome(rD:/uusama/tools/chromedriver.exe) driver webdriver.Ie() # Internet Explorer浏览器 driver webdriver.Edge() # Edge浏览器 driver webdriver.Opera() # Opera浏览器 driver webdriver.PhantomJS() # PhantomJS driver.get(http://uusama.com) # 打开指定路径的页面启动的时候还可以设置启动参数比如下面的代码实现启动时添加代理并且忽略https证书校验。

from selenium import webdriver # 创建chrome启动选项对象 options webdriver.ChromeOptions() options.add_argument(--proxy-server

127.

0.

1:

# 设置代理 options.add_argument(---ignore-certificate-errors) # 设置忽略https证书校验 options.add_experimental_option(excludeSwitches, [enable-logging]) # 启用日志 # 设置浏览器下载文件时保存的默认路径 prefs {download.default_directory: get_download_dir()} options.add_experimental_option(prefs, prefs) driver webdriver.Chrome(optionsoptions)启动的时候还可以设置启动参数比如下面的代码实现启动时添加代理并且忽略https证书校验。

from selenium import webdriver # 创建chrome启动选项对象 options webdriver.ChromeOptions() options.add_argument(--proxy-server

127.

0.

1:

# 设置代理 options.add_argument(---ignore-certificate-errors) # 设置忽略https证书校验 options.add_experimental_option(excludeSwitches, [enable-logging]) # 启用日志 # 设置浏览器下载文件时保存的默认路径 prefs {download.default_directory: get_download_dir()} options.add_experimental_option(prefs, prefs) driver webdriver.Chrome(optionsoptions)一些非常有用的启动选项下面使用的options webdriver.ChromeOptions():options.add_argument(--proxy-server

127.

0.

1:

: 设置代理可以结合mitmproxy进行抓包等option.add_experimental_option(excludeSwitches, [enable-automation]): 设置绕过selenium检测options.add_argument(---ignore-certificate-errors): 设置忽略https证书校验options.add_experimental_option(prefs, {profile.managed_default_content_settings.images: 2}): 设置不请求图片模式加快页面加载速度chrome_options.add_argument(--headless): 设置无头浏览器selenium页面加载等待和检测使用selenium打开页面以后还不能立刻操作需要等到待处理页面元素加载完成这时就需要检测和等待页面元素加载。

使用time.sleep()等待最简单的方法就是打开页面以后使用time.sleep()强制等待一定时间该方法只能设置一个固定时间等待如果页面提前加载完成则会空等阻塞。

from time import sleep from selenium import webdriver driver webdriver.Chrome() driver.get(http://uusama.con) time.sleep(

print(load finish)使用implicitly_wait设置最长等待时间另外还可以使用implicitly_wait设置最长等待时间如果在给定时间内页面加载完成或者已经超时才会执行下一步。

该方法会等到所有资源全部加载完成也就是浏览器标签栏的loading图表不再转才会执行下一步。

有可能页面元素已经加载完成但是js或者图片等资源还未加载完成此时还需要等待。

另需注意使用implicitly_wait只需设置一次且对整个driver生命周期都起作用凡是遇到页面正在加载都会阻塞。

示例如下from selenium import webdriver driver webdriver.Chrome() driver.implicitly_wait(

# 设置最长等30秒 driver.get(http://uusama.com) print(driver.current_url) driver.get(http://baidu.com) print(driver.current_url)使用WebDriverWait设置等待条件使用WebDriverWaitselenium.webdriver.support.wait.WebDriverWait能够更加精确灵活地设置等待时间WebDriverWait可在设定时间内每隔一段时间检测是否满足某个条件如果满足条件则进行下一步操作如果超过设置时间还不满足则抛出TimeoutException异常其方法声明如下WebDriverWait(driver, timeout, poll_frequency

5, ignored_exceptionsNone)其中各参数含义如下driver浏览器驱动timeout最长超时时间默认以秒为单位poll_frequency检测的间隔步长时间默认为

5秒ignored_exceptions忽略的异常即使在调用until()或until_not()的过程中抛出给定异常也不中断WebDriverWait()一般配合until()或until_not()方法使用表示等待阻塞直到返回值为True或者False需要注意这两个方法的参数都需是可调用对象即方法名称可以使用expected_conditions模块中的方法或者自己封装的方法。

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 driver webdriver.Chrome() driver.get(http://baidu.com) # 判断id为input的元素是否被加到了dom树里并不代表该元素一定可见如果定位到就返回WebElement element WebDriverWait(driver, 5,

0.

.until(expected_conditions.presence_of_element_located((By.ID, s_btn_wr))) # implicitly_wait和WebDriverWait都设置时取二者中最大的等待时间 driver.implicitly_wait(

# 判断某个元素是否被添加到了dom里并且可见可见代表元素可显示且宽和高都大于0 WebDriverWait(driver,

.until(EC.visibility_of_element_located((By.ID, su))) # 判断元素是否可见如果可见就返回这个元素 WebDriverWait(driver,

.until(EC.visibility_of(driver.find_element(byBy.ID, valuekw)))下面列出expected_conditions常用的一些方法title_is: 判断当前页面title是否精确等于预期title_contains: 判断当前页面title是否包含预期字符串presence_of_element_located: 判断某个元素是否被加到了dom树里并不代表该元素一定可见visibility_of_element_located: 判断某个元素是否可见元素非隐藏并且元素的宽和高都不等于0visibility_of: 跟上面的方法做一样的事情只是上面的方法要传入locator这个方法直接传定位到的element就好了presence_of_all_elements_located: 判断是否至少有1个元素存在于dom树中。

举个例子如果页面上有n个元素的class都是column-md-3那么只要有1个元素存在这个方法就返回Truetext_to_be_present_in_element: 判断某个元素中的text是否包含了预期的字符串text_to_be_present_in_element_value: 判断某个元素中的value属性是否包含了预期的字符串frame_to_be_available_and_switch_to_it: 判断该frame是否可以switch进去如果可以的话返回True并且switch进去否则返回Falseinvisibility_of_element_located: 判断某个元素中是否不存在于dom树或不可见element_to_be_clickable: 判断某个元素中是否可见并且是enable的这样的话才叫clickablestaleness_of: 等某个元素从dom树中移除注意这个方法也是返回True或Falseelement_to_be_selected: 判断某个元素是否被选中了,一般用在下拉列表element_selection_state_to_be: 判断某个元素的选中状态是否符合预期element_located_selection_state_to_be: 跟上面的方法作用一样只是上面的方法传入定位到的element而这个方法传入locator检测document是否加载完成另外还可以使用driver.execute_script(return document.readyState;) complete来检测document是否加载完成。

注意document加载完成是不包括那种异步加载ajax请求动态渲染dom的这种需要使用WebDriverWait检测某个元素是否渲染完成。

selenium元素定位和读取查找元素selenium提供了一系列api方便获取chrome中的元素这些API都返回WebElement对象或其列表如find_element_by_id(id): 查找匹配id的第一个元素find_element_by_class_name(): 查找匹配class的第一个元素find_elements_by_xpath(): 查找匹配xpath的所有元素find_elements_by_css_selector(): 查找匹配css选择器的所有元素其实可以看WebDriver类里面的实现源码其核心实现都是调用两个基本函数find_element(self, byBy.ID, valueNone): 查找匹配策略的第一个元素find_elements(self, byBy.ID, valueNone): 查找匹配策略的所有元素其中by参数可以是ID, CSS_SELECTOR, CLASS_NAME, XPATH等。

下面举几个简单的例子通过xpath查询包含文本登录的第一个元素 find_element_by_xpath(//*[contains(text(),登录)])查询包含类名refresh的所有元素 find_elements_by_class_name(refresh)查询table表格的第二行 find_element_by_css_selector(table tbody tr:nth(

)dom元素交互上面介绍的元素查找结果WebElement对象常用的api有:element.text: 返回元素的文本内容包括所有后代节点的内容注意如果元素displaynone则返回为空字符串element.screenshot_as_png: 元素的截图element.send_keys(input): 元素输入框输入input字符串element.get_attribute(data-v): 获取data-v名称属性值除了自定义节点属性还可以获取如textContent等属性element.is_displayed(): 判断元素是否用户可见element.clear(): 清除元素文本element.click(): 点击元素如果元素不可点击会抛出ElementNotInteractableException异常element.submit(): 模拟表单提交查找元素失败处理如果找不到指定元素则会抛出NoSuchElementException异常而且需要注意displaynone的元素是可以获取到的凡是在dom节点中的元素都可以获取到。

而且实际使用的时候要注意一些js代码动态创建的元素可能需要轮询获取或者监控。

一个检查是否存在指定元素的方法如下def check_element_exists(xpath): try: driver.find_element_by_xpath(xpath) except NoSuchElementException: return False return Trueselenium交互控制ActionChains动作链webdriver通过ActionChains对象来模拟用户操作该对象表示一个动作链路队列所有操作会依次进入队列但不会立即执行直到调用perform()方法时才会执行。

其常用方法如下click(on_elementNone): 单击鼠标左键click_and_hold(on_elementNone): 点击鼠标左键不松开context_click(on_elementNone): 点击鼠标右键double_click(on_elementNone): 双击鼠标左键send_keys(*keys_to_send): 发送某个键到当前焦点的元素send_keys_to_element(element, *keys_to_send): 发送某个键到指定元素key_down(value, elementNone): 按下某个键盘上的键key_up(value, elementNone): 松开某个键drag_and_drop(source, target): 拖拽到某个元素然后松开drag_and_drop_by_offset(source, xoffset, yoffset): 拖拽到某个坐标然后松开move_by_offset(xoffset, yoffset): 鼠标从当前位置移动到某个坐标move_to_element(to_element): 鼠标移动到某个元素move_to_element_with_offset(to_element, xoffset, yoffset): 移动到距某个元素左上角坐标多少距离的位置perform(): 执行链中的所有动作release(on_elementNone): 在某个元素位置松开鼠标左键模拟鼠标事件下面代码模拟鼠标移动点击拖拽等操作注意操作时需要等待一定时间否则页面还来不及渲染。

from time import sleep from selenium import webdriver # 引入 ActionChains 类 from selenium.webdriver.common.action_chains import ActionChains driver webdriver.Chrome() driver.get(https://www.baidu.cn) action_chains ActionChains(driver) target driver.find_element_by_link_text(搜索) # 移动鼠标到指定元素然后点击 action_chains.move_to_element(target).click(target).perform() time.sleep(

# 也可以直接调用元素的点击方法 target.click() time.sleep(

# 鼠标移动到(10,

坐标处 action_chains.move_by_offset(10,

.perform() time.sleep(

# 鼠标移动到距离元素target(10,

处 action_chains.move_to_element_with_offset(target, 10,

.perform() time.sleep(

# 鼠标拖拽将一个元素拖动到另一个元素 dragger driver.find_element_by_id(dragger) action.drag_and_drop(dragger, target).perform() time.sleep(

# 也可以使用点击 - 移动来实现拖拽 action.click_and_hold(dragger).release(target).perform() time.sleep(

action.click_and_hold(dragger).move_to_element(target).release().perform()模拟键盘输入事件通过send_keys模拟键盘事件常用有send_keys(Keys.BACK_SPACE): 删除键BackSpacesend_keys(Keys.SPACE): 空格键(Space)send_keys(Keys.TAB): 制表键(Tab)send_keys(Keys.ESCAPE): 回退键Escsend_keys(Keys.ENTER): 回车键Entersend_keys(Keys.F

: 键盘 F1send_keys(Keys.CONTROL,a): 全选CtrlAsend_keys(Keys.CONTROL,c): 复制CtrlCsend_keys(Keys.CONTROL,x): 剪切CtrlXsend_keys(Keys.CONTROL,v): 粘贴CtrlV示例定位到输入框然后输入内容# 输入框输入内容 driver.find_element_by_id(kw).send_keys(uusamaa) # 模拟回车删除多输入的一个字符a driver.find_element_by_id(kw).send_keys(Keys.BACK_SPACE)警告框处理用于处理调用alert弹出的警告框。

driver.switch_to_alert(): 切换到警告框text返回alert/confirm/prompt中的文字信息比如js调用alert(failed)则会获取failed字符串accept()接受现有警告框dismiss()关闭现有警告框send_keys(keysToSend)将文本发送至警告框selenium浏览器控制基本常用api下面列出一些非常实用的浏览器控制api:driver.current_url: 获取当前活动窗口的urldriver.switch_to_window(windowName): 移动到指定的标签窗口driver.switch_to_frame(frameName): 移动到指定名称的iframedriver.switch_to_default_content(): 移动到默认文本内容区driver.maximize_window(): 将浏览器最大化显示driver.set_window_size(480,

: 设置浏览器宽

高800显示driver.forword(), driver.back(): 浏览器前进和后退driver.refresh(): 刷新页面driver.close(): 关闭当前标签页driver.quiit(): 关闭整个浏览器driver.save_screenshot(screen.png): 保存页面截图driver.maximize_window(): 将浏览器最大化显示browser.execute_script(return document.readyState;): 执行js脚本selenium读取和加载cookie使用get_cookies和add_cookie可以实现将cookie缓存到本地然后启动时加载这样可以保留登录态。

实现如下import os import json from selenium import webdriver driver webdriver.Chrome() driver.get(https://www.baidu.cn) # 读取所有cookie并保存到文件 cookies driver.get_cookies() cookie_save_path cookie.json with open(cookie_save_path, w, encodingutf-

as file_handle: json.dump(cookies, file_handle, ensure_asciiFalse, indent

# 从文件读取cookie并加载到浏览器 with open(cookie_save_path, r, encodingutf-

as file_handle: cookies json.load(file_handle) for cookie in cookies: driver.add_cookie(cookie)selenium打开新的标签页窗口使用driver.get(url)会默认在第一个标签窗口打开指定连接点击页面中的_blank的链接时也会打开一个新的标签窗口。

还可以用下面的方式手动打开一个指定页面的标签窗口需要注意打开新窗口或者关闭以后还需要手动调用switch_to.window切换当前活动的标签窗口否则会抛出NoSuchWindowException异常。

from selenium import webdriver driver webdriver.Chrome() driver.get(https://www.baidu.cn) new_tab_url http://uusama.com driver.execute_script(fwindow.open({new_tab_url}, _blank);) time.sleep(

# 注意必须调用switch_to.window手动切换window否则会找不到tab view # 聚焦到新打开的tab页面然后关闭 driver.switch_to.window(driver.window_handles[1]) time.sleep(

driver.close() # 关闭当前窗口 # 手动回到原来的tab页面 driver.switch_to.window(driver.window_handles[0]) time.sleep(

除了使用execute_script外还可以使用模拟打开新tab页按键的方式新建一个标签页窗口driver.find_element_by_tag_name(body).send_keys(Keys.CONTROL t)ActionChains(driver).key_down(Keys.CONTROL).send_keys(t).key_up(Keys.CONTROL).perform()selenium一些问题记录获取隐藏元素的文本内容如果一个元素是隐藏的即displaynone虽然可以通过find_element查找到该元素但是用element.text属性是获取不到该元素文本内容的其值是空字符串这时可以用下面的方式获取element driver.find_element_by_id(uusama) driver.execute_script(return arguments[0].textContent, element) driver.execute_script(return arguments[0].innerHTML, element) # 相应的也可以把隐藏的元素设置为非隐藏 driver.execute_script(arguments[0].style.display block;, element)浏览器崩溃WebDriverException异常处理比如在Chrome中长时间运行一个页面会出现Out Of Memory内存不足的错误此时WebDriver会抛出WebDriverException异常基本所有api都会抛出这个异常这个时候需要捕获并进行特殊处理。

我的处理方式是记录页面的一些基本信息比如urlcookie等并定期写入到文件中如果检测到该异常则重启浏览器并且加载url和cookie等数据。

selenium抓取页面请求数据网上有通过driver.requests或者通过解析日志来获取页面请求的方式但是我感觉都不是很好使。

最后使用mitmproxy代理进行抓包处理然后启动selenium时填入代理来实现。

proxy.py为在mitmproxy基础上封装的自定义代理请求处理其代码如下import os import gzip from mitmproxy.options import Options from mitmproxy.proxy.config import ProxyConfig from mitmproxy.proxy.server import ProxyServer from mitmproxy.tools.dump import DumpMaster from mitmproxy.http import HTTPFlow from mitmproxy.websocket import WebSocketFlow class ProxyMaster(DumpMaster): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def run(self, funcNone): try: DumpMaster.run(self, func) except KeyboardInterrupt: self.shutdown() def process(url: str, request_body: str, response_content: str): # 抓包请求处理可以在这儿转存和解析数据 pass class Addon(object): def websocket_message(self, flow: WebSocketFlow): # 监听websockt请求 pass def response(self, flow: HTTPFlow): # 避免一直保存flow流导致内存占用飙升 # flow.request.headers[Connection] close # 监听http请求响应并获取请求体和响应内容 url flow.request.url request_body flow.request response_content flow.response # 如果返回值是压缩的内容需要进行解压缩 if response_content.data.content.startswith(b\x1f\x8b\x

: response_content gzip.decompress(response_content.data.content).decode(utf-

Addon.EXECUTOR.submit(process, url, request_body, response_content) def run_proxy_server(): options Options(listen_host

0.

0.

0, listen_port

config ProxyConfig(options) master ProxyMaster(options, with_termlogFalse, with_dumperFalse) master.server ProxyServer(config) master.addons.add(Addon()) master.run() if __name__ __main__: with open(proxy.pid, modew) as fin: fin.write(os.getpid().__str__()) run_proxy_server()在使用mitmproxy过程中随着时间推移proxy.py会出现占用内存飙升的问题在github的issue区有人也遇到过有说是因为http连接keep-alivetrue请求会一直保存不会释放导致请求越多越占用内存然后通过添加flow.request.headers[Connection] close来手动关闭连接我加了以后有一定缓解但还是不能从根本上解决。

最后通过写入proxy.pid记录代理程序进程然后用另外一个程序定时重启proxy.py来解决内存泄漏的问题。

最后感谢每一个认真阅读我文章的人礼尚往来总是要有的虽然不是什么很值钱的东西如果你用得到的话可以直接拿走这些资料对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴我走过了最艰难的路程希望也能帮助到你凡事要趁早特别是技术行业一定要提升技术功底。

真人荫道口100张-真人荫道口100张应用

百度百家号客服电话人工服务

123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123