Explicit Waits

# Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0 ff = webdriver.Firefox()
ff.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))
finally:
ff.quit()
  • Expected Conditions

# Python
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))

Implicit Waits

# Python
from selenium import webdriver ff = webdriver.Firefox()
ff.implicitly_wait(10) # seconds
ff.get("http://somedomain/url_that_delays_loading")
myDynamicElement = ff.find_element_by_id("myDynamicElement")

Remote WebDriver

  • Taking a Screenshot

# Python
from selenium import webdriver driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.FIREFOX.copy())
driver.get("http://www.google.com")
driver.get_screenshot_as_file('/Screenshots/google.png')
  • Using a FirefoxProfile

# Python
from selenium import webdriver
fp = webdriver.FirefoxProfile()
# set something on the profile...
driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.FIREFOX, browser_profile=fp)
  • Using ChromeOptions

# Python
from selenium import webdriver
options = webdriver.ChromeOptions()
# set some options
driver = webdriver.Remote(desired_capabilities=options.to_capabilities())

Using a Proxy

  • Internet Explorer

# Python
from selenium import webdriver PROXY = "localhost:8080" # Create a copy of desired capabilities object.
desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER.copy()
# Change the proxy properties of that copy.
desired_capabilities['proxy'] = {
"httpProxy":PROXY,
"ftpProxy":PROXY,
"sslProxy":PROXY,
"noProxy":None,
"proxyType":"MANUAL",
"class":"org.openqa.selenium.Proxy",
"autodetect":False
} # you have to use remote, otherwise you'll have to code it yourself in python to
# dynamically changing the system proxy preferences
driver = webdriver.Remote("http://localhost:4444/wd/hub", desired_capabilities)
  • Chrome

  Is basically the same as internet explorer. It uses the same configuration on the machine as IE does (on windows). On Mac it uses the System Preference -> Network settings. On Linux it uses (on Ubuntu) System > Preferences > Network Proxy Preferences (Alternatively in “/etc/environment” set http_proxy). As of this writing it is unknown how to set the proxy programmatically.

  • Firefox

# Python
from selenium import webdriver
from selenium.webdriver.common.proxy import * myProxy = "host:8080" proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'ftpProxy': myProxy,
'sslProxy': myProxy,
'noProxy': '' # set this value as desired
}) driver = webdriver.Firefox(proxy=proxy) # for remote
caps = webdriver.DesiredCapabilities.FIREFOX.copy()
proxy.add_to_capabilities(caps) driver = webdriver.Remote(desired_capabilities=caps)

Data Driven Testing

# Python
# Collection of String values
source = open("input_file.txt", "r")
values = source.readlines()
source.close()
# Execute For loop for each String in the values array
for search in values:
driver.get('http://www.google.com')
driver.find_element_by_name("q").send_keys(search)
driver.find_element_by_id("btnG").click()
element = WebDriverWait(driver, 5).until(ExpectedConditions.presence_of_element_located((By.XPATH, "//*[contains(., 'Results')]"))
assert search in element.text

最新文章

  1. c# Entity DbArithmeticExpression arguments must have a numeric common type
  2. Media Queries使用方法
  3. httpclient提交json参数
  4. Java知识总结--Servlet&JSP
  5. 利用反射把查询到的Table、Reader转换成List、Model
  6. C# 多个线程一直跑着While(true)
  7. android studio adb 打不开
  8. Visual Studio warning MSB3270:There was a mismatch between the processor architecture of the project being built "MSIL"
  9. SharePoint使用BCS开发你第一个应用程序(三)
  10. Core Animation 文档翻译 (第六篇)
  11. Hexo + GitEE 搭建、备份、恢复、多终端
  12. 自学python之路(day5)
  13. nodejs electron 创建桌面应用
  14. leetcode96
  15. Vue-嵌套路由
  16. mysql登录密码相关
  17. about CSS3
  18. Linux(Redhat)安装python虚拟环境
  19. Python中pip install MySQL-python报错解决方法
  20. RSA 算法-MSDN文档

热门文章

  1. 基于mllib的协同过滤实战(电影推荐)
  2. LeetCode Add Two Numbers 两个数相加
  3. java Vamei快速教程04 封装和接口
  4. 全面了解linux情况常用命令
  5. OO终章
  6. web项目小总结
  7. 20180909 解析JS Cookie的设置,获取和检索
  8. 1045: [HAOI2008] 糖果传递
  9. vitrual box安装centos时一直黑屏的解决办法
  10. 无屏幕和键盘配置树莓派WiFi和SSH