Selenium - WebDriver Advanced Usage

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
时间: 2024-10-25 10:37:10

Selenium - WebDriver Advanced Usage的相关文章

Selenium Webdriver 学习总结-Advanced Usage-Cookie、Profile(七)

QQ群:136924235 论坛:http://bbs.shareku.com 一.如何使用Cookie代码示例: import org.openqa.selenium.Cookie; mport org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; import java.util.Set; public class

selenium webdriver (python)大全

webdriver的简介 硒2.0的主要新功能是集成的webdriver的API.webdriver的设计除了解决一些seleniumr-RC API的一些限制,与webdriver 的整合,将提供一个更简单,更简洁的编程接口.selenium webdriver会更好地支持动态的网页,页面本身被重新加载页面元素可能更改.webdriver的目标是提供一个设计良好的面向对象的API,提供了更好的支持现代先进的web-app测试. WebDriver与Selenium-RC相比,是如何来驱动浏览器

java selenium webdriver处理JS操作窗口滚动条

未经作者允许,禁止转载!!! import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class JS20161212 { public static void main(String[] args) throws InterruptedException { // TODO

Selenium Webdriver元素定位的八种常用方式

在使用selenium webdriver进行元素定位时,通常使用findElement或findElements方法结合By类返回的元素句柄来定位元素.其中By类的常用定位方式共八种,现分别介绍如下. 1. By.name() 假设我们要测试的页面源码如下: <button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba"><

Selenium webdriver 操作日历控件

一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 比如: 但是,有的日期控件是readonly的 比如12306的这个 <input id="train_date" class="inp-txt" type="text" value="2015-03-15" name="back_train_date" a

Selenium webdriver 学习总结-元素定位

Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要高些,使用id,name属性定位元素是最可靠,效率最高的一种办法. 1.工具选择:在我们开发测试脚本的过程中各个浏览器给我们也提供了方便定位元素的工具,我比较喜欢使用firefox的firebug工具,也是目前很多开发测试人员比较热衷的选择,原因是firefox是唯一能够集成selenium IDE

用Python selenium+webdriver的一个简单的登录自动化测试--豆丁网登录测试

#coding=utf-8 from selenium import webdriver #from selenium.webdriver.remote import switch_to #from selenium.webdriver.common import alert #import unittest  import time,os def users_zidian():  #用户名用例用一个字典实现参数化调用#     users={'zhengshuheng':'123456','[

【Selenium WebDriver】元素定位函数 FindElement

定位Web页面上的元素,用FindElement函数,它可以根据元素的不同属性来快速定位.具体的属性如下: 例子: HTML页面文件: 1 <html xmlns="http://www.w3.org/1999/xhtml" lang="en-us"> 2 <head> 3 <body> 4 <form name="loginForm"> 5 <label for="username

selenium webdriver处理浏览器Cookie

有时候我们需要验证浏览器中是否存在某个cookie,因为基于真实的cookie 的测试是无法通过白盒和集成测试完成的.WebDriver 提供了操作Cookie 的相关方法可以读取.添加和删除cookie 信息.WebDriver 操作cookie 的方法有:? getCookies() 获得所有cookie 信息? addCookie(cookie_dict) 添加cookie,必须有name 和value 值? deleteAllCookies() 删除所有cookie 信息? delete