wordpress建站实例,网站的会员功能怎么做,网站开发属于软件吗,产品设计创意图片Requests 是 Python 的第三方库#xff0c;主要用于发送 http 请求#xff0c;常用于接口自动化测试等。
Selenium 是一个用于 Web 应用程序的自动化测试工具。Selenium 测试直接运行在浏览器中#xff0c;就像真正的用户在操作一样。
本篇介绍一款将 Requests 和 Seleniu…Requests 是 Python 的第三方库主要用于发送 http 请求常用于接口自动化测试等。
Selenium 是一个用于 Web 应用程序的自动化测试工具。Selenium 测试直接运行在浏览器中就像真正的用户在操作一样。
本篇介绍一款将 Requests 和 Selenium 结合在一起的自动化测试工具 - Requestium
简介
Requestium 是一个 Python 库它将 Requests、Selenium 和 Parsel 的功能合并为一个用于自动化 web 操作的集成工具。
该库是为编写 web 自动化脚本而创建的这些脚本主要使用请求编写但能够在维护会话的同时无缝切换到网站中 JavaScript 密集部分的 Selenium。
Requestium 为 Requests 和 Selenium 添加了独立的改进并且每一个新功能都经过了延迟评估因此即使编写只使用 Requests 或 Selenium 的脚本它也很有用。
特点
1、在维护当前 web 会话的同时启用请求会话和 Selenium web 驱动程序之间的切换。
2、将 Parsel 的解析器集成到库中使 xpath、css 和 regex 的编写更加简洁。
3、改进了 Selenium 对动态加载元素的处理。
4、使 Selenium 中的 cookie 处理更加灵活。
5、使 Selenium 中的点击元素更加可靠。
6、本机支持 Chromedriver并添加自定义网络驱动程序。
安装
pip install requestium如果你使用 Requestium 的 Selenium 部分例如 Chromedriver那么你应该下载 Selenium Web 驱动程序。
快速上手
首先像处理请求一样创建一个会话如果使用 web 驱动程序可以选择添加参数。 #!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号咖啡加剁椒from requestium import Session, Keysoptions {arguments: [headless]}
s Session(webdriver_path./chromedriver, default_timeout15, webdriver_optionsoptions)由于无头模式很常见因此有一个快捷方式可以指定 headlessTrue。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号咖啡加剁椒from requestium import Session, Keyss Session(webdriver_path./chromedriver headlessTrue)你也可以在 Requestium 之外创建一个 Selenium 网络驱动程序并使用它
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号咖啡加剁椒from selenium import webdriver
from requestium import Session, Keysfirefox_driver webdriver.Firefox()s Session(driverfirefox_driver)你不需要解析响应当调用 xpathcss 或 re 时它会自动完成。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号咖啡加剁椒
title s.get(http://samplesite.com).xpath(//title/text()).extract_first(defaultDefault Title)与 Python 的标准 re 模块相比正则表达式需要更少的代码。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号咖啡加剁椒
response s.get(http://samplesite.com/sample_path)# Extracts the first match
identifier response.re_first(rID_\d\w\d, defaultID_1A1)# Extracts all matches as a list
users response.re(ruser_\d\d\d)你可以切换到使用 Selenium Webdriver 来运行任何 js 代码。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号咖啡加剁椒s.transfer_session_cookies_to_driver()
s.driver.get(http://www.samplesite.com/sample/process)最后你可以切换回使用 Requests。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号咖啡加剁椒s.transfer_driver_cookies_to_session()
s.post(http://www.samplesite.com/sample2, data{key1: value1})等待元素
ensure_element_by_ 方法等待元素在浏览器中加载并在加载后立即返回。它以 Selenium的 find_element_by_ 方法命名如果找不到元素它们会立即引发异常。
Requestium 可以等待一个元素处于以下任何状态
存在默认
可点击
看得见的
不可见可用于等待加载… GIF 消失等
这些方法对于单页面 Web 应用程序非常有用其中站点动态地更改其元素。我们通常最终完全用 ensure_element_by_ 调用替换我们的 find_element_by_ 调用因为它们更灵活。使用这些方法获取的元素具有新的 ensure_click 方法这使得点击不太容易失败。这有助于解决 Selenium 点击的许多问题。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号咖啡加剁椒s.driver.ensure_element_by_xpath(//li[classb1], stateclickable, timeout5).ensure_click()# We also added these methods named in accordance to Seleniums api design
ensure_element_by_id
ensure_element_by_name
ensure_element_by_link_text
ensure_element_by_partial_link_text
ensure_element_by_tag_name
ensure_element_by_class_name
ensure_element_by_css_selector添加 Cookie
ensure_add_cookie 方法使得添加 Cookie 更加稳健。Selenium 需要浏览器在能够添加 Cookie 之前处于 Cookie 的域中此方法为此提供了几种解决方法。如果浏览器不在 Cookie 域中它会先获取域然后再添加 Cookie。它还允许你在添加 Cookie 之前覆盖域并避免执行此 GET。域可以被覆盖为 ’’这将把 Cookie 的域设置为驱动程序当前所在的任何域。如果无法添加 cookie它会尝试使用限制性较小的域例如home.site.com - site.com进行添加然后在失败之前。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号咖啡加剁椒cookie {domain: www.site.com,secure: false,value: sd2451dgd13,expiry: 1516824855.759154,path: /,httpOnly: true,name: sessionid}
s.driver.ensure_add_cookie(cookie, override_domain)使用 Requestium 示例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号咖啡加剁椒from requestium import Session, Keys# If you want requestium to type your username in the browser for you, write it in here:
reddit_user_name s Session(./chromedriver, default_timeout15)
s.driver.get(http://reddit.com)
s.driver.find_element_by_xpath(//a[hrefhttps://www.reddit.com/login]).click()print(Waiting for elements to load...)
s.driver.ensure_element_by_class_name(desktop-onboarding-sign-up__form-toggler,statevisible).click()if reddit_user_name:s.driver.ensure_element_by_id(user_login).send_keys(reddit_user_name)s.driver.ensure_element_by_id(passwd_login).send_keys(Keys.BACKSPACE)
print(Please log-in in the chrome browser)s.driver.ensure_element_by_class_name(desktop-onboarding__title, timeout60, stateinvisible)
print(Thanks!)if not reddit_user_name:reddit_user_name s.driver.xpath(//span[classuser]//text()).extract_first()if reddit_user_name:s.transfer_driver_cookies_to_session()response s.get(https://www.reddit.com/user/{}/.format(reddit_user_name))cmnt_karma response.xpath(//span[classkarma comment-karma]//text()).extract_first()reddit_golds_given response.re_first(r(\d) gildings given out)print(Comment karma: {}.format(cmnt_karma))print(Reddit golds given: {}.format(reddit_golds_given))
else:print(Couldnt get user name)最后感谢每一个认真阅读我文章的人礼尚往来总是要有的虽然不是什么很值钱的东西如果你用得到的话可以直接拿走【文末小卡片领取】
这些资料对于【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴上万个测试工程师们走过最艰难的路程希望也能帮助到你