WebDriver介绍
Fetching a Page
driver.get("http://www.google.com")
Locating UI Elements (WebElements)
- By ID
<div id="coolestWidgetEvah">...</div>
driver.find_element_by_id("coolestWidgetEvah")
or
from selenium.webdriver.common.by import By
element = driver.find_element(by=By.ID, value="coolestWidgetEvah")
2. By Class Name:
<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>
cheeses = driver.find_elements_by_class_name("cheese")
or
from selenium.webdriver.common.by import By
cheeses = driver.find_elements(By.CLASS_NAME, "cheese")
3. By Tag Name:
<iframe src="..."></iframe>
frame = driver.find_element_by_tag_name("iframe")
or
from selenium.webdriver.common.by import By
frame = driver.find_element(By.TAG_NAME, "iframe")
4. By Name:
<input name="cheese" type="text"/>
cheese = driver.find_element_by_name("cheese")
or
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.NAME, "cheese")
5. By Link Text:
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>
cheese = driver.find_element_by_partial_link_text("cheese")
or
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.PARTIAL_LINK_TEXT, "cheese")
6. By CSS:
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>
cheese = driver.find_element_by_css_selector("#food span.dairy.aged")
or
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.CSS_SELECTOR, "#food span.dairy.aged")
7. By XPath:
Driver Tag and Atttribute Name Attribute Values Native XPath Support
HtmlUnit Driver Lower-cased As they appear in the HTML Yes
Internet Explorer Driver Lower-cased As they appear in the HTML No
Firefox Driver Case insensitive As they appear in the HTML Yes
<input type="text" name="example" />
<INPUT type="text" name="other" />
inputs = driver.find_elements_by_xpath("//input")
or
from selenium.webdriver.common.by import By
inputs = driver.find_elements(By.XPATH, "//input")
Getting text values:
element = driver.find_element_by_id("element_id")
element.text
User Input - Filling In Forms
我们已经看到如何将文本输入到textarea或文本字段中,但其他元素呢?
您可以“切换”复选框的状态,并且可以使用“单击”来设置类似于所选OPTION标记的内容。
处理SELECT标签并不算太糟糕:
select = driver.find_element_by_tag_name("select")
allOptions = select.find_elements_by_tag_name("option")
for option in allOptions:
print "Value is: " + option.get_attribute("value")
option.click()
这将在页面上找到第一个“SELECT”元素,并依次循环选择每个选项,打印出它们的值并依次选择每个选项。
正如你会注意到的,这不是处理SELECT元素的最有效方式。
WebDriver的支持类包括一个名为“Select”的类,它提供了与这些类交互的有用方法。
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_tag_name("select"))
select.deselect_all()
select.select_by_visible_text("Edam")
这将从页面上的第一个SELECT中取消选择所有OPTION,然后选择显示文本“Edam”的OPTION。
完成表格填写后,您可能需要提交。一种方法是找到“提交”按钮并点击它:
driver.find_element_by_id("submit").click()
或者,WebDriver在每个元素上都有“提交”的便利方法。如果你在表单中的元素上调用它,WebDriver会遍历DOM直到找到包含表单,然后调用它。
如果元素不在表单中,NoSuchElementException则会抛出:
element.submit()
Moving Between Windows and Frames(在Windows和框架之间移动)
一些Web应用程序有许多框架或多个窗口。WebDriver支持使用“switchTo”方法在命名窗口之间移动:
driver.switch_to.window("windowName")
driver现在所有的呼叫将被解释为指向特定的窗口。但你怎么知道这个窗口的名字?看看打开它的javascript或链接:
<a href="somewhere.html" target="windowName">Click here to open a new window</a>
或者,您可以将“窗口句柄”传递给“switchTo()。window()”方法。知道这一点,可以遍历每个打开的窗口,如下所示:
or handle in driver.window_handles:
driver.switch_to.window(handle)
您也可以在一帧之间切换(或者切换到iframe):
driver.switch_to.frame("frameName")
Popup Dialogs(导航:历史和位置)
driver.get("http://www.example.com") # python doesn't have driver.navigate
重申:“ navigate().to()”和“ get()”完全一样。一个比另一个更容易打字!
“导航”界面还提供了在浏览器历史记录中前后移动的功能:
driver.forward()
driver.back()
请注意,此功能完全取决于底层浏览器。
如果您习惯了一种浏览器的行为而另一种浏览器的行为,那么当您调用这些方法时,可能会发生意想不到的情况。
Cookies
# Go to the correct domain
driver.get("http://www.example.com")
# Now set the cookie. Here's one for the entire domain
# the cookie name here is 'key' and its value is 'value'
driver.add_cookie({'name':'key', 'value':'value', 'path':'/'})
# additional keys that can be passed in are:
# 'domain' -> String,
# 'secure' -> Boolean,
# 'expiry' -> Milliseconds since the Epoch it should expire.
# And now output all the available cookies for the current URL
for cookie in driver.get_cookies():
print "%s -> %s" % (cookie['name'], cookie['value'])
# You can delete cookies in 2 ways
# By name
driver.delete_cookie("CookieName")
# Or all of them
driver.delete_all_cookies()
Changing the User Agent(更改用户代理)
使用Firefox驱动程序很容易:
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "some UA string")
driver = webdriver.Firefox(profile)
Drag And Drop(拖放)
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_name("source")
target = driver.find_element_by_name("target") ActionChains(driver).drag_and_drop(element, target).perform()
Firefox驱动程序
profile = webdriver.FirefoxProfile()
profile.native_events_enabled = True
driver = webdriver.Firefox(profile)
WebDriver:高级用法
显式和隐式等待
1、显式等待
一个显式等待是你定义的一段代码,用于等待某个条件发生然后再继续执行后续代码。显式等待是等元素加载!!!
2、隐式等待
相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。隐式等待是等页面加载,而不是元素加载!!!
(隐式等待就是针对页面的,显式等待是针对元素的。)
显式等待
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()
预期条件
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))
隐式等待
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") RemoteWebDriver
截图
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')
使用FirefoxProfile
from selenium import webdriver
fp = webdriver.FirefoxProfile()
# set something on the profile...
driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.FIREFOX, browser_profile=fp)
使用ChromeOptions
from selenium import webdriver
options = webdriver.ChromeOptions()
# set some options
driver = webdriver.Remote(desired_capabilities=options.to_capabilities())
WebDriver介绍的更多相关文章
- webdriver介绍&与Selenium RC的比较
什么是webdriver? webdriver是一个web自动化测试框架,不同于selenium IDE只能运行在firefox上,webdriver能够在不同的浏览器上执行你的web测试用例.其支持 ...
- 【译】Selenium 2.0 WebDriver
Selenium WebDriver 注意:我们正致力于完善帮助指南的每一个章节,虽然这个章节仍然存在需要完善的地方,不过我们坚信当前你看到的帮助信息是精确无误的,后续我们会提供更多的指导信息来完 ...
- 验收测试 - WebDriver 5
验收测试 - WebDriver - 配置 什么是WebDriver 这样说好了,它翻译起来就是Web驱动,用我的经验来说,它就是驱动浏览器运行的一个驱动器 有什么作用? 就像一个司机可以驱动一台汽车 ...
- 章节一、1-Selenium简介
一.Selenium WebDriver介绍 1.跨平台,用web浏览器做自动化的工具. 2.可以在浏览器上运行的一个框架,用来进行界面的自动化. 3.支持多种计算机语言. 4.可以模拟真实的用户去操 ...
- selenium入门基础知识
内容转载自:http://blog.csdn.net/huangbowen521/article/details/7816538 1.selenium介绍: Selenium是一个浏览器自动化操作框架 ...
- 基于Selenium2+Java的UI自动化(4) - WebDriver API简单介绍
1. 启动浏览器 前边有详细介绍启动三种浏览器的方式(IE.Chrome.Firefox): private WebDriver driver = null; private String chrom ...
- Selenium1(RC)与Selenium2(WebDriver)的概念介绍
最近网上学习了Selenium1和selenium2,自己做一些总结,方便以后查阅. 部分内容引用: http://www.cnblogs.com/hyddd/archive/2009/05/30/1 ...
- 转:Selenium2.0介绍——WebDriver两种驱动浏览器的方式.
如果之前熟悉Selenium RC,理解了Selenium RC是如何工作的,那么,当第一次接触Selenium WebDriver的时候,看到WebDriver居然可以不需要指定远端服务器的IP地址 ...
- Android WebDriver 浏览器自动测试工具介绍
Selenium WebDriver 是浏览器自动测试工具,提供轻量级和优雅的方式来测试web应用.Selenium WebDriver作为Android SDK extra,支持Android 2. ...
随机推荐
- unity协程要点
使用协程做计时功能应注意 1.协程中用到的组件,变量等被置空前,应该将协程置空 2.置空协程之前应停止协程 3.为了确保同一个协程同时只运行一次,可在协程开始前添加安全代码:判断改协程是否存在,存在则 ...
- IDEA实用教程(二)
2. 基础设置 1) 进入全局设置 2) 更改主题 3) 修改主题字体 4) 修改代码编辑区字体 5) 修改控制台字体 图中3处修改控制台字体 图中4处修改控制台字体 6) 文件编码的设置 图中4处建 ...
- 解决在jenkins中无法打开robot framework report.html log.html的问题
问题描述: Opening Robot Framework report failed Verify that you have JavaScript enabled in your browser. ...
- python 'NoneType' object has no attribute 'get'
获取 页面链接的时候报错 'NoneType' object has no attribute 'get' href = div.find("a").get("href& ...
- xld特征
halcon中什么是xld? xld(eXtended Line Descriptions) 扩展的线性描述,它不是基于像素的,人们称它是亚像素,只不过比像素更精确罢了,可以精确到像素内部的一种描述. ...
- comm shell command
1.awk command 1.1 Purpose 1: want to distinct and then count and sort by num 1.1.1 Command: cat resu ...
- 多路径技术:ALUA与SLUA
实现的核心 通过存储设备去适配操作系统,从而实现多路径技术,支持ALUA是其中主要部分. ALUA多路径技术 Asymmetric Logical Unit Access,非对称逻辑单元存取,其提 ...
- P4556 雨天的尾巴 线段树合并
使用线段树合并,每个节点维护一棵权值线段树,下标为救济粮种类,区间维护数量最多的救济粮编号(下标).所以每个节点答案即为\(tre[rot[x]]\). 然后运用树上点的差分思想,对于分发路径\(u, ...
- P2679 子串 DP
P2679 子串 DP 从字符串A中取出\(k\)段子串,按原顺序拼接,问存在多少个方案使拼接的字符串与字符串B相同 淦,又是这种字符串dp 设状态\(ans[i][j][k]\)表示A串位置\(i\ ...
- CSPS2019游(tuifei)记
%%%脸哥没脸%%% Day0,日常考前紧张,做不下题去.听各大主任送祝福(从里红(wa)到外) 然后就出发了,大巴上和云力一起坐,吃了好多东西.中午因不满火车站的不合理收费,选择了面包+火腿 下午在 ...