Tag Archives: python

Selenium 中ExpectedConditions 用法说明(最全整理)

首先导入包 [cc lang=”python”]from selenium.webdriver.support import expected_conditions as EC[/cc] 1,判断当前页面的title是否精确等于预期 [cc lang=”text”]titleIs( String title) EC.title_is(‘Google’) [/cc] 2,判断当前页面的title是否包含预期字符串 [cc lang=”text”]titleContains( String title)[/cc] [cc lang=”python”]if EC.title_contains(‘google’): ##等于 if ‘google’ in driver.title: [/cc] 3,判断当前页面的url是否精确等于预期 [cc lang=”text”]urlToBe( String url)[/cc] 4,判断当前页面的url是否包含预期字符串 [cc lang=”text”]urlContains( String fraction)[/cc] 5,当前URL字符串正则表达式匹配 [cc lang=”text”]urlMatches( String regex)[/cc] 6,判断元素是否出现,只要有一个元素出现,就通过。(出现不代表可见) 判断是否至少有 1 个元素存在于 dom 树中。举个例子,如果页面上有 n 个元素的 class 都是’column-md-3’,那么只要有 1… Read More »

mitmproxy抓https出现502Bad Gateway. unable to get local issuer certificate的解决方法

502 Bad Gateway. Certificate Verification Error for www.xxxx.com: unable to get local issuer certificate (errno: 20, depth: 0) 想到可能是当前证书不支持,网上找一个最新的cacert.pem替换certifi包(xx\python36\Lib\site-packages\certifi)目录的证书,再次使用可以抓到包了。 参考资料 https://github.com/mitmproxy/mitmproxy/issues/1608 https://curl.haxx.se/docs/caextract.html

使用MitmProxy绕过Content Security Policy (CSP)

这几天测试用Selenium 和 Chrome插件都无法在 谷歌插件商店https://chrome.google.com/webstore/category/extensions?hl=en 实现JS注入 检查发现响应头里面有一个 ‘content-security-policy’,错误提示:[cc lang=”text”]”Refused to execute inline script because it violates the following Content Security Policy directive: “script-src ‘report-sample’ ‘nonce-wNKF1pjNhZent+g6jyFL9g’ ‘unsafe-inline’ ‘unsafe-eval’”. Note that ‘unsafe-inline’ is ignored if either a hash or nonce value is present in the source list.”[/cc] CSP全称Content Security Policy ,可以直接翻译为内容安全策略,说白了,就是为了页面内容安全而制定的一系列防护策略. 通过CSP所约束的的规责指定可信的内容来源(这里的内容可以指脚本、图片、iframe、fton、style等等可能的远程的资源)。通过CSP协定,让WEB处于一个安全的运行环境中. 我这里使用MitmProxy绕过CSP执行JS注入 脚本如下 [cc lang=”python”] from bs4… Read More »

Mitmproxy + Python安装和使用的一些问题

安装: [cc lang=”python”]pip3 install mitmproxy [/cc] 国内: [cc lang=”python”]pip install mitmproxy -i http://pypi.douban.com/simple –trusted-host pypi.douban.com[/cc] 如果’Microsoft Visual C++ Build Tools’错误,可以在https://visualstudio.microsoft.com/zh-hans/downloads/ 直接下载安装即可 如果’Dll load error…..’ 错误,安装VC https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads 检查是否安装成功: [cc lang=”python”]mitmdump –version[/cc] 版本升级: [cc lang=”python”]pip install –upgrade mitmproxy[/cc] 安装证书 访问 http://mitm.it/ 自动安装 手动: 命令 certutil -addstore root mitmproxy-ca-cert.cer 自定义脚本 : 脚本内容是实时修改生效 [cc lang=”python”]mitmdump -s xxx.py[/cc] 指定代理 [cc… Read More »

Python Selenium提供的execute_script调用js的一些操作

调用js方法 execute_script(script, *args) 在当前窗口/框架 同步执行javaScript 脚本:JavaScript的执行。 *参数:适用任何JavaScript脚本。 使用: [cc lang=”python”]driver.execute_script(‘document.title’)[/cc] 滚动到目标视图 [cc lang=”python”]target = driver.find_element_by_xxxx() driver.execute_script(“arguments[0].scrollIntoView();”, target)[/cc] 通过执行JavaScript中的代码删除target JS code [cc lang=”js”]document.getElementsByClassName(“site-nav-right fr”)[0].childNodes[1].removeAttribute(“target”)[/cc] python [cc lang=”python”]js=’arguments[0].removeAttribute(argument[1])’ driver.execute_script(js,login_link,”target”)[/cc]

Selenium:利用select模块处理下拉框

在利用selenium进行UI自动化测试过程中,经常会遇到下拉框选项,这篇博客,就介绍下如何利用selenium的Select模块来对标准select下拉框进行操作。。。 首先导入Select模块: [cc lang=”python”] # coding=utf-8 from selenium import webdriver from selenium.webdriver.support.select import Select [/cc] 1、Select提供了三种选择某一项的方法 [cc lang=”python”] select_by_index # 通过索引定位 select_by_value # 通过value值定位 select_by_visible_text # 通过文本值定位 [/cc] 注意事项: index索引是从“0”开始; value是option标签的一个属性值,并不是显示在下拉框中的值; visible_text是在option标签中间的值,是显示在下拉框的值; 2、Select提供了三种返回options信息的方法 [cc lang=”python”] options # 返回select元素所有的options all_selected_options # 返回select元素中所有已选中的选项 first_selected_options # 返回select元素中选中的第一个选项[/cc] 注意事项: 这三种方法的作用是查看已选中的元素是否是自己希望选择的: options:提供所有选项的元素列表; all_selected_options:提供所有被选中选项的元素列表; first_selected_option:提供第一个被选中的选项元素; 3、Select提供了四种取消选中项的方法 [cc lang=”python”] deselect_all # 取消全部的已选择项… Read More »

Python常用模块

Flask  Web微内核中文文档: https://dormousehole.readthedocs.io/en/latest/ Jinja文档 http://jinja.pocoo.org/docs Requests 网络请求模块 https://requests.readthedocs.io/zh_CN/latest/index.html Beautiful Soup 4.4.0  数据提取 https://beautifulsoup.readthedocs.io/zh_CN/latest/ Selenium 浏览器测试 http://www.selenium.org.cn/    https://www.selenium.dev/