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. ...
随机推荐
- matlab(3) Logistic Regression: 求cost 和gradient \ 求sigmoid的值
sigmoid.m文件 function g = sigmoid(z)%SIGMOID Compute sigmoid functoon% J = SIGMOID(z) computes the si ...
- 7月新的开始 - Axure学习04 - 发布与预览、菜单和表格元件、流程图和连接点、标记元件
Axure 的发布与预览 1.发布 2.生成html文件 常规:指定浏览器.工具栏的生成 页面.页面说明.元件说明.交互.标志(logo和描述).字体.移动设备等 3.发布到Axshare Axure ...
- x006-函数和模块的使用
来源:百度SEO公司 函数和模块的使用 在Python中可以使用def关键字来定义函数,和变量一样每个函数也有一个响亮的名字,而且命名规则跟变量的命名规则是一致的.在函数名后面的圆括号中可以放置传递给 ...
- ubuntu下卸载旧Mysql并安装新Mysql(升级)
由于从apt-get下安装的Mysql不是最新版的,所以,需要升级.先卸载,再安装. 1.卸载 先看mysql是否在运行: netstat -tap | grep mysql 然后 sudo apt- ...
- 2019牛客多校B Beauty Values——思维题
题目 求所有子区间中不同元素之和. 分析 枚举相邻的相同数字形成的区间,计算它是哪些区间的子区间,每次有-1的贡献,将其从总贡献中减去. #include<bits/stdc++.h> u ...
- location对象方法(assign()、reload()、replace())
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MyBatis底层实现原理: 动态代理的运用
转载:https://mp.weixin.qq.com/s/_6nyhaWX15mh3mkj8Lb0Zw Mybatis中声明一个interface接口,没有编写任何实现类,Mybatis就能返回接口 ...
- Qt进程间通信
Qt 提供了四种进程间通信的方式: 使用共享内存(shared memory)交互:这是 Qt 提供的一种各个平台均有支持的进程间交互的方式. TCP/IP:其基本思想就是将同一机器上面的两个进程一个 ...
- luogu 3248
直接向原树加子树是不可能的考虑重新建立这样一颗树,我们称之为 S 树 将每次需要添加的子树看做一个点,称之为 S 点 新建的树就是由这些点构成的,那么树的大小是合理的 初始节点为整棵原树由于添加的子树 ...
- 爬虫(十六):scrapy爬取知乎用户信息
一:爬取思路 首先我们应该找到一个账号,这个账号被关注的人和关注的人都相对比较多的,就是下图中金字塔顶端的人,然后通过爬取这个账号的信息后,再爬取他关注的人和被关注的人的账号信息,然后爬取被关注人的账 ...