﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-qileilove-文章分类-python</title><link>http://www.blogjava.net/qileilove/category/54131.html</link><description>不想做屌丝的码农，不是好项目经理！屌丝生涯从此开始！</description><language>zh-cn</language><lastBuildDate>Mon, 21 Apr 2014 12:10:09 GMT</lastBuildDate><pubDate>Mon, 21 Apr 2014 12:10:09 GMT</pubDate><ttl>60</ttl><item><title>selenium + python 鼠标事件</title><link>http://www.blogjava.net/qileilove/articles/412756.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Mon, 21 Apr 2014 11:17:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/412756.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/412756.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/412756.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/412756.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/412756.html</trackback:ping><description><![CDATA[<div>十、鼠标事件</div><div>本章重点：</div><div>ActionChains 类</div><div></div><div>context_click()</div><div>右击</div><div></div><div>double_click()</div><div>双击</div><div></div><div>drag_and_drop()</div><div>拖动</div><div>测试的产品中有一个操作是右键点击文件列表会弹出一个快捷菜单，可以方</div><div>便的选择快捷菜单中的选择对文件进行操作（删除、移动、重命名），之前学习</div><div>元素的点击非常简单：</div><div>driver.find_element_by_id(&#8220;xxx&#8221;).click()</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>26</div><div>那么鼠标的双击、右击、拖动等是否也是这样的写法呢？例如右击：</div><div>driver.find_element_by_id(&#8220;xxx&#8221;).context_click()</div><div>经过运行脚本得到了下面的错误提示：</div><div>AttributeError: 'WebElement' object has no attribute 'context_click'</div><div>提示右点方法不属于 webelement 对象，通过查找文档，发现属于</div><div>ActionChains 类，但文档中没有具体写法。这里要感谢 北京-QC-rabbit 的指</div><div>点，其实整个 python+selenium 学习过程都要感谢 北京-QC-rabbit 的指点。</div><div>10.1、鼠标右键</div><div>下面介绍鼠标右键的用法，以快播私有云为例：</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>from selenium.webdriver.common.action_chains import ActionChains</div><div>import time</div><div>driver = webdriver.Firefox()</div><div>driver.get("http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud</div><div>.kuaibo.com%2F")</div><div>#登陆快播私有云</div><div>driver.find_element_by_id("user_name").send_keys("username")</div><div>driver.find_element_by_id("user_pwd").send_keys("123456")</div><div>driver.find_element_by_id("dl_an_submit").click()</div><div>time.sleep(3)</div><div>#定位到要右击的元素</div><div>qqq</div><div>=driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table</div><div>/tbody/tr/td[2]")</div><div>#对定位到的元素执行鼠标右键操作</div><div>ActionChains(driver).context_click(qqq).perform()</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>27</div><div>'''</div><div>#你也可以使用三行的写法，但我觉得上面两行写法更容易理解</div><div>chain = ActionChains(driver)</div><div>implement =</div><div>driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/</div><div>tbody/tr/td[2]")</div><div>chain.context_click(implement).perform()</div><div>'''</div><div>time.sleep(3) #休眠3秒</div><div>driver.close()</div><div>这里需要注意的是，在使用 ActionChains 类之前，要先将包引入。</div><div>右击的操作会了，下面的其它方法比葫芦画瓢也能写出来。</div><div>10.2、鼠标双击</div><div>鼠标双击的写法：</div><div>#定位到要双击的元素</div><div>qqq =driver.find_element_by_xpath("xxx")</div><div>#对定位到的元素执行鼠标双击操作</div><div>ActionChains(driver).double_click(qqq).perform()</div><div>10.3、鼠标拖放</div><div>鼠标拖放操作的写法：</div><div>#定位元素的原位置</div><div>element = driver.find_element_by_name("source")</div><div>#定位元素要移动到的目标位置</div><div>target =</div><div>driver.find_element_by_name("target")</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>28</div><div>#执行元素的移动操作</div><div>ActionChains(driver).drag_and_drop(element, target).perform()</div><img src ="http://www.blogjava.net/qileilove/aggbug/412756.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-04-21 19:17 <a href="http://www.blogjava.net/qileilove/articles/412756.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>selenium + python 键盘事件</title><link>http://www.blogjava.net/qileilove/articles/412755.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Mon, 21 Apr 2014 11:14:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/412755.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/412755.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/412755.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/412755.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/412755.html</trackback:ping><description><![CDATA[<div>九、键盘事件</div><div>本章重点：</div><div></div><div>键盘按键用法</div><div></div><div>键盘组合键用法</div><div></div><div>send_keys() 输入中文乱码问题</div><div>9.1、键盘按键用法</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>from selenium.webdriver.common.keys import Keys</div><div>#需要引入 keys 包</div><div>import os,time</div><div>driver = webdriver.Firefox()</div><div>driver.get("http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud</div><div>.kuaibo.com%2F")</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>24</div><div>time.sleep(3)</div><div>driver.maximize_window() # 浏览器全屏显示</div><div>driver.find_element_by_id("user_name").clear()</div><div>driver.find_element_by_id("user_name").send_keys("fnngj")</div><div>#tab 的定位相相于清除了密码框的默认提示信息，等同上面的 clear()</div><div>driver.find_element_by_id("user_name").send_keys(Keys.TAB)</div><div>time.sleep(3)</div><div>driver.find_element_by_id("user_pwd").send_keys("123456")</div><div>#通过定位密码框，enter（回车）来代替登陆按钮</div><div>driver.find_element_by_id("user_pwd").send_keys(Keys.ENTER)</div><div>'''</div><div>#也可定位登陆按钮，通过 enter（回车）代替 click()</div><div>driver.find_element_by_id("login").send_keys(Keys.ENTER)</div><div>'''</div><div>time.sleep(3)</div><div>driver.quit()</div><div>要想调用键盘按键操作需要引入 keys 包：</div><div>from selenium.webdriver.common.keys import Keys</div><div>通过 send_keys()调用按键：</div><div>send_keys(Keys.TAB)</div><div># TAB</div><div>send_keys(Keys.ENTER)</div><div># 回车</div><div>注意：这个操作和页面元素的遍历顺序有关，假如当前定位在账号输入框，按键</div><div>盘的 tab 键后遍历的不是密码框，那就不法输入密码。 假如输入密码后，还有</div><div>需要填写验证码，那么回车也起不到登陆的效果。</div><div>9.2、键盘组合键用法</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>from selenium.webdriver.common.keys import Keys</div><div>import time</div><div>driver = webdriver.Firefox()</div><div>driver.get("http://www.baidu.com")</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>25</div><div>#输入框输入内容</div><div>driver.find_element_by_id("kw").send_keys("selenium")</div><div>time.sleep(3)</div><div>#ctrl+a 全选输入框内容</div><div>driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')</div><div>time.sleep(3)</div><div>#ctrl+x 剪切输入框内容</div><div>driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')</div><div>time.sleep(3)</div><div>#输入框重新输入内容，搜索</div><div>driver.find_element_by_id("kw").send_keys(u"虫师 cnblogs")</div><div>driver.find_element_by_id("su").click()</div><div>time.sleep(3)</div><div>driver.quit()</div><div>上面的操作没有实际意义，但向我们演示了键盘组合按键的用法。</div><div>9.3、中文乱码问题</div><div>selenium2 python 在 send_keys()中输入中文一直报错，其实前面加个小 u 就解决了：</div><div>coding=utf-8</div><div>send_keys(u"输入中文")</div><div>需要注意的是 utf-8并不是万能的，我们需要保持脚本、浏览器、程序三者编码之</div><div>间的转换；如果 utf-8不能解决，可以尝试 GBK 或修改浏览器的默认编码。</div><img src ="http://www.blogjava.net/qileilove/aggbug/412755.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-04-21 19:14 <a href="http://www.blogjava.net/qileilove/articles/412755.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>八、操作测试对象</title><link>http://www.blogjava.net/qileilove/articles/412754.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Mon, 21 Apr 2014 11:11:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/412754.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/412754.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/412754.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/412754.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/412754.html</trackback:ping><description><![CDATA[<div>八、操作测试对象</div><div>前面讲到了不少知识都是定位元素，定位只是第一步，定位之后需要对这个原素进</div><div>行操作。鼠标点击呢还是键盘输入，这要取决于我们定位的是按钮还输入框。</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>21</div><div>一般来说，webdriver 中比较常用的操作对象的方法有下面几个</div><div></div><div>click</div><div>点击对象</div><div></div><div>send_keys</div><div>在对象上模拟按键输入</div><div></div><div>clear</div><div>清除对象的内容，如果可以的话</div><div></div><div>submit</div><div>清除对象的内容，如果可以的话</div><div></div><div>text</div><div>用于获取元素的文本信息</div><div>8.1、鼠标点击与键盘输入</div><div>在我们本系列开篇的第一个例子里就用到了到 click 和 send_skys ，别翻回去找</div><div>了，我再贴一下代码：</div><div>coding=utf-8</div><div>from selenium import webdriver</div><div>import time</div><div>driver = webdriver.Firefox()</div><div>driver.get("http://www.baidu.com")</div><div>driver.find_element_by_id("kw").clear()</div><div>driver.find_element_by_id("kw").send_keys("selenium")</div><div>time.sleep(2)</div><div>#通过 submit() 来操作</div><div>driver.find_element_by_id("su").submit()</div><div>time.sleep(3)</div><div>driver.quit()</div><div>send_keys("xx")</div><div>用于在一个输入框里输入 xx 内容。</div><div>click()</div><div>用于点击一个按钮。</div><div>clear()</div><div>用于清除输入框的内容，比如百度输入框里默认有个&#8220;请输入关键</div><div>字&#8221;的信息，再比如我们的登陆框一般默认会有&#8220;账号&#8221;&#8220;密码&#8221;这样的默认信息。</div><div>clear 可以帮助我们清除这些信息。</div><div>8.2、submit 提交表单</div><div>我们把&#8220;百度一下&#8221;的操作从 click 换成 submit 可以达到相同的效果：</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>import time</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>22</div><div>driver = webdriver.Firefox()</div><div>driver.get("http://www.baidu.com")</div><div>driver.find_element_by_id("kw").send_keys("selenium")</div><div>time.sleep(2)</div><div>#通过 submit() 来操作</div><div>driver.find_element_by_id("su").submit()</div><div>time.sleep(3)</div><div>driver.quit()</div><div>8.3、text 获取元素文本</div><div>text 用于获取元素的文本信息</div><div>下面把百度首页底部的声明打印输出</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>import</div><div>time</div><div>driver = webdriver.Firefox()</div><div>driver.get("http://www.baidu.com")</div><div>time.sleep(2)</div><div>#id = cp 元素的文本信息</div><div>data=driver.find_element_by_id("cp").text</div><div>print data</div><div>#打印信息</div><div>time.sleep(3)</div><div>driver.quit()</div><div>输出：</div><div>&gt;&gt;&gt;</div><div>&#169;2013 Baidu 使用百度前必读 京 ICP 证030173号</div><div>8.4、get_attribute 获得属性值</div><div>get_attribute</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>23</div><div>获得属性值。</div><div>这个函数的用法前面已经有出现过，在定位一组元素的时候有使用到它，只是我们</div><div>没有做过多的解释。</div><div>一般用法：</div><div>select = driver.find_element_by_tag_name("select")</div><div>allOptions = select.find_elements_by_tag_name("option")</div><div>for option in allOptions:</div><div>print "Value is: " + option.get_attribute("value")</div><div>option.click()</div><div>.....</div><div>具体应用参考第十一节层级定位例子。</div><img src ="http://www.blogjava.net/qileilove/aggbug/412754.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-04-21 19:11 <a href="http://www.blogjava.net/qileilove/articles/412754.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>selenium + python 浏览器的操作</title><link>http://www.blogjava.net/qileilove/articles/412611.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Thu, 17 Apr 2014 07:38:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/412611.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/412611.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/412611.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/412611.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/412611.html</trackback:ping><description><![CDATA[<div>六、浏览器的操作</div><div>6.1、浏览器最大化</div><div>我们知道调用启动的浏览器不是全屏的，这样不会影响脚本的执行，但是有时候会</div><div>影响我们&#8220;观看&#8221;脚本的执行。</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>import time</div><div>browser = webdriver.Firefox()</div><div>browser.get("http://www.baidu.com")</div><div>print "浏览器最大化"</div><div>browser.maximize_window()</div><div>#将浏览器最大化显示</div><div>time.sleep(2)</div><div>browser.find_element_by_id("kw").send_keys("selenium")</div><div>browser.find_element_by_id("su").click()</div><div>time.sleep(3)</div><div>browser.quit()</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>19</div><div>6.2、设置浏览器宽、高</div><div>最大化还是不够灵活，能不能随意的设置浏览的宽、高显示？当然是可以的。</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>import time</div><div>browser = webdriver.Firefox()</div><div>browser.get("http://m.mail.10086.cn")</div><div>time.sleep(2)</div><div>#参数数字为像素点</div><div>print "设置浏览器宽480、高800显示"</div><div>browser.set_window_size(480, 800)</div><div>time.sleep(3)</div><div>browser.quit()</div><img src ="http://www.blogjava.net/qileilove/aggbug/412611.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-04-17 15:38 <a href="http://www.blogjava.net/qileilove/articles/412611.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>selenium + python 打印信息</title><link>http://www.blogjava.net/qileilove/articles/412509.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Wed, 16 Apr 2014 03:33:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/412509.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/412509.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/412509.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/412509.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/412509.html</trackback:ping><description><![CDATA[<div>五、打印信息</div><div>很多时间我们不可能盯着脚本执行，我们需要一些打印信息来证明脚本运行是否正确：</div><div>5.1、打印 tile</div><div>把刚才访问页面的 title 打印出来。</div><div>coding = utf-8</div><div>from selenium import webdriver</div><div>driver = webdriver.Chrome()</div><div>driver.get('http://www.baidu.com')</div><div>print driver.title</div><div># 把页面 title 打印出来</div><div>driver.quit()</div><div>虽然我没看到脚本的执行过程，但我在执行结果里看到了</div><div>&gt;&gt;&gt;</div><div>百度一下，你就知道</div><div>说明页面正确被我打开了。</div><div>5.2、打印 URL</div><div>可以将浏览器的 title 打印出来，这里再讲个简单的，把当前 URL 打印出来。其实</div><div>也没啥大用，可以做个凑数的用例。</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>import time</div><div>browser = webdriver.Firefox()</div><div>url= 'http://www.baidu.com'</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>18</div><div>#通过 get 方法获取当前 URL 打印</div><div>print "now access %s" %(url)</div><div>browser.get(url)</div><div>time.sleep(2)</div><div>browser.find_element_by_id("kw").send_keys("selenium")</div><div>browser.find_element_by_id("su").click()</div><div>time.sleep(3)</div><div>browser.quit()</div><div>六、浏览器的操作</div><div>6.1、浏览器最大化</div><div>我们知道调用启动的浏览器不是全屏的，这样不会影响脚本的执行，但是有时候会</div><div>影响我们&#8220;观看&#8221;脚本的执行。</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>import time</div><div>browser = webdriver.Firefox()</div><div>browser.get("http://www.baidu.com")</div><div>print "浏览器最大化"</div><div>browser.maximize_window()</div><div>#将浏览器最大化显示</div><div>time.sleep(2)</div><div>browser.find_element_by_id("kw").send_keys("selenium")</div><div>browser.find_element_by_id("su").click()</div><div>time.sleep(3)</div><div>browser.quit()</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>19</div><div>6.2、设置浏览器宽、高</div><div>最大化还是不够灵活，能不能随意的设置浏览的宽、高显示？当然是可以的。</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>import time</div><div>browser = webdriver.Firefox()</div><div>browser.get("http://m.mail.10086.cn")</div><div>time.sleep(2)</div><div>#参数数字为像素点</div><div>print "设置浏览器宽480、高800显示"</div><div>browser.set_window_size(480, 800)</div><div>time.sleep(3)</div><div>browser.quit()</div><img src ="http://www.blogjava.net/qileilove/aggbug/412509.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-04-16 11:33 <a href="http://www.blogjava.net/qileilove/articles/412509.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>selenium + python 添加等待时间</title><link>http://www.blogjava.net/qileilove/articles/412450.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Tue, 15 Apr 2014 03:28:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/412450.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/412450.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/412450.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/412450.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/412450.html</trackback:ping><description><![CDATA[<div>四、添加等待时间</div><div>有时候为了保证脚本运行的稳定性，需要脚本中添加等待时间。</div><div>4.1、添加休眠</div><div>添加休眠非常简单，我们需要引入 time 包，就可以在脚本中自由的添加休眠时间了。</div><div># coding = utf-8</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>16</div><div>from selenium import webdriver</div><div>import</div><div>time</div><div>#调入 time 函数</div><div>browser = webdriver.Firefox()</div><div>browser.get("http://www.baidu.com")</div><div>time.sleep(0.3)</div><div>#休眠0.3秒</div><div>browser.find_element_by_id("kw").send_keys("selenium")</div><div>browser.find_element_by_id("su").click()</div><div>time.sleep(3)</div><div># 休眠3秒</div><div>browser.quit()</div><div>4.2、智能等待</div><div>通过添加 implicitly_wait() 方法就可以方便的实现智能等待；implicitly_wait(30)</div><div>的用法应该比 time.sleep() 更智能，后者只能选择一个固定的时间的等待，前者可以</div><div>在一个时间范围内智能的等待。</div><div>文档解释：</div><div>selenium.webdriver.remote.webdriver.implicitly_wait(time_to_wait)</div><div>隐式地等待一个无素被发现或一个命令完成；这个方法每次会话只需要调用一次</div><div>time_to_wait: 等待时间</div><div>用法:</div><div>browser.implicitly_wait(30)</div><div># coding = utf-8</div><div>from selenium import webdriver</div><div>import</div><div>time</div><div>#调入 time 函数</div><div>browser = webdriver.Firefox()</div><div>browser.get("http://www.baidu.com")</div><div>browser.implicitly_wait(30)</div><div>#智能等待30秒</div><div>browser.find_element_by_id("kw").send_keys("selenium")</div><div>browser.find_element_by_id("su").click()</div><div>browser.quit()</div><img src ="http://www.blogjava.net/qileilove/aggbug/412450.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-04-15 11:28 <a href="http://www.blogjava.net/qileilove/articles/412450.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python-webdriver 开始第一个脚本</title><link>http://www.blogjava.net/qileilove/articles/412233.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Thu, 10 Apr 2014 07:16:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/412233.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/412233.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/412233.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/412233.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/412233.html</trackback:ping><description><![CDATA[<div>2.1 为什么选 python</div><div>之前的菜鸟系列是基于 java 的，一年没学其实也忘的差不多了，目前所测的产品</div><div>部分也是 python 写的，而且团队也在推广 python ，其实就测试人员来说，python 也</div><div>相当受欢迎。易学，易用。翻翻各测试招聘，python 出现的概率也颇高。(个人原因)</div><div>最重要的还是 python 简单易学，应用也相对广泛；是测试人员学习编程的不二之选。</div><div>下面看看 python 穿上 selenium webdriver 是多么的性感：</div><div>2.2 第一个脚本</div><div># coding = utf-8</div><div>from selenium import webdriver</div><div>browser = webdriver.Firefox()</div><div>browser.get("http://www.baidu.com")</div><div>browser.find_element_by_id("kw").send_keys("selenium")</div><div>browser.find_element_by_id("su").click()</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>10</div><div>browser.quit()</div><div>2.3 脚本解析</div><div># coding = utf-8</div><div>可加可不加，开发人员喜欢加一下，防止乱码嘛。</div><div>from selenium import webdriver</div><div>要想使用 selenium 的 webdriver 里的函数，首先把包导进来嘛</div><div>browser = webdriver.Firefox()</div><div>我们需要操控哪个浏览器呢？Firefox ，当然也可以换成 Ie 或 Chrome 。browser 可以</div><div>随便取，但后面要用它操纵各种函数执行。</div><div>browser.find_element_by_id("kw").send_keys("selenium")</div><div>一个控件有若干属性 id 、name、（也可以用其它方式定位），百度输入框的 id 叫 kw ，</div><div>我要在输入框里输入 selenium 。多自然语言呀！</div><div>browser.find_element_by_id("su").click()</div><div>搜索的按钮的 id 叫 su ，我需要点一下按钮（ click() ）。</div><div>browser.quit()</div><div>退出并关闭窗口的每一个相关的驱动程序，有洁癖用这个。</div><div>browser.close()</div><div>关闭当前窗口 ，用哪个看你的需求了。</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>11</div><div>三、元素的定位</div><div>对象的定位应该是自动化测试的核心，要想操作一个对象，首先应该识别这个对象。</div><div>一个对象就是一个人一样，他会有各种的特征（属性），如比我们可以通过一个人的身</div><div>份证号，姓名，或者他住在哪个街道、楼层、门牌找到这个人。</div><div>那么一个对象也有类似的属性，我们可以通过这个属性找到这对象。</div><div>webdriver 提供了一系列的对象定位方法，常用的有以下几种</div><div></div><div>&#183; id</div><div></div><div>&#183; name</div><div></div><div>&#183; class name</div><div></div><div>&#183; link text</div><div></div><div>&#183; partial link text</div><div></div><div>&#183; tag name</div><div></div><div>&#183; xpath</div><div></div><div>&#183; css selector</div><div>我们可以看到，一个百度的输入框，可以用这么用种方式去定位。</div><div>&lt;input</div><div>id="kw"</div><div>class="s_ipt"</div><div>type="text"</div><div>maxlength="100"</div><div>name="wd"</div><div>autocomplete="off"&gt;</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>browser = webdriver.Firefox()</div><div>browser.get("http://www.baidu.com")</div><div>#########百度输入框的定位方式##########</div><div>#通过 id 方式定位</div><div>browser.find_element_by_id("kw").send_keys("selenium")</div><div>#通过 name 方式定位</div><div>browser.find_element_by_name("wd").send_keys("selenium")</div><div>#通过 tag name 方式定位</div><div>browser.find_element_by_tag_name("input").send_keys("selenium")</div><div>#通过 class name 方式定位</div><div>browser.find_element_by_class_name("s_ipt").send_keys("selenium")</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>12</div><div>#通过 CSS 方式定位</div><div>browser.find_element_by_css_selector("#kw").send_keys("selenium")</div><div>#通过 xphan 方式定位</div><div>browser.find_element_by_xpath("//input[@id='kw']").send_keys("selenium")</div><div>############################################</div><div>browser.find_element_by_id("su").click()</div><div>time.sleep(3)</div><div>browser.quit()</div><div>3.1</div><div>id 和 name 定位</div><div>id 和 name 是我们最最常用的定位方式，因为大多数控件都有这两个属性，而且</div><div>在对控件的 id 和 name 命名时一般使其有意义也会取不同的名字。通过这两个属性使我</div><div>们找一个页面上的属性变得相当容易</div><div>我们通过前端工具，找到了百度输入框的属性信息，如下：</div><div>&lt;input</div><div>id="kw"</div><div>class="s_ipt"</div><div>type="text"</div><div>maxlength="100"</div><div>name="wd"</div><div>autocomplete="off"&gt;</div><div>id=&#8221;kw&#8221;</div><div>通过 find_element_by_id("kw") 函数就是捕获到百度输入框</div><div>name=&#8221;wd&#8221;</div><div>通过 find_element_by_name("wd")函数同样也可以捕获百度输入框</div><div>3.2</div><div>tag name 和 class name 定位</div><div>从上面的百度输入框的属性信息中，我们看到，不单单只有 id 和 name 两个属性，</div><div>比如 class 和 tag name(标签名）</div><div>&lt;input</div><div>id="kw"</div><div>class="s_ipt"</div><div>type="text"</div><div>maxlength="100"</div><div>name="wd"</div><div>autocomplete="off"&gt;</div><div>&lt;input&gt;</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>13</div><div>input 就是一个标签的名字<br /><br /><div>3.4</div><div>XPath 定位</div><div>什么是 XPath：http://www.w3.org/TR/xpath/</div><div>XPath 基础教程：http://www.w3schools.com/xpath/default.asp</div><div>selenium 中被误解的 XPath ： http://magustest.com/blog/category/webdriver/</div><div>XPath 是一种在 XML 文档中定位元素的语言。因为 HTML 可以看做 XML 的一种实现，</div><div>所以 selenium 用户可是使用这种强大语言在 web 应用中定位元素。</div><div>XPath 扩展了上面 id 和 name 定位方式，提供了很多种可能性，比如定位页面上的</div><div>第三个多选框。</div><div>xpath:attributer （属性）</div><div>driver.find_element_by_xpath("//input[@id='kw']").send_keys("selenium")</div><div>#input 标签下 id =kw 的元素</div><div>xpath:idRelative （id 相关性）</div><div>driver.find_element_by_xpath("//div[@id='fm']/form/span/input").send_keys("s</div><div>elenium")</div><div>#在/form/span/input 层级标签下有个 div 标签的 id=fm 的元素</div><div>driver.find_element_by_xpath("//tr[@id='check']/td[2]").click()</div><div># id 为'check' 的 tr ，定位它里面的第2个 td</div><div>xpath:position （位置）</div><div>driver.find_element_by_xpath("//input").send_keys("selenium")</div><div>driver.find_element_by_xpath("//tr[7]/td[2]").click()</div><div>#第7个 tr 里面的第2个 td</div><div>xpath: href （水平参考）</div><div>driver.find_element_by_xpath("//a[contains(text(),'网页')]").click()</div><div>#在 a 标签下有个文本（text）包含（contains）'网页' 的元素</div><div>xpath:link</div><div>driver.find_element_by_xpath("//a[@href='http://www.baidu.com/']").click()</div><div>#有个叫 a 的标签，他有个链接 href='http://www.baidu.com/ 的元素</div><div>博客园&#8212;虫师</div><div>http://fnng.cnblogs.com</div><div>15</div><div>3.5</div><div>link 定位</div><div>有时候不是一个输入框也不是一个按钮，而是一个文字链接，我们可以通过 link</div><div>#coding=utf-8</div><div>from selenium import webdriver</div><div>browser = webdriver.Firefox()</div><div>browser.get("http://www.baidu.com")</div><div>browser.find_element_by_link_text("贴 吧").click()</div><div>browser.quit()</div><div>一般一个页面上不会出现相同的文件链接，通过文字链接来定位也是一种简单有效</div><div>的定位方式。</div><div>3.6</div><div>Partial link text 定位</div><div>通过部分链接定位，这个有时候也会用到，我还没有想到很好的用处。拿上面的例</div><div>子，我可以只用链接的一部分文字进行匹配：</div><div>browser.find_element_by_partial_link_text("贴").click()</div><div>#通过 find_element_by_partial_link_text() 函数，我只用了&#8220;贴&#8221;字，脚本一样找到了"贴 吧</div><div>" 的链接</div></div><img src ="http://www.blogjava.net/qileilove/aggbug/412233.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-04-10 15:16 <a href="http://www.blogjava.net/qileilove/articles/412233.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>selenium+python 环境搭建</title><link>http://www.blogjava.net/qileilove/articles/412136.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Wed, 09 Apr 2014 03:06:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/412136.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/412136.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/412136.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/412136.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/412136.html</trackback:ping><description><![CDATA[<div>一、selenium+python 环境搭建</div><div>1.1 selenium 介绍</div><div>selenium 是一个 web 的自动化测试工具，不少学习功能自动化的同学开始首选 selenium ，</div><div>相因为它相比 QTP 有诸多有点：</div><div>*</div><div>免费，也不用再为破解 QTP 而大伤脑筋</div><div>*</div><div>小巧，对于不同的语言它只是一个包而已，而 QTP 需要下载安装1个多 G 的程序。</div><div>*</div><div>这也是最重要的一点，不管你以前更熟悉 C、 java、ruby、python、或都是 C# ，你都</div><div>可以通过 selenium 完成自动化测试，而 QTP 只支持 VBS</div><div>*</div><div>支持多平台：windows、linux、MAC ，支持多浏览器：ie、ff、safari、opera、chrome</div><div>*</div><div>支持分布式测试用例的执行，可以把测试用例分布到不同的测试机器的执行，相当于分</div><div>发机的功能。<br /><br /><div>1.2 准备工作</div><div>搭建平台 windows</div><div>准备工具如下：</div><div>-------------------------------------------------------------</div><div>下载 python</div><div>http://python.org/getit/</div><div>下载 setuptools 【python 的基础包工具】</div><div>http://pypi.python.org/pypi/setuptools</div><div>下载 pip 【python 的安装包管理工具】</div><div>https://pypi.python.org/pypi/pip</div><div>-------------------------------------------------------------</div><div>因为版本都在更新，pyhton 选择2.7.xx ，setuptools 选择你平台对应的版本，pip 不要</div><div>担心 tar.gz 在 windows 下一样可用。<br /><br /><div>1.3 安装步骤</div><div>一、python 的安装 ，这个不解释，exe 文件运行安装即可，既然你选择 python，相信</div><div>你是熟悉 python 的，我安装目录 C:\Python27</div><div>二、setuptools 的安装也非常简单，同样是 exe 文件，默认会找到 python 的安装路径，</div><div>将安装到 C:\Python27\Lib\site-packages 目录下</div><div>三、安装 pip ，我默认解压在了 C:\pip-1.3.1 目录下</div><div>四、打开命令提示符（开始---cmd 回车）进入 C:\pip-1.3.1目录下输入：</div><div>C:\pip-1.3.1</div><div>&gt; python setup.py install</div><div>（如果提示 python 不是内部或外部命令！别急，去配置一下环境变量吧）</div><div>修改我的电脑-&gt;属性-&gt;高级-&gt;环境变量-&gt;系统变量中的 PATH 为:</div><div>变量名：PATH</div><div>变量值：;C:\Python27</div><div>五、再切换到 C:\Python27\Scripts 目录下输入：</div><div>C:\Python27\Scripts &gt; easy_install pip</div><div>六、安装 selenium，（下载地址： https://pypi.python.org/pypi/selenium</div><div>）</div><div>如果是联网状态的话，可以直接在 C:\Python27\Scripts</div><div>下输入命令安装：</div><div>C:\Python27\Scripts &gt; pip install -U selenium</div><div>如果没联网（这个一般不太可能），下载 selenium 2.33.0 （目前的最新版本）</div><div>并解压把整个目录放到 C:\Python27\Lib\site-packages 目录下。</div><div>注意：七、八两步可以暂不进行，如果你要学习第二十一章 webdriver 原理的时候再进行</div><div>也不迟。</div><div>==============<br /><br /><div>七、下载并安装</div><div>（http://www.java.com/zh_CN/download/chrome.jsp?locale=zh_CN），什么！？你没整过</div><div>java 虚拟机，百度一下 java 环境搭建吧。</div><div>八、下载 selenium 的服务端（https://code.google.com/p/selenium/）在页面的左</div><div>侧列表中找到</div><div>selenium-server-standalone-XXX.jar</div><div>对！就是这个东西，把它下载下来并解压；</div><div>在</div><div>selenium-server-standalone-xxx.jar</div><div>目 录 下 使 用 命 令</div><div>java</div><div>-jar</div><div>selenium-server-standalone-xxx.jar 启动（如果打不开，查看是否端口被占 用：netstat</div><div>-aon|findstr 4444）。</div><div>==============<br /><br /><div>1.4 安装 chrome driver</div><div>chrome driver 的下载地址在这里。</div><div>1. 下载解压，你会得到一个 chromedriver.exe 文件（我点开，运行提示 started no prot</div><div>9515 ，这是干嘛的？端口9515被占了？中间折腾了半天），后来才知道需要把这家伙放到</div><div>chrome 的安装目录下...\Google\Chrome\Application\ ,然后设置 path 环境变量，把</div><div>chrome 的安装目录（我的：C:\Program Files\Google\Chrome\Application），然后再调用</div><div>运行：</div><div># coding = utf-8</div><div>from selenium import webdriver</div><div>driver =webdriver.Chrome()</div><div>driver.get('http://radar.kuaibo.com')</div><div>print driver.title</div><div>driver.quit()</div><div>报错提示：</div><div>Chrome</div><div>version</div><div>must</div><div>be</div><div>&gt;=</div><div>27.0.1453.0\n</div><div>(Driver</div><div>info:</div><div>chromedriver=2.0,platform=Windows NT 5.1 SP3 x86)</div><div>说我 chrome 的版本没有大于27.0.1453.0 ，这个好办，更新到最新版本即可。<br /><br /><div>1.5 安装IE driver</div><div>在新版本的 webdriver 中，只有安装了 ie driver 使用 ie 进行测试工作。</div><div>ie driver 的下载地址在这里，记得根据自己机器的操作系统版本来下载相应的 driver。</div><div>暂时还没尝试，应该和 chrome 的安装方式类似。</div><div>记得配置 IE 的保护模式</div><div>如果要使用 webdriver 启动 IE 的话，那么就需要配置 IE 的保护模式了。</div><div>把 IE 里的保护模式都选上或都勾掉就可以了。</div></div></div></div></div></div><img src ="http://www.blogjava.net/qileilove/aggbug/412136.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-04-09 11:06 <a href="http://www.blogjava.net/qileilove/articles/412136.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>自动化测试如何解决验证码的问题</title><link>http://www.blogjava.net/qileilove/articles/412047.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Tue, 08 Apr 2014 03:13:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/412047.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/412047.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/412047.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/412047.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/412047.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">下面摘录别人的 当然有好的一面 有差的一面 &nbsp;好的一面 我们认识了 解决验证码最基础的几种方法 其实不叫解决 &nbsp;只能算是避过验证码 真正的验证码识别 需要用的很多技术 &nbsp;图像识别 源码分析，网络数据流等等 起码我办不到 做自动化的话 还要什么验证码 我觉得如果你验证码只是为了防止防止用户误操作 或者 防止某些刷子 减少服务器的压力 而设置的 &nbsp;如果真的是为了自动化测试 我们完全可以屏蔽改功能 要他干嘛 。<br /><br />注意：本标题的&#8220;自动化测试&#8221; 包括性能测试 与UI级的自动化测试</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　经常会被问到如何解决验证码的问题，在此记录一下我所知道的几种方式。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　对于<span style="font-family: 'Times New Roman';">web</span><span style="font-family: 宋体;">应用来说，大部分的系统在用户登录时都要求用户输入验证码，验证码的类型的很多，有字母数字的，有汉字的，甚至还要用户输入一条算术题的答案的，对于系统来说使用验证码可以有效果的防止采用机器猜测方法对口令的刺探，在一定程度上增加了安全性。但对于测试人员来说，不管是进行性能测试还是自动化测试都是一个棘手的问题。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-family: 宋体;"><img src="http://images.cnitblog.com/i/311516/201403/180906223343844.png" alt="" style="border: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">下面来谈一下处理验证码的几种方法。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>去掉验证码</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　这是最简单的方法，对于开发人员来说，只是把验证码的相关代码注释掉即可，如果是在测试环境，这样做可省去了测试人员不少麻烦，如果自动化脚本是要在正式环境跑，这样就给系统带来了一定的风险。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>设置万能码</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　去掉验证码的主要是安全问题，为了应对在线系统的安全性威胁，可以在修改程序时不取消验证码，而是程序中留一个&#8220;后门&#8221;<span style="font-family: 'Times New Roman';">---</span><span style="font-family: 宋体;">设置一个&#8220;万能验证码&#8221;，只要用户输入这个&#8220;万能验证码&#8221;，程序就认为验证通过，否则按照原先的验证方式进行验证。</span></span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 14px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> random  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">生成0到10之间的随机数</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> #</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">d = random.uniform(0,10)</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> #</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">print d</span>  <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">生成一个1000到9999之间的随机整数</span> d = random.randint(1000,9999<span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> u<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">生成的随机数:%d </span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span> %<span style="font-size: 12px !important; line-height: 1.5 !important;">d  i </span>= input(u<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">请输入随机数:</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> i  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">if</span> i ==<span style="font-size: 12px !important; line-height: 1.5 !important;"> d:     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> u<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">登录成功!!</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">elif</span> i == 1111<span style="font-size: 12px !important; line-height: 1.5 !important;">:     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> u<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">登录成功!!</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">else</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> u<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">请重新输入验证码！</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">运行结果：</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 14px; line-height: 1.5 !important;">&gt;&gt;&gt; ================================ RESTART ================================ &gt;&gt;&gt;<span style="font-size: 12px !important; line-height: 1.5 !important;">  生成的随机数:</span>3764<span style="font-size: 12px !important; line-height: 1.5 !important;">  请输入随机数:</span>1111 1111<span style="font-size: 12px !important; line-height: 1.5 !important;"> 登录成功!! </span>&gt;&gt;&gt; ================================ RESTART ================================ &gt;&gt;&gt;<span style="font-size: 12px !important; line-height: 1.5 !important;">  生成的随机数:</span>3763<span style="font-size: 12px !important; line-height: 1.5 !important;">  请输入随机数:</span>3763 3763<span style="font-size: 12px !important; line-height: 1.5 !important;"> 登录成功!! </span>&gt;&gt;&gt; ================================ RESTART ================================ &gt;&gt;&gt;<span style="font-size: 12px !important; line-height: 1.5 !important;">  生成的随机数:</span>1928<span style="font-size: 12px !important; line-height: 1.5 !important;">  请输入随机数:</span>1354646 1354646<span style="font-size: 12px !important; line-height: 1.5 !important;"> 请重新输入验证码！</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">random</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">random用于生成随机数</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">randint()</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">randint()方法用于生成随机整数，传递的两个参数分别是随机数的范围，randint(1000，9999)第二个参数要大于第一个参数。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">我们要求用户输入随机数，并且对用户输入做判断，如果等于生成的随机数那么，登录成功，如果等于1111也算登录成功，否则失败。那么等于1111的判断就是一个万能码。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>验证码识别技术</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　例如可以通过Python-tesseract&nbsp;来识别图片验证码，Python-tesseract<span style="font-family: 宋体;">是光学字符识别</span><span style="font-family: 'Times New Roman';">Tesseract&nbsp;OCR</span><span style="font-family: 宋体;">引擎的</span><span style="font-family: 'Times New Roman';">Python</span><span style="font-family: 宋体;">封装类。能够读取任何常规的图片文件</span><span style="font-family: 'Times New Roman';">(JPG,&nbsp;GIF&nbsp;,PNG&nbsp;,&nbsp;TIFF</span><span style="font-family: 宋体;">等</span><span style="font-family: 'Times New Roman';">)</span>。不过，目前市面上的验证码形式繁多，目前任何一种验证码识别技术，识别率都不是<span style="font-family: 'Times New Roman';">100%&nbsp;</span><span style="font-family: 宋体;">。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">记录cookie</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">（<span style="color: #ff0000;">适用于UI自动化测试，且目前在大部应用的用户名密码不记录在cookie 或 进行加密处理。</span>）</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　通过向浏览器中添加<span style="font-family: 'Times New Roman';">cookie&nbsp;</span><span style="font-family: 宋体;">可以绕过登录的验证码，这是比较有意思的一种解决方案。我们可以在用户登录之前，通过</span>add_cookie()方法将用户名密码写入浏览器cookie&nbsp;，再次访问系统登录链接将自动登录。例如下面的方式：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 14px; line-height: 1.5 !important;"><span style="font-size: 12px !important; line-height: 1.5 !important;">.... </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">访问xxxx网站</span> driver.get(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.xxxx.cn/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">将用户名密码写入浏览器cookie</span> driver.add_cookie({<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">name</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>:<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">Login_UserNumber</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>, <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>:<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">username</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">}) driver.add_cookie({</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">name</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>:<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">Login_Passwd</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>, <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>:<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">password</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">}) </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">再次访问xxxx网站，将会自动登录</span> driver.get(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.xxxx.cn/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) .... driver.quit()</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　<span style="font-size: 15px;">　使用cookie进行登录最大的难点是如何获得用户名密码的name&nbsp;，如果找到不到name&nbsp;的名字，就没办法向value&nbsp;中输用户名、密码信息。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　我建议是可以通过get_cookies()方法来获取登录的所有的cookie信息，从而进行找到用户名、密码的name&nbsp;对象的名字；当然，最简单的方法还是询问前端开发人员。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">总结：</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp; &nbsp; 最简单安全，行之有效的方式就是设置万能码，稍微和开发沟通一下就OK了。如果乐于&#8220;闷头苦干自力更生&#8221;的话也可研究验证码识别技术。</span></p><img src ="http://www.blogjava.net/qileilove/aggbug/412047.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-04-08 11:13 <a href="http://www.blogjava.net/qileilove/articles/412047.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>selenium-webdriver(python) (十六) --unittest 框架</title><link>http://www.blogjava.net/qileilove/articles/411893.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Thu, 03 Apr 2014 03:39:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411893.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411893.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411893.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411893.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411893.html</trackback:ping><description><![CDATA[<h2><a id="cb_post_title_url" href="http://www.cnblogs.com/fnng/p/3300788.html" style="color: #223355; text-decoration: none;"><br /></a><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">学习unittest 很好的一个切入点就是从selenium IDE 录制导出脚本。相信不少新手学习selenium 也是从IED 开始的。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">IDE学习参考：</span></p></h2><h2 style="margin-top: 10px; font-size: 1em; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><a id="CategoryEntryList1_EntryStoryList_Entries_TitleUrl_13" href="http://www.cnblogs.com/fnng/archive/2011/10/23/2222157.html" style="color: #1d58d1; text-decoration: none;">菜鸟学自动化测试（一）----selenium IDE</a></span></h2><h2><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">借助<span style="font-family: 'Times New Roman';">IED&nbsp;</span><span style="font-family: 宋体;">录制脚本</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><img src="http://images.cnitblog.com/blog/311516/201309/04124833-b60d511451e44efe850c27ff49a4a4ac.jpg" alt="" style="border: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><img src="http://images.cnitblog.com/blog/311516/201309/04124929-642cd2a2d2d54b488587dd92ee062316.jpg" alt="" style="border: 0px;" /></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">将脚本导出，保存为<span style="font-family: 'Times New Roman';">baidu.py&nbsp;</span><span style="font-family: 宋体;">，通过</span><span style="font-family: 'Times New Roman';">python&nbsp;IDLE</span><span style="font-family: 宋体;">编辑器打开。如下：</span></span></p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver
</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium.webdriver.common.by <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> By
</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium.webdriver.common.keys <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> Keys
</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium.webdriver.support.ui <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> Select
</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium.common.exceptions <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> NoSuchElementException
</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> unittest, time, re

</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> Baidu(unittest.TestCase):
    </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> setUp(self):
        self.driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()
        self.driver.implicitly_wait(</span>30<span style="font-size: 12px !important; line-height: 1.5 !important;">)
        self.base_url </span>= <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">
        self.verificationErrors </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> []
        self.accept_next_alert </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> True
    
    </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> test_baidu(self):
        driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> self.driver
        driver.get(self.base_url </span>+ <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)
        driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">selenium webdriver</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)
        driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">su</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click()
        driver.close()

    </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> is_element_present(self, how, what):
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">try</span>: self.driver.find_element(by=how, value=<span style="font-size: 12px !important; line-height: 1.5 !important;">what)
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">except</span> NoSuchElementException, e: <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> False
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> True

    </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> is_alert_present(self):
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">try</span><span style="font-size: 12px !important; line-height: 1.5 !important;">: self.driver.switch_to_alert()
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">except</span> NoAlertPresentException, e: <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> False
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> True
    
    </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> close_alert_and_get_its_text(self):
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">try</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:
            alert </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> self.driver.switch_to_alert()
            alert_text </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> alert.text
            </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">if</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> self.accept_next_alert:
                alert.accept()
            </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">else</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:
                alert.dismiss()
            </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> alert_text
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">finally</span>: self.accept_next_alert =<span style="font-size: 12px !important; line-height: 1.5 !important;"> True
    
    </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">if</span> <span style="color: #800080; font-size: 12px !important; line-height: 1.5 !important;">__name__</span> == <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">__main__</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:
    unittest.main()</span></span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">加入<span style="font-family: 'Times New Roman';">unittest</span><span style="font-family: 宋体;">框架后，看上去比我们之前见的脚本复杂了很多，除了中间操作浏览器的几行，其它都看不懂，不要急，我们来分析一下</span><span style="font-family: 'Times New Roman';">~</span><span style="font-family: 宋体;">！</span></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="background-color: #c0c0c0;"><strong><span style="font-family: 宋体; font-size: 18pt;">框架分析 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></strong></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span>  unittest  </span></pre></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">相想使用<span style="font-family: 'Times New Roman';">unittest</span><span style="font-family: 宋体;">框架，首先要引入</span><span style="font-family: 'Times New Roman';">unittest&nbsp;</span><span style="font-family: 宋体;">包，这个不多解释。</span></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">class</span> Baidu(unittest.TestCase):</span></pre></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">Baidu<span style="font-family: 宋体;">类继承</span>unittest.TestCase&nbsp;类，从<span style="font-family: 'Times New Roman';">TestCase</span><span style="font-family: 宋体;">类继承是告诉</span><span style="font-family: 'Times New Roman';">unittest</span><span style="font-family: 宋体;">模块的方式，这是一个测试案例</span>。</span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> setUp(self):
　　self.driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()
　　self.base_url </span>= <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span></span></pre></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">setUp&nbsp;<span style="font-family: 宋体;">用于设置初始化的部分，在测试用例执行前，这个方法中的函数将先被调用。这里将浏览器的调用和</span><span style="font-family: 'Times New Roman';">URL</span><span style="font-family: 宋体;">的访问放到初始化部分。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;">self.<span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">verificationErrors</span> = []</span></pre></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">脚本运行时，错误的信息将被打印到这个列表中。</span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;">self.accept_next_alert = True</span></pre></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">是否继续接受下一下警告（字面意思，没找到解释！）</span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> test_baidu(self):
        driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> self.driver
        driver.get(self.base_url </span>+ <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)
        driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">selenium webdriver</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)
        driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">su</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).click()</span></pre></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;test_baidu<span style="font-family: 宋体;">中放置的就是我们的测试脚本了，这部分我们并不陌生；因为我们执行的脚本就在这里。</span></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> is_element_present(self, how, what):
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">try</span>: self.driver.find_element(by=how, value=<span style="font-size: 12px !important; line-height: 1.5 !important;">what)
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">except</span> NoSuchElementException, e: <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> False
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span> True</span></pre></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;<span style="font-size: 15px;">is_element_present<span style="font-family: 宋体;">函数用来查找页面元素是否存在，在这里用处不大，通常删除。</span></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">因为判断页面元素是否存在一般都加在<span style="font-family: 'Times New Roman';">testcase</span><span style="font-family: 宋体;">中。</span></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> is_alert_present(self):
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">try</span><span style="font-size: 12px !important; line-height: 1.5 !important;">: self.driver.switch_to_alert()
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">except</span> NoAlertPresentException, e: <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> False
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span> True</span></pre></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">对弹窗异常的处理</span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> close_alert_and_get_its_text(self):
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">try</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:
            alert </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> self.driver.switch_to_alert()
            alert_text </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> alert.text
            </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">if</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> self.accept_next_alert:
                alert.accept()
            </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">else</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:
                alert.dismiss()
            </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> alert_text
        </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">finally</span>: self.accept_next_alert = True</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">关闭警告和对得到文本框的处理，如果不熟悉python<span style="font-family: 宋体;">的异常处理和</span><span style="font-family: 'Times New Roman';">if&nbsp;</span><span style="font-family: 宋体;">语句的话，请去补基础知识，这里不多解释。</span></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)</span></span></pre></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　tearDown&nbsp;<span style="font-family: 宋体;">方法在每个测试方法执行后调用，这个地方做所有清理工作，如退出浏览器等。</span></span><span style="font-size: 15px;">&nbsp;</span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　self.assertEqual([],&nbsp;self.verificationErrors)&nbsp;是个难点，对前面<span style="color: #008000;">verificationErrors</span>方法获得的列表进行比较；如查<span style="color: #008000;">verificationErrors</span>的列表不为空</span><span style="font-size: 15px;">，</span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">输出列表中的报错信息。</span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　而且，这个东西，也可以将来被你自己更好的调用和使用，根据自己的需要写入你希望的信息。（rabbit&nbsp;<span style="font-family: 宋体;">告诉我的</span>）</span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">if</span> <span style="color: #800080; font-size: 12px !important; line-height: 1.5 !important;">__name__</span> == <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">__main__</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:
　　unittest.main()</span></span></pre></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">unitest.main()<span style="font-family: 宋体;">函数用来测试</span>&nbsp;类中以<span style="font-family: 'Times New Roman';">test</span><span style="font-family: 宋体;">开头的测试用例</span></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 18pt; background-color: #c0c0c0;"><strong><span style="font-family: 宋体;">执行结果 &nbsp; &nbsp; &nbsp;</span></strong></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　这样一一分析下来，我们对unittest&nbsp;<span style="font-family: 宋体;">框架有了初步的了解。运行脚本，因为引入了</span><span style="font-family: 'Times New Roman';">unittest&nbsp;</span><span style="font-family: 宋体;">框架，所以控制台输出了脚本执行情况的信息。</span></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div class="cnblogs_code" style="margin: 5px 0px; font-size: 12px; max-width: 900px; font-weight: normal; line-height: 18px;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;">&gt;&gt;&gt; ========================= RESTART ================================
&gt;&gt;&gt;<span style="font-size: 12px !important; line-height: 1.5 !important;"> 
.
</span>----------------------------------------------------------------------<span style="font-size: 12px !important; line-height: 1.5 !important;">
Ran </span>1 test <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">in</span> 10<span style="font-size: 12px !important; line-height: 1.5 !important;">.656s

OK
</span>&gt;&gt;&gt; </span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">很帅吧！？&nbsp;后面将以<span style="font-family: 'Times New Roman';">unittest&nbsp;</span><span style="font-family: 宋体;">为基础，向新的征程进发</span><span style="font-family: 'Times New Roman';">~</span><span style="font-family: 宋体;">！</span></span></p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p class="p0" style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><a id="cb_post_title_url" href="http://www.cnblogs.com/fnng/p/3300788.html"></a></h2><img src ="http://www.blogjava.net/qileilove/aggbug/411893.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-04-03 11:39 <a href="http://www.blogjava.net/qileilove/articles/411893.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>selenium-webdriver(python) (十五) -- 鼠标事件</title><link>http://www.blogjava.net/qileilove/articles/411784.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Tue, 01 Apr 2014 03:37:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411784.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411784.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411784.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411784.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411784.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>本节重点：</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">ActionChains&nbsp;类</span></p><ul style="margin-left: 45px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><li style="list-style: inherit !important;"><span style="font-size: 15px;">&nbsp;&nbsp;context_click()&nbsp;&nbsp;右击</span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">&nbsp;&nbsp;double_click()&nbsp;&nbsp;&nbsp;双击</span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">&nbsp;&nbsp;drag_and_drop()&nbsp;&nbsp;拖动</span></li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">测试的产品中有一个操作是右键点击文件列表会弹出一个快捷菜单，可以方便的选择快捷菜单中的选择对文件进行操作（删除、移动、重命名），之前学习元素的点击非常简单：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">driver.find_element_by_id(&#8220;xxx&#8221;).click()</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">那么鼠标的双击、右击、拖动等是否也是这样的写法呢？例如右击：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">driver.find_element_by_id(&#8220;xxx&#8221;).context_click()</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">经过运行脚本得到了下面的错误提示：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px; color: #ff0000;">AttributeError:&nbsp;'WebElement'&nbsp;object&nbsp;has&nbsp;no&nbsp;attribute&nbsp;'context_click'&nbsp;</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">提示右点方法不属于<span style="font-family: 'Times New Roman';">webelement&nbsp;</span><span style="font-family: 宋体;">对象，通过查找文档，发现属于</span>ActionChains&nbsp;类，但文档中没有具体写法。</span><span style="font-size: 15px;">这里要感谢&nbsp;北京-QC-rabbit&nbsp;的指点，其实整个python+selenium&nbsp;学习过程都要感谢&nbsp;北京-QC-rabbit&nbsp;的指点。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">下面介绍鼠标右键的用法，以快播私有云为例：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium.webdriver.common.action_chains <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> ActionChains </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time  driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() driver.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">登陆快播私有云</span> driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">user_name</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">username</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">user_pwd</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">123456</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">dl_an_submit</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click() time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">定位到要右击的元素</span> qqq =driver.find_element_by_xpath(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">对定位到的元素执行鼠标右键操作</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">ActionChains(driver).context_click(qqq).perform()   </span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'''</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;"> #你也可以使用三行的写法，但我觉得上面两行写法更容易理解 chain = ActionChains(driver) implement = driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]") chain.context_click(implement).perform() </span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'''</span><span style="font-size: 12px !important; line-height: 1.5 !important;">  time.sleep(</span>3) <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">休眠3秒</span> driver.close()</pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">这里需要注意的是，在使用ActionChains&nbsp;类之前，要先将包引入。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">右击的操作会了，下面的其它方法比葫芦画瓢也能写出来。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">鼠标双击的写法：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">定位到要双击的元素</span> qqq =driver.find_element_by_xpath(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">xxx</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">对定位到的元素执行鼠标双击操作</span> ActionChains(driver).double_click(qqq).perform()</pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">鼠标拖放操作的写法：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">定位元素的原位置</span> element = driver.find_element_by_name(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">source</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">定位元素要移动到的目标位置</span> target =  driver.find_element_by_name(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">target</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">执行元素的移动操作</span> ActionChains(driver).drag_and_drop(element, target).perform()</pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">ActionChains&nbsp;类不仅仅是只包含了上面的三个方法，下面将方法列出：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">class&nbsp;ActionChains(driver)</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">driver:The&nbsp;WebDriver&nbsp;instance&nbsp;which&nbsp;performs&nbsp;user&nbsp;actions.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Generate&nbsp;user&nbsp;actions.&nbsp;All&nbsp;actions&nbsp;are&nbsp;stored&nbsp;in&nbsp;the&nbsp;ActionChains&nbsp;object.&nbsp;Call&nbsp;perform()&nbsp;to&nbsp;fire&nbsp;stored&nbsp;actions.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">perform()</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Performs&nbsp;all&nbsp;stored&nbsp;actions.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">click(on_element=None)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Clicks&nbsp;an&nbsp;element.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">on_element:The&nbsp;element&nbsp;to&nbsp;click.&nbsp;If&nbsp;None,&nbsp;clicks&nbsp;on&nbsp;current&nbsp;mouse&nbsp;position.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">click_and_hold(on_element)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Holds&nbsp;down&nbsp;the&nbsp;left&nbsp;mouse&nbsp;button&nbsp;on&nbsp;an&nbsp;element.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">on_element:The&nbsp;element&nbsp;to&nbsp;mouse&nbsp;down.&nbsp;If&nbsp;None,&nbsp;clicks&nbsp;on&nbsp;current&nbsp;mouse&nbsp;position.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">context_click(on_element)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Performs&nbsp;a&nbsp;context-click&nbsp;(right&nbsp;click)&nbsp;on&nbsp;an&nbsp;element.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">on_element:The&nbsp;element&nbsp;to&nbsp;context-click.&nbsp;If&nbsp;None,&nbsp;clicks&nbsp;on&nbsp;current&nbsp;mouse&nbsp;position.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">double_click(on_element)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Double-clicks&nbsp;an&nbsp;element.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">on_element:The&nbsp;element&nbsp;to&nbsp;double-click.&nbsp;If&nbsp;None,&nbsp;clicks&nbsp;on&nbsp;current&nbsp;mouse&nbsp;position.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">drag_and_drop(source,&nbsp;target)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Holds&nbsp;down&nbsp;the&nbsp;left&nbsp;mouse&nbsp;button&nbsp;on&nbsp;the&nbsp;source&nbsp;element,&nbsp;then&nbsp;moves&nbsp;to&nbsp;the&nbsp;target&nbsp;element&nbsp;and&nbsp;releases&nbsp;the&nbsp;mouse&nbsp;button.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">source:The&nbsp;element&nbsp;to&nbsp;mouse&nbsp;down.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">target:&nbsp;The&nbsp;element&nbsp;to&nbsp;mouse&nbsp;up.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">key_down(key,&nbsp;element=None)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Sends&nbsp;a&nbsp;key&nbsp;press&nbsp;only,&nbsp;without&nbsp;releasing&nbsp;it.&nbsp;Should&nbsp;only&nbsp;be&nbsp;used&nbsp;with&nbsp;modifier&nbsp;keys&nbsp;(Control,&nbsp;Alt&nbsp;andShift).</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">key:The&nbsp;modifier&nbsp;key&nbsp;to&nbsp;send.&nbsp;Values&nbsp;are&nbsp;defined&nbsp;in&nbsp;Keys&nbsp;class.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">element:The&nbsp;element&nbsp;to&nbsp;send&nbsp;keys.&nbsp;If&nbsp;None,&nbsp;sends&nbsp;a&nbsp;key&nbsp;to&nbsp;current&nbsp;focused&nbsp;element.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">key_up(key,&nbsp;element=None)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Releases&nbsp;a&nbsp;modifier&nbsp;key.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">key:The&nbsp;modifier&nbsp;key&nbsp;to&nbsp;send.&nbsp;Values&nbsp;are&nbsp;defined&nbsp;in&nbsp;Keys&nbsp;class.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">element:The&nbsp;element&nbsp;to&nbsp;send&nbsp;keys.&nbsp;If&nbsp;None,&nbsp;sends&nbsp;a&nbsp;key&nbsp;to&nbsp;current&nbsp;focused&nbsp;element.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">move_by_offset(xoffset,&nbsp;yoffset)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Moving&nbsp;the&nbsp;mouse&nbsp;to&nbsp;an&nbsp;offset&nbsp;from&nbsp;current&nbsp;mouse&nbsp;position.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">xoffset:X&nbsp;offset&nbsp;to&nbsp;move&nbsp;to.yoffset:Y&nbsp;offset&nbsp;to&nbsp;move&nbsp;to.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">move_to_element(to_element)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Moving&nbsp;the&nbsp;mouse&nbsp;to&nbsp;the&nbsp;middle&nbsp;of&nbsp;an&nbsp;element.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">to_element:&nbsp;The&nbsp;element&nbsp;to&nbsp;move&nbsp;to.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">move_to_element_with_offset(to_element,&nbsp;xoffset,&nbsp;yoffset)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Move&nbsp;the&nbsp;mouse&nbsp;by&nbsp;an&nbsp;offset&nbsp;of&nbsp;the&nbsp;specificed&nbsp;element.&nbsp;Offsets&nbsp;are&nbsp;relative&nbsp;to&nbsp;the&nbsp;top-left&nbsp;corner&nbsp;of&nbsp;the</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">element.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">to_element:&nbsp;The&nbsp;element&nbsp;to&nbsp;move&nbsp;to.xoffset:X&nbsp;offset&nbsp;to&nbsp;move&nbsp;to.yoffset:Y&nbsp;offset&nbsp;to&nbsp;move&nbsp;to.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">release(on_element)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Releasing&nbsp;a&nbsp;held&nbsp;mouse&nbsp;button.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">on_element:The&nbsp;element&nbsp;to&nbsp;mouse&nbsp;up.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">send_keys(*keys_to_send)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Sends&nbsp;keys&nbsp;to&nbsp;current&nbsp;focused&nbsp;element.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">keys_to_send:The&nbsp;keys&nbsp;to&nbsp;send.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　&#8211;&nbsp;<span style="font-family: 'Times New Roman';">send_keys_to_element(self,&nbsp;element,*keys_to_send):</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">Sends&nbsp;keys&nbsp;to&nbsp;an&nbsp;element.</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">element:The&nbsp;element&nbsp;to&nbsp;send&nbsp;keys.keys_to_send:The&nbsp;keys&nbsp;to&nbsp;send.</p><img src ="http://www.blogjava.net/qileilove/aggbug/411784.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-04-01 11:37 <a href="http://www.blogjava.net/qileilove/articles/411784.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>selenium-webdriver(python) (十四) -- webdriver原理</title><link>http://www.blogjava.net/qileilove/articles/411751.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Mon, 31 Mar 2014 10:51:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411751.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411751.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411751.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411751.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411751.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;之前看乙醇视频中提到，selenium&nbsp;的ruby&nbsp;实现有一个小后门，在代码中加上$DEBUG=1&nbsp;，再运行脚本的过程中，就可以看到客户端请求的信息与服务器端返回的数据；觉得这个功能很强大，可以帮助理解webdriver的运行原理。后来查了半天，python并没有提供这样一个方便的后门，不过我们可以通过代理的方式获得这些交互信息；&nbsp;一、需要安装j...&nbsp;&nbsp;<a href='http://www.blogjava.net/qileilove/articles/411751.html'>阅读全文</a><img src ="http://www.blogjava.net/qileilove/aggbug/411751.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-31 18:51 <a href="http://www.blogjava.net/qileilove/articles/411751.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>selenium-webdriver(python) (十三) -- cookie处理</title><link>http://www.blogjava.net/qileilove/articles/411609.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Fri, 28 Mar 2014 06:32:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411609.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411609.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411609.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411609.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411609.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">本节重点<span style="font-family: 'Times New Roman';">:</span></span></p><ul style="margin-left: 45px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><li style="list-style: inherit !important;"><span style="font-size: 15px;">driver.get_cookies（）&nbsp;获得<span style="font-family: 'Times New Roman';">cookie</span><span style="font-family: 宋体;">信息</span></span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">add_cookie(cookie_dict)&nbsp;&nbsp;向<span style="font-family: 'Times New Roman';">cookie</span><span style="font-family: 宋体;">添加会话信息</span></span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">delete_cookie(name)&nbsp;&nbsp;&nbsp;删除特定<span style="font-family: 'Times New Roman';">(</span><span style="font-family: 宋体;">部分</span><span style="font-family: 'Times New Roman';">)</span><span style="font-family: 宋体;">的</span><span style="font-family: 'Times New Roman';">cookie</span></span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">delete_all_cookies()&nbsp;&nbsp;&nbsp;&nbsp;删除所有<span style="font-family: 'Times New Roman';">cookie</span></span></li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">通过<span style="font-family: 'Times New Roman';">webdriver&nbsp;</span><span style="font-family: 宋体;">操作</span><span style="font-family: 'Times New Roman';">cookie&nbsp;</span><span style="font-family: 宋体;">是一件非常有意思的事儿，有时候我们需要了解浏览器中是否存在了某个</span><span style="font-family: 'Times New Roman';">cookie&nbsp;</span><span style="font-family: 宋体;">信息，</span><span style="font-family: 'Times New Roman';">webdriver&nbsp;</span><span style="font-family: 宋体;">可以帮助我们读取、添加，删除</span><span style="font-family: 'Times New Roman';">cookie</span><span style="font-family: 宋体;">信息。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><span style="font-family: 宋体;"><strong>打印cookie信息</strong></span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time  driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Chrome() driver.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.youdao.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 获得cookie信息</span> cookie=<span style="font-size: 12px !important; line-height: 1.5 !important;"> driver.get_cookies()  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">将获得cookie的信息打印</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> cookie  driver.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">运行打印信息：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;">[{u'domain': u'.youdao.com', u'secure': False, u'value': u'aGFzbG9nZ2VkPXRydWU=', u'expiry': 1408430390.991375, u'path': u'/', u'name': u'_PREF_ANONYUSER__MYTH'}, {u'domain': u'.youdao.com', u'secure': False, u'value': u'1777851312@218.17.158.115', u'expiry': 2322974390.991376, u'path': u'/', u'name': u'OUTFOX_SEARCH_USER_ID'}, {u'path': u'/', u'domain': u'www.youdao.com', u'name': u'JSESSIONID', u'value': u'abcUX9zdw0minadIhtvcu', u'secure': False}]</pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>对cookie的操作</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">上面的方式打印了所有<span style="font-family: 'Times New Roman';">cookie</span><span style="font-family: 宋体;">信息表，太多太乱，我们只想有真对性的打印自己想要的信息，看下面的例子</span></span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time  driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() driver.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.youdao.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)   </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">向cookie的name 和value添加会话信息。</span> driver.add_cookie({<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">name</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>:<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">key-aaaaaaa</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>, <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>:<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">value-bbbb</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">})  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">遍历cookies中的name 和value信息打印，当然还有上面添加的信息</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">for</span> cookie <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">in</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> driver.get_cookies():     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">%s -&gt; %s</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span> % (cookie[<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">name</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>], cookie[<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">])   </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 下面可以通过两种方式删除cookie</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> #</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 删除一个特定的cookie</span> driver.delete_cookie(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">CookieName</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 删除所有cookie</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">driver.delete_all_cookies()  time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.close()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">运行打印信息：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 12px !important; line-height: 1.5 !important;">YOUDAO_MOBILE_ACCESS_TYPE -&gt; 1 _PREF_ANONYUSER__MYTH -&gt; aGFzbG9nZ2VkPXRydWU= OUTFOX_SEARCH_USER_ID -&gt; -1046383847@218.17.158.115 JSESSIONID -&gt; abc7qSE_SBGsVgnVLBvcu key-aaaaaaa -&gt; value-bbbb  # 这一条是我们自己添加的</span></pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">通过博客园登陆来分析<span style="font-family: 'Times New Roman';">cookie</span></span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time  driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() driver.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://passport.cnblogs.com/login.aspx?ReturnUrl=http://www.cnblogs.com/fnng/admin/EditPosts.aspx</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.maximize_window() </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 浏览器全屏显示</span>  <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">通过用户名密码登陆</span> driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">tbUserName</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">fnngj</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">tbPassword</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">123456</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">勾选保存密码</span> driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">chkRemember</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click() time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">点击登陆按钮</span> driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">btnLogin</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click()  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">获取cookie信息并打印</span> cookie=<span style="font-size: 12px !important; line-height: 1.5 !important;"> driver.get_cookies() </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> cookie  time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.close()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">运行打印信息：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 12px !important; line-height: 1.5 !important;">#第一次执行信息 &gt;&gt;&gt;  [{u'domain': u'.cnblogs.com', u'name': u'.DottextCookie', u'value': u'C709F15A8BC0B3E8D9AD1F68B371053849F7FEE31F73F1292A150932FF09A7B0D4A1B449A32A6B24AD986CDB05B9998471A37F39C3B637E85E481AA986D3F8C187D7708028F9D4ED3B326B46DC43B416C47B84D706099ED1D78B6A0FC72DCF948DB9D5CBF99D7848FDB78324', u'expiry': None, u'path': u'/', u'secure': False}] &gt;&gt;&gt; ========================= RESTART ================================ #第二次执行信息 &gt;&gt;&gt;  [{u'domain': u'.cnblogs.com', u'name': u'.DottextCookie', u'value': u'5BB735CAD62E99F8CCB9331C32724E2975A0150D199F4243AD19357B3F99A416A93B2E803F4D5C9D065429713BE8B5DB4ED760EDCBAF492EABE2158B3A6FBBEA2B95C4DA3D2EFEADACC3247040906F1462731F652199E2A8BEFD8A9B6AAE87CF3059A3CAEB9AB0D8B1B7AD2A', u'expiry': 1379502502, u'path': u'/', u'secure': False}] &gt;&gt;&gt; </span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">第一次注释掉勾选保存密码的操作，第二次通过勾选保存密码获得<span style="font-family: 'Times New Roman';">cookie</span><span style="font-family: 宋体;">信息&nbsp;；来看两次运行结果的</span><span style="font-family: 'Times New Roman';">cookie</span><span style="font-family: 宋体;">的何不同：</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">u'expiry':&nbsp;None</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">u'expiry':&nbsp;1379502502</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">通过对比发现，不勾选保存密码时<span style="font-family: 'Times New Roman';">expiry&nbsp;</span><span style="font-family: 宋体;">的值为</span><span style="font-family: 'Times New Roman';">none&nbsp;;&nbsp;</span><span style="font-family: 宋体;">那么就可以初步判断勾选保存密码的操作在</span><span style="font-family: 'Times New Roman';">cookie&nbsp;</span><span style="font-family: 宋体;">中起到了作用。至于是否准确可以再做进一步的分析。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><img src ="http://www.blogjava.net/qileilove/aggbug/411609.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-28 14:32 <a href="http://www.blogjava.net/qileilove/articles/411609.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松自动化---selenium-webdriver(python) (十二)之模拟键盘</title><link>http://www.blogjava.net/qileilove/articles/411495.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Wed, 26 Mar 2014 05:26:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411495.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411495.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411495.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411495.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411495.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">本节重点：</p><ul style="margin-left: 45px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><li style="list-style: inherit !important;">l&nbsp;键盘按键用法</li><li style="list-style: inherit !important;">l&nbsp;键盘组合键用法</li><li style="list-style: inherit !important;">l&nbsp;send_keys()&nbsp;<span style="font-family: 宋体;">输入中文运行报错问题</span></li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong>键盘按键键用法：</strong></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium.webdriver.common.keys <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span> Keys  <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">需要引入keys包</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> os,time  driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() driver.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.maximize_window() </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 浏览器全屏显示</span> <span style="font-size: 12px !important; line-height: 1.5 !important;"> driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">user_name</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).clear() driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">user_name</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">fnngj</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">tab的定位相相于清除了密码框的默认提示信息，等同上面的clear()</span> driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">user_name</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).send_keys(Keys.TAB) time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">user_pwd</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">123456</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">通过定位密码框，enter（回车）来代替登陆按钮</span> driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">user_pwd</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).send_keys(Keys.ENTER) </span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'''</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;"> #也可定位登陆按钮，通过enter（回车）代替click() driver.find_element_by_id("login").send_keys(Keys.ENTER) </span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'''</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">)  time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">要想调用键盘按键操作需要引入<span style="font-family: 'Times New Roman';">keys</span><span style="font-family: 宋体;">包：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">from&nbsp;selenium.webdriver.common.keys&nbsp;import&nbsp;Keys</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">通过<span style="font-family: 'Times New Roman';">send_keys()</span><span style="font-family: 宋体;">调用按键：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">send_keys(Keys.TAB) &nbsp; &nbsp; &nbsp; &nbsp;#&nbsp;TAB</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">send_keys(Keys.ENTER) &nbsp; &nbsp;#&nbsp;<span style="font-family: 宋体;">回车</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong>注意</strong>：这个操作和页面元素的遍历顺序有关，假如当前定位在账号输入框，按键盘的<span style="font-family: 'Times New Roman';">tab</span><span style="font-family: 宋体;">键后遍历的不是密码框，那就不法输入密码。&nbsp;假如输入密码后，还有需要填写验证码，那么回车也起不到登陆的效果。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">这并不是一种好的方法，这里只是为了验证单个按键的用法</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong>键盘组合键用法：</strong></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium.webdriver.common.keys <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> Keys </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> os,time  driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()  driver.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">输入框输入内容</span> driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">selenium</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">ctrl+a 全选输入框内容</span> driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(Keys.CONTROL,<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">ctrl+x 剪切输入框内容</span> driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(Keys.CONTROL,<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">x</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">输入框重新输入内容，搜索</span> driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(u<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">虫师 cnblogs</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">su</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click()  time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">上面的操作没有实际意义，但向我们演示了键盘组合按键的用法。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>输入中文问题</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">顺便解决了一个困扰我到现的一个输入中文件的问题。<span style="font-family: 'Times New Roman';">selenium2&nbsp;python</span><span style="font-family: 宋体;">在</span>send_keys()<span style="font-family: 宋体;">中输入中文一直报错，其实前面加个小</span><span style="font-family: 'Times New Roman';">u&nbsp;</span><span style="font-family: 宋体;">就解决了：</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">send_keys(u"输入中文")</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">基本上键盘上所有的按键都可以模拟</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 12px !important; line-height: 1.5 !important;">module:selenium.webdriver.common.keys &#8226; class Keys() &#8211; NULL = u&#8217;ue000&#8217; &#8211; CANCEL = u&#8217;ue001&#8217; # ^break &#8211; HELP = u&#8217;ue002&#8217; &#8211; BACK_SPACE = u&#8217;ue003&#8217; &#8211; TAB = u&#8217;ue004&#8217; &#8211; CLEAR = u&#8217;ue005&#8217; &#8211; RETURN = u&#8217;ue006&#8217; &#8211; ENTER = u&#8217;ue007&#8217; &#8211; SHIFT = u&#8217;ue008&#8217; &#8211; LEFT_SHIFT = u&#8217;ue008&#8217; # alias &#8211; CONTROL = u&#8217;ue009&#8217; &#8211; LEFT_CONTROL = u&#8217;ue009&#8217; # alias &#8211; ALT = u&#8217;ue00a&#8217; &#8211; LEFT_ALT = u&#8217;ue00a&#8217; # alias &#8211; PAUSE = u&#8217;ue00b&#8217; &#8211; ESCAPE = u&#8217;ue00c&#8217; &#8211; SPACE = u&#8217;ue00d&#8217; &#8211; PAGE_UP = u&#8217;ue00e&#8217; &#8211; PAGE_DOWN = u&#8217;ue00f&#8217; &#8211; END = u&#8217;ue010&#8217; &#8211; HOME = u&#8217;ue011&#8217; &#8211; LEFT = u&#8217;ue012&#8217; &#8211; ARROW_LEFT = u&#8217;ue012&#8217; # alias &#8211; UP = u&#8217;ue013&#8217; &#8211; ARROW_UP = u&#8217;ue013&#8217; # alias &#8211; RIGHT = u&#8217;ue014&#8217; &#8211; ARROW_RIGHT = u&#8217;ue014&#8217; # alias &#8211; DOWN = u&#8217;ue015&#8217; &#8211; ARROW_DOWN = u&#8217;ue015&#8217; # alias &#8211; INSERT = u&#8217;ue016&#8217; &#8211; DELETE = u&#8217;ue017&#8217; &#8211; SEMICOLON = u&#8217;ue018&#8217; &#8211; EQUALS = u&#8217;ue019&#8217; &#8211; NUMPAD0 = u&#8217;ue01a&#8217; # numbe pad keys &#8211; NUMPAD1 = u&#8217;ue01b&#8217; &#8211; NUMPAD2 = u&#8217;ue01c&#8217; &#8211; NUMPAD3 = u&#8217;ue01d&#8217; &#8211; NUMPAD4 = u&#8217;ue01e&#8217; &#8211; NUMPAD5 = u&#8217;ue01f&#8217; &#8211; NUMPAD6 = u&#8217;ue020&#8217; &#8211; NUMPAD7 = u&#8217;ue021&#8217; &#8211; NUMPAD8 = u&#8217;ue022&#8217; &#8211; NUMPAD9 = u&#8217;ue023&#8217; &#8211; MULTIPLY = u&#8217;ue024&#8217; &#8211; ADD = u&#8217;ue025&#8217; &#8211; SEPARATOR = u&#8217;ue026&#8217; &#8211; SUBTRACT = u&#8217;ue027&#8217; &#8211; DECIMAL = u&#8217;ue028&#8217; &#8211; DIVIDE = u&#8217;ue029&#8217; &#8211; F1 = u&#8217;ue031&#8217; # function keys &#8211; F2 = u&#8217;ue032&#8217; &#8211; F3 = u&#8217;ue033&#8217; &#8211; F4 = u&#8217;ue034&#8217; &#8211; F5 = u&#8217;ue035&#8217; &#8211; F6 = u&#8217;ue036&#8217; &#8211; F7 = u&#8217;ue037&#8217; &#8211; F8 = u&#8217;ue038&#8217; &#8211; F9 = u&#8217;ue039&#8217; &#8211; F10 = u&#8217;ue03a&#8217; &#8211; F11 = u&#8217;ue03b&#8217; &#8211; F12 = u&#8217;ue03c&#8217; &#8211; META = u&#8217;ue03d&#8217; &#8211; COMMAND = u&#8217;ue03d&#8217;</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">--------------------------<br /></span></p></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">学习更多selenium 内容：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;<a id="homepage1_HomePageDays_ctl04_DayList_TitleUrl_0" href="http://www.cnblogs.com/fnng/p/3157639.html" style="color: #1d58d1; text-decoration: none;">「功能测试自动化」汇总</a></span></p><img src ="http://www.blogjava.net/qileilove/aggbug/411495.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-26 13:26 <a href="http://www.blogjava.net/qileilove/articles/411495.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松自动化---selenium-webdriver(python) (十）处理下拉框</title><link>http://www.blogjava.net/qileilove/articles/411423.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Mon, 24 Mar 2014 08:58:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411423.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411423.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411423.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411423.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411423.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">本节重点</p><ul style="margin-left: 45px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><li style="list-style: inherit !important;">处理下拉框</li><li style="list-style: inherit !important;">switch_to_alert() &nbsp;</li><li style="list-style: inherit !important;">accept()</li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">下拉框是我们最常见的一种页面元素，对于一般的元素，我们只需要一次就定位，但下拉框里的内容需要进行两次定位，先定位到下拉框，再定位到下拉框内里的选项。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">drop_down.html</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">select </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">id</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="ShippingMethod"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> onchange</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="updateShipping(options[selectedIndex]);"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> name</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="ShippingMethod"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="12.51"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>UPS Next Day Air ==&gt; $12.51<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="11.61"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>UPS Next Day Air Saver ==&gt; $11.61<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="10.69"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>UPS 3 Day Select ==&gt; $10.69<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="9.03"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>UPS 2nd Day Air ==&gt; $9.03<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="8.34"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>UPS Ground ==&gt; $8.34<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="9.25"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>USPS Priority Mail Insured ==&gt; $9.25<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="7.45"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>USPS Priority Mail ==&gt; $7.45<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="3.20"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> selected</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">=""</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>USPS First Class ==&gt; $3.20<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">select</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;将上面的代码保存成html通过浏览器打开会看到一个最简单常见的下拉框，下拉列表有几个选项。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">现在我们来选择下拉列表里的<span style="font-family: 宋体; font-size: medium;"><span style="font-family: 'Times New Roman';">$10.69</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">-*-coding=utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> os,time  driver</span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()  file_path </span>=  <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">file:///</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> + os.path.abspath(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">drop_down.html</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  driver.get(file_path)  time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  m</span>=driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">ShippingMethod</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  m.find_element_by_xpath(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">//option[@value='10.69']</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click()  time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">)  driver.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>解析：</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">这里可能和之前的操作有所不同，首先要定位到下拉框的元素，然后选择下拉列表中的选项进行点击操作。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">m=driver.find_element_by_id("ShippingMethod")</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">m.find_element_by_xpath("//option[@value='10.69']").click()</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-family: 宋体; font-size: 15px;">&nbsp;</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-family: 宋体; font-size: 15px;">百度搜索设置下拉框操作</span></strong></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">-*-coding=utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> os,time  driver</span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()  driver.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  <br /></span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">进入搜索设置页</span> <span style="font-size: 12px !important; line-height: 1.5 !important;"> driver.find_element_by_link_text(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">搜索设置</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click()  <br /></span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">设置每页搜索结果为100条</span> <span style="font-size: 12px !important; line-height: 1.5 !important;"> m</span>=driver.find_element_by_name(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">NR</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  m.find_element_by_xpath(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">//option[@value='100']</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click()  time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  <br /></span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">保存设置的信息</span> <span style="font-size: 12px !important; line-height: 1.5 !important;"> driver.find_element_by_xpath(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">//input[@value='保存设置']</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click()  time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  driver.switch_to_alert().accept()  <br /><br /></span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">跳转到百度首页后，进行搜索表（一页应该显示100条结果）</span> <span style="font-size: 12px !important; line-height: 1.5 !important;"> driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">selenium</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">su</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click()  time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">)  driver.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong>解析：</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">当我们在保存百度的设置时会会弹出一个确定按钮；我们并没按照常规的方法去定位弹窗上的&#8220;确定&#8221;按钮，而是使用：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">driver.switch_to_alert().accept()</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;完成了操作，这是因为弹窗比较是一个具有唯一性的警告信息，所以可以用这种简便的方法处理。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&#8211; &nbsp;<span style="color: #ff0000;">switch_to_alert()</span>&nbsp;&nbsp;</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">焦点集中到页面上的一个警告（提示）</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&#8211;<span style="color: #ff0000;">&nbsp;accept()</span></span><br /><span style="font-size: 15px;">接受警告提示</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px;">--------------------------<br /></p></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">学习更多selenium 内容：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;<a id="homepage1_HomePageDays_ctl04_DayList_TitleUrl_0" href="http://www.cnblogs.com/fnng/p/3157639.html" style="color: #1d58d1; text-decoration: none;">「功能测试自动化」汇总</a></p><img src ="http://www.blogjava.net/qileilove/aggbug/411423.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-24 16:58 <a href="http://www.blogjava.net/qileilove/articles/411423.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松自动化---selenium-webdriver(python) (八)执行js语句</title><link>http://www.blogjava.net/qileilove/articles/411326.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Fri, 21 Mar 2014 06:13:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411326.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411326.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411326.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411326.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411326.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>本节重点：</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">调用<span style="font-family: 'Times New Roman';">js</span><span style="font-family: 宋体;">方法</span></span></p><ul style="margin-left: 45px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><li style="list-style: inherit !important;"><span style="font-size: 15px;">execute_script(script,&nbsp;*args)</span></li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">在当前窗口<span style="font-family: 'Times New Roman';">/</span><span style="font-family: 宋体;">框架&nbsp;同步执行</span><span style="font-family: 'Times New Roman';">javaScript</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">脚本：<span style="font-family: 'Times New Roman';">JavaScript</span><span style="font-family: 宋体;">的执行。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">*<span style="font-family: 宋体;">参数：适用任何</span><span style="font-family: 'Times New Roman';">JavaScript</span>脚本。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">使用：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">driver.execute_script<span style="font-family: 宋体;">（</span>&#8216;document.title&#8217;）</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>使快播登陆用户名输入框标红显示</strong>：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time  driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() driver.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fvod.kuaibo.com%2F%3Ft%3Dhome</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">给用户名的输入框标红</span> js=<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">var q=document.getElementById(\"user_name\");q.style.border=\"1px solid red\";</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span> <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">调用js</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">driver.execute_script(js) time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">)  driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">user_name</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">username</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">user_pwd</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">password</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">dl_an_submit</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click() time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">)  driver.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>js解释：</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">q=document.getElementById(\"user_name\")</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">元素<span style="font-family: 'Times New Roman';">q</span><span style="font-family: 宋体;">的</span><span style="font-family: 'Times New Roman';">id&nbsp;</span><span style="font-family: 宋体;">为</span><span style="font-family: 'Times New Roman';">user_name</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">q.style.border=\"1px&nbsp;solid&nbsp;red\</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">元素<span style="font-family: 'Times New Roman';">q</span><span style="font-family: 宋体;">的样式，边框为</span><span style="font-family: 'Times New Roman';">1</span><span style="font-family: 宋体;">个像素红色</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong>隐藏元素</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">js.html</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">head</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>       <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">meta </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">http-equiv</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="content-type"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> content</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="text/html;charset=utf-8"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span>       <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">title</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>js<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">title</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>            <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="text/javascript"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> async</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">=""</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> src</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>       <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">link </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> rel</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="stylesheet"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span>               <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="text/javascript"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span><span style="font-size: 12px !important; line-height: 1.5 !important;">         $(document).ready(</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">function</span><span style="font-size: 12px !important; line-height: 1.5 !important;">(){           $(</span><span style="font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">#tooltip</span><span style="font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).tooltip({</span><span style="font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">placement</span><span style="font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">: </span><span style="font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">right</span><span style="font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">});         });       </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">head</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>      <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>       <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">h3</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>js<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">h3</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>       <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="row-fluid"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="span6 well"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                   <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">id</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="tooltip"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="#"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> data-toggle</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="tooltip"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> title</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">=" selenium-webdriver(python)"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>hover to see tooltip<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>           <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="btn"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Button<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>             <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>             <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">src</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>   <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">(<span style="font-family: 宋体;">保持</span><span style="font-family: 'Times New Roman';">html</span><span style="font-family: 宋体;">文件与执行脚本在同一目录下</span><span style="font-family: 'Times New Roman';">)</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">执行<span style="font-family: 'Times New Roman';">js</span><span style="font-family: 宋体;">一般有两种场景：</span></span></p><ul style="margin-left: 45px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><li style="list-style: inherit !important;"><span style="font-size: 15px;">一种是在页面上直接执行<span style="font-family: 'Times New Roman';">JS</span></span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">另一种是在某个已经定位的元素上执行<span style="font-family: 'Times New Roman';">JS</span></span></li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time,os  driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() file_path </span>=  <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">file:///</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> + os.path.abspath(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">js.html</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.get(file_path)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">######通过JS 隐藏选中的元素#########</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> #</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">第一种方法：</span> driver.execute_script(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">$("#tooltip").fadeOut();</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) time.sleep(</span>5<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">第二种方法：</span> button = driver.find_element_by_class_name(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">btn</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.execute_script(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">$(arguments[0]).fadeOut()</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">,button) time.sleep(</span>5<span style="font-size: 12px !important; line-height: 1.5 !important;">)  driver.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px;"><strong><span style="font-size: 15px;">js<span style="font-family: 宋体;">解释：</span></span></strong></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">arguments对象，它是调用对象的一个特殊属性，用来引用<span style="font-family: 'Times New Roman';">Arguments</span><span style="font-family: 宋体;">对象。</span><span style="font-family: 'Times New Roman';">Arugments</span><span style="font-family: 宋体;">对象就像数组</span>。</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">fadeOut()&nbsp;方法使用淡出效果来隐藏被选元素，假如该元素是隐藏的。</span></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">PS:<span style="font-family: 宋体;">可以看到</span><span style="font-family: 'Times New Roman';">js&nbsp;</span><span style="font-family: 宋体;">可以做</span><span style="font-family: 'Times New Roman';">selenium&nbsp;</span><span style="font-family: 宋体;">做不到的事情，但是在什么样的自动化的时候才能（或必须）要</span><span style="font-family: 'Times New Roman';">js</span><span style="font-family: 宋体;">帮忙，我还没遇到过。不过</span><span style="font-family: 'Times New Roman';">js</span><span style="font-family: 宋体;">可以</span><span style="font-family: 'Times New Roman';">selenium</span><span style="font-family: 宋体;">完成更强大的功能，这是</span>不容置疑的。</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">另外，之前没有学过JS&nbsp;<span style="font-family: 宋体;">，所以</span><span style="font-family: 'Times New Roman';">js</span><span style="font-family: 宋体;">代码很陌生。如果有时间的话也建议各位同学补充这方面的知识。</span><span style="font-family: 'Times New Roman';">UI</span><span style="font-family: 宋体;">自动化离不开前端技术。</span></span></p></div><img src ="http://www.blogjava.net/qileilove/aggbug/411326.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-21 14:13 <a href="http://www.blogjava.net/qileilove/articles/411326.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松自动化---selenium-webdriver(python) (七)---定位iframe</title><link>http://www.blogjava.net/qileilove/articles/411280.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Thu, 20 Mar 2014 06:32:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411280.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411280.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411280.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411280.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411280.html</trackback:ping><description><![CDATA[<div id="cnblogs_post_body" style="margin-bottom: 20px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;"><strong>本节知识点：</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">多层框架或窗口的定位：</span></p><ul style="margin-left: 45px;"><li style="list-style: inherit !important;"><span style="font-size: 15px;">switch_to_frame()</span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">switch_to_window()</span></li></ul><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">智能等待：</span></p><ul style="margin-left: 45px;"><li style="list-style: inherit !important;"><span style="font-size: 15px;">implicitly_wait()</span></li></ul><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">对于一个现代的web应用，经常会出现框架（frame）&nbsp;或窗口（<span style="font-family: 'Times New Roman';">window</span><span style="font-family: 宋体;">）的应用，</span>这也就给我们的定位带来了一个难题。</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">有时候我们定位一个元素，定位器没有问题，但一直定位不了，这时候就要检查这个元素是否在一个frame<span style="font-family: 宋体;">中，</span><span style="font-family: 'Times New Roman';">seelnium&nbsp;&nbsp;webdriver&nbsp;</span><span style="font-family: 宋体;">提供了一个</span>switch_to_frame方法，可以很轻松的来解决这个问题。</span></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">frame.html</span></p><div style="margin: 5px 0px; max-width: 900px; font-size: 12px !important;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">head</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">meta </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">http-equiv</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="content-type"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> content</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="text/html;charset=utf-8"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">title</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>frame<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">title</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="text/javascript"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> async</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">=""</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> src</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js "</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">link </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> rel</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="stylesheet"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="text/javascript"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> $(document).ready(</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">function</span><span style="font-size: 12px !important; line-height: 1.5 !important;">(){ }); </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">head</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="row-fluid"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="span10 well"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">h3</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>frame<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">h3</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">iframe </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">id</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="f1"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> src</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="inner.html"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> width</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="800"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">, height</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="600"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">iframe</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">src</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px;">inner.html</p><div style="margin: 5px 0px; max-width: 900px; font-size: 12px !important;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">head</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">meta </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">http-equiv</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="content-type"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> content</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="text/html;charset=utf-8"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">title</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>inner<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">title</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">head</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="row-fluid"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="span6 well"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">h3</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>inner<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">h3</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">iframe </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">id</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="f2"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> src</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://www.baidu.com"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> width</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="700"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> height</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="500"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">iframe</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="javascript:alert('watir-webdriver better than selenium webdriver;')"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>click<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">frame.html 中嵌套inner.html ，两个文件和我们的脚本文件放同一个目录下：</span></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><strong><span style="font-size: 15px;">switch_to_frame()</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">操作上面页面，代码如下：</span></p><div style="margin: 5px 0px; max-width: 900px; font-size: 12px !important;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> os  browser </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() file_path </span>=  <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">file:///</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> + os.path.abspath(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">frame.html</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.get(file_path)  browser.implicitly_wait(</span>30<span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">先找到到ifrome1（id = f1）</span> browser.switch_to_frame(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">f1</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">再找到其下面的ifrome2(id =f2)</span> browser.switch_to_frame(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">f2</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) <br /><br /></span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">下面就可以正常的操作元素了</span> browser.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">selenium</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">su</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click() time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) <br />browser.quit()</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><strong><span style="font-size: 15px;">driver.switch_to_window()</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">有可能嵌套的不是框架，而是窗口，还有真对窗口的方法：switch_to_window</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">用法与switch_to_frame&nbsp;<span style="font-family: 宋体;">相同：</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">driver.switch_to_window("windowName")</span></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><strong><span style="font-size: 15px;">implicitly_wait()</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">细心的话会发现上面的例子中有browser.implicitly_wait(30)，它的用法应该比time.sleep()&nbsp;<span style="font-family: 宋体;">更智能，后者只能选择一个固定的时间的等待，前者可以在一个时间范围内智能的等待。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;"><strong><span style="font-size: 15px;">文档解释：</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">selenium.webdriver.remote.webdriver.implicitly_wait(time_to_wait)</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">隐式地等待一个无素被发现或一个命令完成；这个方法每次会话只需要调用一次</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">time_to_wait:&nbsp;等待时间</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">用法:</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">driver.implicitly_wait(30)</span></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><div><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">--------------------------<span style="font-family: 'Times New Roman';"><br /></span></span></p></div><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px; color: #ff0000;">学习更多selenium 内容：</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">&nbsp;<a id="homepage1_HomePageDays_ctl04_DayList_TitleUrl_0" href="http://www.cnblogs.com/fnng/p/3157639.html" style="color: #1d58d1; text-decoration: none;">「功能测试自动化」汇总</a></span></p></div><div id="MySignature" style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"></div><div style="clear: both; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"></div><div id="blog_post_info_block" style="margin-top: 20px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><div id="blog_post_info"><div id="BlogPostCategory" style="margin-bottom: 10px;">分类:&nbsp;<a href="http://www.cnblogs.com/fnng/category/349036.html" style="color: #1d58d1; text-decoration: none;">selenium</a></div></div></div><img src ="http://www.blogjava.net/qileilove/aggbug/411280.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-20 14:32 <a href="http://www.blogjava.net/qileilove/articles/411280.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松自动化---selenium-webdriver(python) （六）之操作对象</title><link>http://www.blogjava.net/qileilove/articles/411166.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Tue, 18 Mar 2014 03:01:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411166.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411166.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411166.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411166.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411166.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">本节知识点：</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">操作对象：</span></p><ul style="margin-left: 45px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><li style="list-style: inherit !important;"><span style="font-size: 15px;">&#183;&nbsp;click&nbsp;点击对象</span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">&#183;&nbsp;send_keys&nbsp;在对象上模拟按键输入</span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">&#183;&nbsp;clear&nbsp;清除对象的内容，如果可以的话</span></li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">WebElement&nbsp;&nbsp;另一些常用方法：</span></p><ul style="margin-left: 45px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><li style="list-style: inherit !important;"><span style="font-size: 15px;">&#183;&nbsp;text&nbsp;&nbsp;获取该元素的文本</span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">&#183;&nbsp;submit&nbsp;&nbsp;提交表单</span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">&#183;&nbsp;get_attribute&nbsp;&nbsp;获得属性值</span></li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">======================================</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">操作测试对象</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">前面讲到了不少知识都是定位元素，定位只是第一步，定位之后需要对这个原素进行操作。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">鼠标点击呢还是键盘输入，这要取决于我们定位的是按钮还输入框。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">一般来说，webdriver中比较常用的操作对象的方法有下面几个</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&#183;&nbsp;click&nbsp;点击对象</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&#183;&nbsp;send_keys&nbsp;在对象上模拟按键输入</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&#183;&nbsp;clear&nbsp;清除对象的内容，如果可以的话</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">在我们本系列开篇的第一个例子里就用到了到click&nbsp;和send_skys ，别翻回去找了，我再贴一下代码：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> coding = utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver  browser </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()  browser.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).clear() browser.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">selenium</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">su</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click() browser.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">send_keys("XX")&nbsp;　　用于在一个输入框里输入内容。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">click()&nbsp;　　用于点击一个按钮。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">clear()　　&nbsp;用于清除输入框的内容，比如百度输入框里默认有个&#8220;请输入关键字&#8221;的信息，再比如我们的登陆框一般默认会有&#8220;账号&#8221;&#8220;密码&#8221;这样的默认信息。clear可以帮助我们清除这些信息。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">WebElement&nbsp;&nbsp;另一些常用方法：</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&#183;&nbsp;text&nbsp;&nbsp;获取该元素的文本</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&#183;&nbsp;submit&nbsp;&nbsp;提交表单</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&#183; get_attribute&nbsp;&nbsp;获得属性值</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">text&nbsp;&nbsp;</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">用于获取元素的文本信息</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">下面把百度首页底部的声明打印输出</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;">  time  driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() driver.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">id = cp 元素的文本信息</span> data=driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">cp</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).text </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> data   <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">打印信息</span> <span style="font-size: 12px !important; line-height: 1.5 !important;"> time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">输出：</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;">&gt;&gt;&gt;<span style="font-size: 12px !important; line-height: 1.5 !important;">  &#169;</span>2013 Baidu 使用百度前必读 京ICP证030173号 </pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>submit</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">提交表单</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">我们把&#8220;百度一下&#8221;的操作从click&nbsp;换成submit&nbsp;：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time  driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() driver.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  driver.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">selenium</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">通过submit() 来操作</span> driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">su</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).submit()    time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) driver.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">这里用submit&nbsp;与click的效果一样，我暂时还没想到只能用submit&nbsp;不能用click的场景。他们之间到底有啥区别，知道的同学请留言告诉我。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">get_attribute</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">获得属性值。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">这个函数的用法前面已经有出现过，在定位一组元素的时候有使用到它，只是我们没有做过多的解释。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">一般用法：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;">select = driver.find_element_by_tag_name(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">select</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  allOptions </span>= select.find_elements_by_tag_name(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">option</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">for</span> option <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">in</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> allOptions:      </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">Value is: </span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span> + option.get_attribute(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">value</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)      option.click()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">具体应用参考：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><span style="color: #008000;">定位一组元素</span>：http://www.cnblogs.com/fnng/p/3190966.html</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="color: #ff0000; font-size: 15px;"><strong>小结：</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="color: #ff0000; font-size: 15px;">学到这里我们是不是已经撑握了不少知识，简单的操作浏览器，定位元素，操作元素以及打印一些信息。其实，我们前面的学习中大多使用的是WebElement&nbsp;里的方法。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">WebElement的方法：</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">一般来说，所有有趣的操作与页面进行交互的有趣的操作，都通过&nbsp;WebElement&nbsp;完成</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">classselenium.webdriver.remote.webelement.WebElement(parent,&nbsp;id_)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">这个类代表HTML页面元素</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="font-size: 12px !important; line-height: 1.5 !important;">id_  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">当前元素的ID</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    tag_name  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">获取元素标签名的属性</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    text  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">获取该元素的文本。</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    click()  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">单击（点击）元素</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    submit()  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">提交表单</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    clear()  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">清除一个文本输入元素的文本</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    get_attribute(name)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">获得属性值</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    s_selected(self)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">元素是否被选择</span> <span style="font-size: 12px !important; line-height: 1.5 !important;"> Whether the element </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">is</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> selected.  is_enabled()  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">元素是否被启用</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    find_element_by_id(id_)  find_elements_by_id(id_)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">查找元素的id </span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    find_element_by_name(name)  find_elements_by_name(name)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">查找元素的name</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    find_element_by_link_text(link_text)  find_elements_by_link_text(link_text)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">查找元素的链接文本</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    find_element_by_partial_link_text(link_text)  find_elements_by_partial_link_text(link_text)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">查找元素的链接的部分文本</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    find_element_by_tag_name(name)  find_elements_by_tag_name(name)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">查找元素的标签名</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    find_element_by_xpath(xpath)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">查找元素的xpath</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    find_elements_by_xpath(xpath)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">查找元素内的子元素的xpath</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    find_element_by_class_name(name)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">查找一个元素的类名</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    find_elements_by_class_name(name)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">查找元素的类名</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    find_element_by_css_selector(css_selector)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">查找并返回一个元素的CSS 选择器</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    find_elements_by_css_selector(css_selector)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">查找并返回多个元素的CSS 选择器列表</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">    send_keys(</span>*<span style="font-size: 12px !important; line-height: 1.5 !important;">value)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">模拟输入元素</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">--------------------------<span style="font-family: 'Times New Roman';"><br /></span></span></p></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px; color: #ff0000;">学习更多selenium 内容：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;<a id="homepage1_HomePageDays_ctl04_DayList_TitleUrl_0" href="http://www.cnblogs.com/fnng/p/3157639.html" style="color: #1d58d1; text-decoration: none;">「功能测试自动化」汇总</a></span></p><img src ="http://www.blogjava.net/qileilove/aggbug/411166.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-18 11:01 <a href="http://www.blogjava.net/qileilove/articles/411166.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松自动化---selenium-webdriver(python) (五)--层级定位</title><link>http://www.blogjava.net/qileilove/articles/411133.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Mon, 17 Mar 2014 03:27:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411133.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411133.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411133.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411133.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411133.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">本节要解决的问题：</span></p><ul style="margin-left: 45px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><li style="list-style: inherit !important;"><span style="font-size: 15px;">层级定位</span></li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">场景：</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　假如两个控件，他们长的一模样，还都叫&#8220;张三&#8221;，唯一的不同是一个在北京，一个在上海，那我们就可以通过，他们的城市，区，街道，来找到他们。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　在实际的测试中也经常会遇到这种问题：页面上有很多个属性基本相同的元素，现在需要具体定位到其中的一个。由于属性基本相当，所以在定位的时候会有些麻烦，这时候就需要用到层级定位。先定位父元素，然后再通过父元素定位子孙元素。</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">head</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">meta </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">http-equiv</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="content-type"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> content</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="text/html;charset=utf-8"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">title</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Level Locate<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">title</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="text/javascript"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> async</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">=""</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> src</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">link </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> rel</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="stylesheet"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span>             <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">head</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">h3</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Level locate<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">h3</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="span3"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="well"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="dropdown"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="dropdown-toggle"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> data-toggle</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="dropdown"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="#"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Link1<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">ul </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="dropdown-menu"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> role</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="menu"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> aria-labelledby</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="dLabel"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> id</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="dropdown1"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">tabindex</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="-1"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="#"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Action<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">tabindex</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="-1"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="#"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Another action<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">tabindex</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="-1"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="#"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Something else here<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="divider"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">tabindex</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="-1"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="#"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Separated link<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">ul</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                             <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="span3"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="well"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="dropdown"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="dropdown-toggle"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> data-toggle</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="dropdown"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="#"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Link2<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">ul </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="dropdown-menu"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> role</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="menu"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> aria-labelledby</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="dLabel"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">tabindex</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="-1"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="#"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Action<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">tabindex</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="-1"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="#"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Another action<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">tabindex</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="-1"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="#"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Something else here<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="divider"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">tabindex</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="-1"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="#"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Separated link<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">a</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">li</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">ul</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                             <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">src</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">将这段代码保存复制到记事本中，将保存成level_locate.html文件，（注意，<span style="color: #ff0000;">这个页面需要和我们的自动化脚本放在同一个目录下</span>）浏览器打开：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;<img src="http://images.cnitblog.com/blog/311516/201307/16183421-9ca7d07bc9604ba382a78964ce1d93c9.png" alt="" style="border: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　这里自制了一个页面，上面有两个文字链接，点击两个链接会弹出一模一样的的两个下拉菜单，这两个菜单的属性基本一样。那么我如何区分找到相应的菜单项呢？</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">方法如下：</span></strong></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> -*- coding: utf-8 -*-</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium.webdriver.support.ui <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> WebDriverWait </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> os  dr </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() file_path </span>=  <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">file:///</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> + os.path.abspath(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">level_locate.html</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) dr.get(file_path)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">点击Link1链接（弹出下拉列表）</span> dr.find_element_by_link_text(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">Link1</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click()  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">找到id 为dropdown1的父元素</span> WebDriverWait(dr, 10).until(<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">lambda</span> the_driver: the_driver.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">dropdown1</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).is_displayed()) </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">在父亲元件下找到link为Action的子元素</span> menu = dr.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">dropdown1</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>).find_element_by_link_text(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">Action</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">鼠标定位到子元素上</span> <span style="font-size: 12px !important; line-height: 1.5 !important;">webdriver.ActionChains(dr).move_to_element(menu).perform()  time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  dr.quit()</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">定位思路：</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">具体思路是：先点击显示出<span style="font-family: Helvetica;">1</span><span style="font-family: 宋体;">个下拉菜单，然后再定位到该下拉菜单所在的</span><span style="font-family: Helvetica;">ul</span><span style="font-family: 宋体;">，再定位这个</span><span style="font-family: Helvetica;">ul</span><span style="font-family: 宋体;">下的某个具体的</span><span style="font-family: Helvetica;">link</span><span style="font-family: 宋体;">。在这里，我们定位第</span><span style="font-family: Helvetica;">1</span><span style="font-family: 宋体;">个下拉菜单中的</span>Action这个选项。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;---------------------------------------------------------------------</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">虽然我每行代码前叫了注释，但可能还是不太容易理解，因为里面多了不少以前没见过的新东东。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">WebDriverWait(dr,&nbsp;10)&nbsp;</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">10秒内每隔500毫秒扫描1次页面变化，当出现指定的元素后结束。dr就不解释了，前面操作webdriver.firefox()的句柄</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">is_displayed()</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">该元素是否用户可以见</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">class&nbsp;ActionChains(driver)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">driver:&nbsp;执行用户操作实例<span style="font-family: 'Times New Roman';">webdriver&nbsp;</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">生成用户的行为。所有的行动都存储在<span style="font-family: 'Times New Roman';">actionchains</span><span style="font-family: 宋体;">对象。</span>通过perform()<span style="font-family: 宋体;">存储的行为。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">move_to_element(menu)</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">移动鼠标到一个元素中，menu<span style="font-family: 宋体;">上面已经定义了他所指向的哪一个元素</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">to_element<span style="font-family: 宋体;">：元件移动到</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">perform()</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">执行所有存储的行为</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;------------------------------需要我们日常工作中细细品味、慢慢消化这些函数的用法</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">其实，啰嗦了这么多，我们只是想达到一种效果，&#8220;<span style="color: #0000ff;">下拉列表中Action选项处于被选中状态</span>&#8221;，通过鼠标移动到选项上就达到到了这种效果，但通过程序模拟确实比较麻烦：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;<img src="http://images.cnitblog.com/blog/311516/201307/16184144-4b5f06c0219e40f8b5e126c0eada9ed0.png" alt="" style="border: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">--------------------------<span style="font-family: 'Times New Roman';"><br /></span></span></p></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">学习更多selenium 内容：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;<a id="homepage1_HomePageDays_ctl04_DayList_TitleUrl_0" href="http://www.cnblogs.com/fnng/p/3157639.html" style="color: #1d58d1; text-decoration: none;">「功能测试自动化」汇总</a></span></p><img src ="http://www.blogjava.net/qileilove/aggbug/411133.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-17 11:27 <a href="http://www.blogjava.net/qileilove/articles/411133.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松自动化---selenium-webdriver(python) (四)-定位一组元素</title><link>http://www.blogjava.net/qileilove/articles/411024.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Fri, 14 Mar 2014 03:19:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411024.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411024.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411024.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411024.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411024.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">本节要解决的问题：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">如何定位一组元素？</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">场景</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">从上一节的例子中可以看出，webdriver可以很方便的使用findElement方法来定位某个特定的对象，不过有时候我们却需要定位一组对象，</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">这时候就需要使用findElements方法。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">定位一组对象一般用于以下场景：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&#183;&nbsp;批量操作对象，比如将页面上所有的checkbox都勾上</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&#183;&nbsp;先获取一组对象，再在这组对象中过滤出需要具体定位的一些对象。比如定位出页面上所有的checkbox，然后选择最后一个</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">head</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">meta </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">http-equiv</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="content-type"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> content</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="text/html;charset=utf-8"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">title</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>Checkbox<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">title</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="text/javascript"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> async</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">=""</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> src</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">link </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">href</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> rel</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="stylesheet"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">src</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">script</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">head</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">h3</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>checkbox<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">h3</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="well"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>             <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">form </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="form-horizontal"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="control-group"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">label </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="control-label"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> for</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="c1"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>checkbox1<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">label</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="controls"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">input </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="checkbox"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> id</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="c1"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="control-group"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">label </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="control-label"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> for</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="c2"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>checkbox2<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">label</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="controls"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">input </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="checkbox"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> id</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="c2"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="control-group"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">label </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="control-label"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> for</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="c3"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>checkbox3<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">label</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="controls"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">input </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="checkbox"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> id</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="c3"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                              <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="control-group"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">label </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="control-label"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> for</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="r"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>radio<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">label</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="controls"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">input </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="radio"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> id</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="r1"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                                  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="control-group"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">label </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="control-label"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> for</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="r"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>radio<span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">label</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">class</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="controls"</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">input </span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="radio"</span><span style="color: #ff0000; font-size: 12px !important; line-height: 1.5 !important;"> id</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">="r2"</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">/&gt;</span>                     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>                 <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>             <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">form</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>         <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">div</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span>     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">body</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&lt;/</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">html</span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">&gt;</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">将这段代码保存复制到记事本中，将保存成<span style="font-family: 'Times New Roman';">checkbox.html</span><span style="font-family: 宋体;">文件。（注意，<span style="color: #ff0000;">这个页面需要和我们的自动化脚本放在同一个目录下</span>）</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;<img src="http://images.cnitblog.com/blog/311516/201307/15185828-33c914103c1649e980e6f5c04bffe16d.png" alt="" style="border: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">第一种方法：</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">通过浏览器打个这个页面我们看到三个复选框和两个单选框。下面我们就来定位这三个复选框。</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> -*- coding: utf-8 -*-</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> os  dr </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() file_path </span>=  <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">file:///</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> + os.path.abspath(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">checkbox.html</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) dr.get(file_path)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 选择页面上所有的input，然后从中过滤出所有的checkbox并勾选之</span> inputs = dr.find_elements_by_tag_name(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">input</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">for</span> input <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">in</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> inputs:     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">if</span> input.get_attribute(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">type</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>) == <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">checkbox</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:         input.click() time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  dr.quit()</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">你可以试着把input.get_attribute('type')&nbsp;==&nbsp;'checkbox'&nbsp;中的<span style="font-family: 'Times New Roman';">checkbox&nbsp;</span><span style="font-family: 宋体;">变成</span><span style="font-family: 'Times New Roman';">radio&nbsp;</span><span style="font-family: 宋体;">，那这个脚本定位的会是两个单选框。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">第二种定位方法：</span></strong></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> -*- coding: utf-8 -*-</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> os  dr </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() file_path </span>=  <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">file:///</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> + os.path.abspath(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">checkbox.html</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) dr.get(file_path)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 选择所有的checkbox并全部勾上</span> checkboxes = dr.find_elements_by_css_selector(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">input[type=checkbox]</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">for</span> checkbox <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">in</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> checkboxes:     checkbox.click() time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 打印当前页面上有多少个checkbox</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> len(dr.find_elements_by_css_selector(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">input[type=checkbox]</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)) time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  dr.quit()</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">第二种写法与第一种写法差别不大，都是通过一个循环来勾选控件；如果你学过上一章的话，细心的你一定发现用的定位函数不一样，</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">第一种用的<span style="font-family: 'Times New Roman';">name&nbsp;</span><span style="font-family: 宋体;">，第二种用的</span><span style="font-family: 'Times New Roman';">CSS 。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">&nbsp;如何去掉勾选：</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">还有一个问题，有时候我们并不想勾选页面的所有的复选框（checkbox），可以通过下面办法把最后一个被勾选的框去掉。如下：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> -*- coding: utf-8 -*-</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> os  dr </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox() file_path </span>=  <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">file:///</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> + os.path.abspath(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">checkbox.html</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) dr.get(file_path)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 选择所有的checkbox并全部勾上</span> checkboxes = dr.find_elements_by_css_selector(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">input[type=checkbox]</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">for</span> checkbox <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">in</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> checkboxes:     checkbox.click() time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 把页面上最后1个checkbox的勾给去掉</span> dr.find_elements_by_css_selector(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">input[type=checkbox]</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).pop().click() time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  dr.quit()</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">其实，去掉勾选表也逻辑也非常简单，就是再次点击勾选的按钮。可能我们比较迷惑的是如何找到&#8220;最后一个&#8221;按钮。<span style="font-family: 'Times New Roman';">pop()&nbsp;</span><span style="font-family: 宋体;">可以实现这个功能。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">好吧！在web<span style="font-family: 宋体;">自动化的</span>学习过程中，我们必须要知道一些前端的东西，这里扩展一下：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">http://www.w3school.com.cn/js/jsref_pop.asp</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>尝试</strong>：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">把find_elements_by_css_selector('input[type=checkbox]').pop().click() 中的<span style="font-family: 'Times New Roman';">checkbox&nbsp;</span><span style="font-family: 宋体;">变成</span><span style="font-family: 'Times New Roman';">radio&nbsp;</span><span style="font-family: 宋体;">会是什么效果，自己尝试一下吧！</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><img src ="http://www.blogjava.net/qileilove/aggbug/411024.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-14 11:19 <a href="http://www.blogjava.net/qileilove/articles/411024.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松自动化---selenium-webdriver(python) (三)</title><link>http://www.blogjava.net/qileilove/articles/410976.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Thu, 13 Mar 2014 05:07:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/410976.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/410976.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/410976.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/410976.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/410976.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;本节重点：简单对象的定位　　　　　　-----自动化测试的核心　　对象的定位应该是自动化测试的核心，要想操作一个对象，首先应该识别这个对象。一个对象就是一个人一样，他会有各种的特征（属性），如比我们可以通过一个人的身份证号，姓名，或者他住在哪个街道、楼层、门牌找到这个人。那么一个对象也有类似的属性，我们可以通过这个属性找到这对象。&nbsp;定位对象的目的一般有下面几种&#183;&n...&nbsp;&nbsp;<a href='http://www.blogjava.net/qileilove/articles/410976.html'>阅读全文</a><img src ="http://www.blogjava.net/qileilove/aggbug/410976.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-13 13:07 <a href="http://www.blogjava.net/qileilove/articles/410976.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松自动化---selenium-webdriver(python) (二)</title><link>http://www.blogjava.net/qileilove/articles/410863.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Tue, 11 Mar 2014 03:47:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/410863.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/410863.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/410863.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/410863.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/410863.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">本节知识点：</span></p><ul style="margin-left: 45px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><li style="list-style: inherit !important;"><span style="font-size: 15px;">打印<span style="font-family: 'Times New Roman';">URL</span></span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">将浏览器最大化</span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">设置浏览器固定宽、高</span></li><li style="list-style: inherit !important;"><span style="font-size: 15px;">操控浏览器前进、后退</span></li></ul><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>打印<span style="font-family: 'Times New Roman';">URL</span></strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">上一节讲到，可以将浏览器的<span style="font-family: 'Times New Roman';">title</span><span style="font-family: 宋体;">打印出来，这里再讲个简单的，把当前</span><span style="font-family: 'Times New Roman';">URL</span><span style="font-family: 宋体;">打印出来。其实也没啥大用，可以做个凑数的用例。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time  browser </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()  url</span>= <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>  <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">通过get方法获取当前URL打印</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">now access %s</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span> %<span style="font-size: 12px !important; line-height: 1.5 !important;">(url) browser.get(url)  time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">selenium</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">su</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click() time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.quit()   </span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">其实，我们可以把这用户登录成功后的<span style="font-family: 'Times New Roman';">URL</span><span style="font-family: 宋体;">打印，用于验证用户登录成功。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">又或者，我们打印其它信息，比如，一般的登录成功页会出现&#8220;欢迎<span style="font-family: 'Times New Roman';">+</span><span style="font-family: 宋体;">用户名&#8221;，可以将这个信息打印表明用户登录成功。（如何实现，你自己琢磨一下吧</span><span style="font-family: 'Times New Roman';">~</span><span style="font-family: 宋体;">！）</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>将浏览器最大化</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">我们知道调用启动的浏览器不是全屏的，这样不会影响脚本的执行，但是有时候会影响我们&#8220;观看&#8221;脚本的执行。</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time  browser </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()  browser.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">浏览器最大化</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> browser.maximize_window()  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">将浏览器最大化显示</span> time.sleep(2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  browser.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">selenium</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">su</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click() time.sleep(</span>3<span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.quit()</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>设置浏览器固定宽、高</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">最大化还是不够灵活，能不能随意的设置浏览的宽、高显示？当然是可以的。</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time  browser </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()  browser.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://m.mail.10086.cn</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">设置浏览器宽480、高800显示</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> browser.set_window_size(</span>480, 800)  <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">参数数字为像素点</span> time.sleep(3<span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.quit()</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">这个需求也还是有的，比如我们通过<span style="font-family: 'Times New Roman';">PC</span><span style="font-family: 宋体;">浏览器在访问一下手机网站时，调整浏览器为手机屏幕的宽、高，容易发现一些显示问题。（上面的手机邮箱网站就是笔者测试过的一个产品）</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>操控浏览器前进、后退</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">浏览器上有一个后退、前进按钮，对于浏览网页的人是比较方便的；对于做<span style="font-family: 'Times New Roman';">web</span><span style="font-family: 宋体;">自动化测试的同学来说应该算是一个比较难模拟的问题；其实很简单，下面看看</span><span style="font-family: 'Times New Roman';">python</span><span style="font-family: 宋体;">的实现方式</span></span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time  browser </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">访问百度首页</span> first_url= <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">now access %s</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span> %<span style="font-size: 12px !important; line-height: 1.5 !important;">(first_url) browser.get(first_url) time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">访问新闻页面</span> second_url=<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://news.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">now access %s</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span> %<span style="font-size: 12px !important; line-height: 1.5 !important;">(second_url) browser.get(second_url) time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">返回（后退）到百度首页</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">back to  %s </span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>%<span style="font-size: 12px !important; line-height: 1.5 !important;">(first_url) browser.back() time.sleep(</span>1<span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">前进到新闻页</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">forward to  %s</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>%<span style="font-size: 12px !important; line-height: 1.5 !important;">(second_url) browser.forward() time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)  browser.quit()</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">为了使过程让你看得更清晰，在每一步操作上都加了<span style="font-family: 'Times New Roman';">print&nbsp;</span><span style="font-family: 宋体;">和</span><span style="font-family: 'Times New Roman';">sleep&nbsp;</span><span style="font-family: 宋体;">。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">说实话，这两个功能平时不太常用，所能想到的场景就是几个页面来回跳转，但又不想用<span style="font-family: 'Times New Roman';">get&nbsp;url</span><span style="font-family: 宋体;">的情况下。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">--------------------------<span style="font-family: 'Times New Roman';"><br /></span></span></p></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">学习更多selenium 内容：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;<a id="homepage1_HomePageDays_ctl04_DayList_TitleUrl_0" href="http://www.cnblogs.com/fnng/p/3157639.html" style="color: #1d58d1; text-decoration: none;">「功能测试自动化」汇总</a></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;pasting</p><p style="margin: 0px 0px 14px;">官方API:&nbsp;<a href="http://selenium.googlecode.com/git/docs/api/py/index.html" target="_blank" style="color: #1d58d1; text-decoration: none;">http://selenium.googlecode.com/git/docs/api/py/index.html</a><br /><br />webdriver python:<br /><a href="http://wenku.baidu.com/view/8ecc867f31b765ce050814e6.html" target="_blank" style="color: #1d58d1; text-decoration: none;">http://wenku.baidu.com/view/8ecc867f31b765ce050814e6.html</a></p><img src ="http://www.blogjava.net/qileilove/aggbug/410863.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-11 11:47 <a href="http://www.blogjava.net/qileilove/articles/410863.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>轻松自动化---selenium-webdriver(python) (一)</title><link>http://www.blogjava.net/qileilove/articles/410812.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Mon, 10 Mar 2014 03:28:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/410812.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/410812.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/410812.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/410812.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/410812.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>为什么选<span style="font-family: 'Times New Roman';">python</span><span style="font-family: 宋体;">？</span></strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">之前的菜鸟系列是基于<span style="font-family: 'Times New Roman';">java</span><span style="font-family: 宋体;">的，一年没学其实也忘的差不多了，目前所测的产品部分也是</span><span style="font-family: 'Times New Roman';">python</span><span style="font-family: 宋体;">写的，而且团队也在推广</span><span style="font-family: 'Times New Roman';">python&nbsp;</span><span style="font-family: 宋体;">，其实就测试人员来说，</span><span style="font-family: 'Times New Roman';">python</span><span style="font-family: 宋体;">也相当受欢迎。易学，易用。翻翻各测试招聘，</span><span style="font-family: 'Times New Roman';">python</span><span style="font-family: 宋体;">出现的概率也颇高。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>平台搭建：</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">前一篇中已经介绍，如果你也想体验一下自动化魅力，那就赶快搭建自己的环境吧<span style="font-family: 'Times New Roman';">~</span><span style="font-family: 宋体;">！</span></span></p><h4><span style="font-size: 15px;"><a id="homepage1_HomePageDays_DaysList_DayItem_2_DayList_2_TitleUrl_0" href="http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html" style="color: #1d58d1; text-decoration: none;">selenium + python自动化测试环境搭建</a></span></h4><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>第一个脚本：</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">下面看看<span style="font-family: 'Times New Roman';">python&nbsp;</span><span style="font-family: 宋体;">穿上</span><span style="font-family: 'Times New Roman';">selenium&nbsp;webdriver&nbsp;</span><span style="font-family: 宋体;">是多么的性感：</span></span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> coding = utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver   browser </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()  browser.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">selenium</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">su</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click() browser.quit()</span></span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">怎么样？相信不懂代码的人都能看懂，但还是请容我在这里啰嗦一下每一句的含义：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">#&nbsp;coding&nbsp;=&nbsp;utf-8</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">可加可不加，开发人员喜欢加一下，防止乱码嘛。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">from&nbsp;selenium&nbsp;import&nbsp;webdriver</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">要想使用<span style="font-family: 'Times New Roman';">selenium</span><span style="font-family: 宋体;">的</span><span style="font-family: 'Times New Roman';">webdriver&nbsp;</span><span style="font-family: 宋体;">里的函数，首先把包导进来嘛</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">browser&nbsp;=&nbsp;webdriver.Firefox()&nbsp;</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">我们需要操控哪个浏览器呢？<span style="font-family: 'Times New Roman';">Firefox&nbsp;</span><span style="font-family: 宋体;">，当然也可以换成</span><span style="font-family: 'Times New Roman';">Ie&nbsp;</span><span style="font-family: 宋体;">或&nbsp;</span><span style="font-family: 'Times New Roman';">Chrome&nbsp;</span><span style="font-family: 宋体;">。</span><span style="font-family: 'Times New Roman';">browser</span><span style="font-family: 宋体;">可以随便取，但后面要用它操纵各种函数执行。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">browser.find_element_by_id("kw").send_keys("selenium")</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">一个控件有若干属性<span style="font-family: 'Times New Roman';">id&nbsp;</span><span style="font-family: 宋体;">、</span><span style="font-family: 'Times New Roman';">name</span><span style="font-family: 宋体;">、（也可以用其它方式定位），百度输入框的</span><span style="font-family: 'Times New Roman';">id&nbsp;</span><span style="font-family: 宋体;">叫</span><span style="font-family: 'Times New Roman';">kw&nbsp;</span><span style="font-family: 宋体;">，我要在输入框里输入&nbsp;</span><span style="font-family: 'Times New Roman';">selenium&nbsp;</span><span style="font-family: 宋体;">。多自然语言呀！</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">browser.find_element_by_id("su").click()</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">搜索的按钮的<span style="font-family: 'Times New Roman';">id&nbsp;</span><span style="font-family: 宋体;">叫</span><span style="font-family: 'Times New Roman';">su&nbsp;</span><span style="font-family: 宋体;">，我需要点一下按钮（&nbsp;</span><span style="font-family: 'Times New Roman';">click()&nbsp;</span><span style="font-family: 宋体;">）。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">browser.quit()</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">退出并关闭窗口的每一个相关的驱动程序，它还有个类似的表弟。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">browser.close()</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">关闭当前窗口&nbsp;，用哪个看你的需求了。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>添加休眠</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">什么？你说刚才太快没看清浏览器的操作过程。请<span style="font-family: 'Times New Roman';">time</span><span style="font-family: 宋体;">出马，让他跑慢点。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> coding = utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span>  time  <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">调入time函数</span> <span style="font-size: 12px !important; line-height: 1.5 !important;"> browser </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Firefox()  browser.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) time.sleep(</span>0.3)  <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">休眠0.3秒</span> browser.find_element_by_id(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">kw</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>).send_keys(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">selenium</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) browser.find_element_by_id(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">su</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">).click() time.sleep(</span>3)  <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 休眠3秒</span> browser.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">time.sleep()&nbsp;<span style="font-family: 宋体;">函数随意插，哪里太快插哪里，再也不用担心看不清脚本的运行过程了。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">其实，这个函数的真正用途不是给我们看脚本的运行过程的，有时候网络原因，或页面加载慢。假设搜索框输入框输入了selenium&nbsp;<span style="font-family: 宋体;">，搜索按钮还没加载出来，那么脚本就报错。在适当的位置加入</span><span style="font-family: 微软雅黑;">time.sleep()</span><span style="font-family: 宋体;">有助于减少网络原因造成的脚本执行失败；</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>输出</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">什么？在运行脚本的时候，上了个厕所，你都不知道刚才的脚本是否运行成功了。把刚才访问页面的title&nbsp;<span style="font-family: 宋体;">打印出来。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> coding = utf-8</span>  <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">from</span> selenium <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver  driver </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> webdriver.Chrome() driver.get(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://www.baidu.com</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> driver.title  <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> 把页面title 打印出来</span> driver.quit()</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">虽然我没看到脚本的执行过程，但我在执行结果里看到了</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 15px; line-height: 1.5 !important;">&gt;&gt;&gt;<span style="font-size: 12px !important; line-height: 1.5 !important;">  百度一下，你就知道</span></span></pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">说明页面正确被我打开了。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><div style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">--------------------------<span style="font-family: 'Times New Roman';"><br /></span></span></p></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">学习更多selenium 内容：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;<a id="homepage1_HomePageDays_ctl04_DayList_TitleUrl_0" href="http://www.cnblogs.com/fnng/p/3157639.html" style="color: #1d58d1; text-decoration: none;">「功能测试自动化」汇总</a></span></p><div></div><img src ="http://www.blogjava.net/qileilove/aggbug/410812.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-10 11:28 <a href="http://www.blogjava.net/qileilove/articles/410812.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>selenium webdriver (python)</title><link>http://www.blogjava.net/qileilove/articles/410728.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Fri, 07 Mar 2014 06:15:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/410728.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/410728.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/410728.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/410728.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/410728.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 如果翻译的很烂，原文档地址：&nbsp;http://docs.seleniumhq.org/docs/03_webdriver.jsp#firefox-driver&nbsp;&nbsp;&nbsp;selenium&nbsp;webdriver注:&nbsp;我们不断的更新这份文档，使这份文档更加正确和完善；这份文档相对来讲也是比较准确的。&nbsp;webdriver的简介硒2.0的主要新功...&nbsp;&nbsp;<a href='http://www.blogjava.net/qileilove/articles/410728.html'>阅读全文</a><img src ="http://www.blogjava.net/qileilove/aggbug/410728.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-07 14:15 <a href="http://www.blogjava.net/qileilove/articles/410728.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python读取xml文件</title><link>http://www.blogjava.net/qileilove/articles/410641.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Wed, 05 Mar 2014 05:40:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/410641.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/410641.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/410641.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/410641.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/410641.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 关于python读取xml文章很多，但大多文章都是贴一个xml文件，然后再贴个处理文件的代码。这样并不利于初学者的学习，希望这篇文章可以更通俗易懂的教如何使用python&nbsp;来读取xml&nbsp;文件。&nbsp;什么是xml？xml即可扩展标记语言，它可以用来标记数据、定义数据类型，是一种允许用户对自己的标记语言进行定义的源语言。abc.xml&lt;?xml version="1.0...&nbsp;&nbsp;<a href='http://www.blogjava.net/qileilove/articles/410641.html'>阅读全文</a><img src ="http://www.blogjava.net/qileilove/aggbug/410641.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-05 13:40 <a href="http://www.blogjava.net/qileilove/articles/410641.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python实现简单爬虫功能</title><link>http://www.blogjava.net/qileilove/articles/410583.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Tue, 04 Mar 2014 03:33:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/410583.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/410583.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/410583.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/410583.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/410583.html</trackback:ping><description><![CDATA[<div id="cnblogs_post_body" style="margin-bottom: 20px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">　在我们日常上网浏览网页的时候，经常会看到一些好看的图片，我们就希望把这些图片保存下载，或者用户用来做桌面壁纸，或者用来做设计的素材。</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">　　我们最常规的做法就是通过鼠标右键，选择另存为。但有些图片鼠标右键的时候并没有另存为选项，还有办法就通过就是通过截图工具截取下来，但这样就降低图片的清晰度。好吧～！其实你很厉害的，右键查看页面源代码。</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">　　我们可以通过<span style="font-family: 'Times New Roman';">python&nbsp;</span><span style="font-family: 宋体;">来实现这样一个简单的爬虫功能，把我们想要的代码爬取到本地。下面就看看如何使用</span><span style="font-family: 'Times New Roman';">python</span><span style="font-family: 宋体;">来实现这样一个功能。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><strong><span style="font-size: 15px;">一，获取整个页面数据</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">首先我们可以先获取要下载图片的整个页面信息。</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">getjpg.py</span></p><div style="margin: 5px 0px; max-width: 900px; font-size: 12px !important;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 14px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> urllib  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> getHtml(url):     page </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> urllib.urlopen(url)     html </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> page.read()     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> html  html </span>= getHtml(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://tieba.baidu.com/p/2738151262</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> html</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px;">　　<span style="font-size: 15px;">Urllib&nbsp;<span style="font-family: 宋体;">模块提供了读取</span><span style="font-family: 'Times New Roman';">web</span><span style="font-family: 宋体;">页面数据的接口，我们可以像读取本地文件一样读取</span>www<span style="font-family: 宋体;">和</span><span style="font-family: 'Times New Roman';">ftp</span><span style="font-family: 宋体;">上的数据</span>。首先，我们定义了一个<span style="font-family: 'Times New Roman';">getHtml()</span><span style="font-family: 宋体;">函数</span><span style="font-family: 'Times New Roman';">:</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">　　urllib.urlopen()<span style="font-family: 宋体;">方法用于打开一个</span><span style="font-family: 'Times New Roman';">URL</span><span style="font-family: 宋体;">地址。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">　　read()<span style="font-family: 宋体;">方法用于读取</span><span style="font-family: 'Times New Roman';">URL</span><span style="font-family: 宋体;">上的数据，向</span>getHtml()<span style="font-family: 宋体;">函数</span>传递一个网址，并把整个页面下载下来。执行程序就会把整个网页打印输出。</span></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><strong><span style="font-size: 15px;">二，筛选页面中想要的数据</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">　　Python&nbsp;<span style="font-family: 宋体;">提供了非常强大的正则表达式，我们</span>需要先要了解一点<span style="font-family: 'Times New Roman';">python&nbsp;</span><span style="font-family: 宋体;">正则表达式的知识才行。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;"><a href="http://www.cnblogs.com/fnng/archive/2013/05/20/3089816.html" style="color: #1d58d1; text-decoration: none;">http://www.cnblogs.com/fnng/archive/2013/05/20/3089816.html</a></span></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">假如我们百度贴吧找到了几张漂亮的壁纸，通过到前段查看工具。找到了图片的地址，如：<span style="font-family: 'Times New Roman';">src=&#8221;http://imgsrc.baidu.com/forum......jpg&#8221;</span>pic_ext=&#8221;jpeg&#8221;</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><img src="http://images.cnitblog.com/i/311516/201403/020013141657112.png" alt="" style="border: 0px;" /></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">修改代码如下：</span></p><div style="margin: 5px 0px; max-width: 900px; font-size: 12px !important;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 14px; line-height: 1.5 !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> re </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> urllib  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> getHtml(url):     page </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> urllib.urlopen(url)     html </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> page.read()     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> html  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> getImg(html):     reg </span>= r<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">src="(.+?\.jpg)" pic_ext</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">     imgre </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> re.compile(reg)     imglist </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> re.findall(imgre,html)     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> imglist           html </span>= getHtml(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://tieba.baidu.com/p/2460150866</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> getImg(html)</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px;">　<span style="font-size: 15px;">　我们又创建了<span style="font-family: 'Times New Roman';">getImg()</span><span style="font-family: 宋体;">函数，用于在获取的整个页面中筛选需要的图片连接。</span><span style="font-family: 'Times New Roman';">re</span><span style="font-family: 宋体;">模块主要包含了正则表达式：</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">　　re.compile()&nbsp;可以把正则表达式编译成一个正则表达式对象.</span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">　　re.findall()&nbsp;<span style="font-family: 宋体;">方法读取</span><span style="font-family: 'Times New Roman';">html&nbsp;</span><span style="font-family: 宋体;">中包含&nbsp;</span><span style="font-family: 'Times New Roman';">imgre</span><span style="font-family: 宋体;">（正则表达式）的数据。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">&nbsp;　　&nbsp;运行脚本将得到整个页面中包含图片的<span style="font-family: 'Times New Roman';">URL</span><span style="font-family: 宋体;">地址。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><strong><span style="font-size: 15px;">三，将页面筛选的数据保存到本地</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">把筛选的图片地址通过<span style="font-family: 'Times New Roman';">for</span><span style="font-family: 宋体;">循环遍历并保存到本地，代码如下：</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><div style="margin: 5px 0px; max-width: 900px; font-size: 12px !important;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 14px; line-height: 1.5 !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> urllib </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> re  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> getHtml(url):     page </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> urllib.urlopen(url)     html </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> page.read()     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> html  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">def</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> getImg(html):     reg </span>= r<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">src="(.+?\.jpg)" pic_ext</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">     imgre </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> re.compile(reg)     imglist </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> re.findall(imgre,html)     x </span>=<span style="font-size: 12px !important; line-height: 1.5 !important;"> 0     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">for</span> imgurl <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">in</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> imglist:         urllib.urlretrieve(imgurl,</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">%s.jpg</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> %<span style="font-size: 12px !important; line-height: 1.5 !important;"> x)         x</span>+=1<span style="font-size: 12px !important; line-height: 1.5 !important;">   html </span>= getHtml(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">http://tieba.baidu.com/p/2460150866</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> getImg(html)</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;">　<span style="font-size: 15px;">　这里的核心是用到了urllib.urlretrieve()<span style="font-family: 宋体;">方法，直接将远程数据下载到本地。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">　　通过一个<span style="font-family: 'Times New Roman';">for</span><span style="font-family: 宋体;">循环对获取的图片连接进行遍历，为了使图片的文件名看上去更规范，对其进行重命名，命名规则通过</span><span style="font-family: 'Times New Roman';">x</span><span style="font-family: 宋体;">变量加</span><span style="font-family: 'Times New Roman';">1</span><span style="font-family: 宋体;">。保存的位置默认为程序的存放目录。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px;"><span style="font-size: 15px;">程序运行完成，将在目录下看到下载到本地的文件。</span></p><p style="margin-top: 10px; margin-bottom: 10px;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px;"><img src="http://images.cnitblog.com/i/311516/201403/020014265548094.png" alt="" style="border: 0px;" /></p></div><div id="MySignature" style="font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"></div><div style="clear: both; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"></div><div id="blog_post_info_block" style="margin-top: 20px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><div id="blog_post_info"><div id="BlogPostCategory" style="margin-bottom: 10px;">分类:&nbsp;<a href="http://www.cnblogs.com/fnng/category/454439.html" style="color: #1d58d1; text-decoration: none;">python</a></div></div></div><img src ="http://www.blogjava.net/qileilove/aggbug/410583.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-04 11:33 <a href="http://www.blogjava.net/qileilove/articles/410583.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python使用mysql数据库</title><link>http://www.blogjava.net/qileilove/articles/410535.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Mon, 03 Mar 2014 05:19:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/410535.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/410535.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/410535.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/410535.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/410535.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: ，安装mysql&nbsp;如果是windows&nbsp;用户，mysql&nbsp;的安装非常简单，直接下载安装文件，双击安装文件一步一步进行操作即可。Linux&nbsp;下的安装可能会更加简单，除了下载安装包进行安装外，一般的linux&nbsp;仓库中都会有mysql&nbsp;，我们只需要通过一个命令就可以下载安装：Ubuntu\deepin&gt;&gt;sudo&nbsp;apt-...&nbsp;&nbsp;<a href='http://www.blogjava.net/qileilove/articles/410535.html'>阅读全文</a><img src ="http://www.blogjava.net/qileilove/aggbug/410535.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-03-03 13:19 <a href="http://www.blogjava.net/qileilove/articles/410535.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python异常处理（续）</title><link>http://www.blogjava.net/qileilove/articles/410456.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Fri, 28 Feb 2014 06:48:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/410456.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/410456.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/410456.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/410456.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/410456.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">之前在学习python的时候有整理过python异常处理的文章，不够简单也不够完整，所以决定再整理一篇，算做补充。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><a href="http://www.cnblogs.com/fnng/archive/2013/04/28/3048356.html" style="color: #1d58d1; text-decoration: none;">http://www.cnblogs.com/fnng/archive/2013/04/28/3048356.html</a></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">python shell</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;">&gt;&gt;&gt; open(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">abc.txt</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>,<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">r</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) Traceback (most recent call last):   File </span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">&lt;stdin&gt;</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>, line 1, <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">in</span> &lt;module&gt;<span style="font-size: 12px !important; line-height: 1.5 !important;"> IOError: [Errno </span>2] No such file <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">or</span> directory: <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">abc.txt</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span></pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　打开一个不存在的文件<span style="font-family: 'Times New Roman';">abc.txt&nbsp;</span><span style="font-family: 宋体;">文件，当系统找不到</span><span style="font-family: 'Times New Roman';">abc.txt&nbsp;</span><span style="font-family: 宋体;">文件时，就会抛出给我们一个</span>IOError类型的错误，No&nbsp;such&nbsp;file&nbsp;or&nbsp;directory：<span style="font-family: 'Times New Roman';">abc.txt&nbsp;</span><span style="font-family: 宋体;">（没有</span><span style="font-family: 'Times New Roman';">abc.txt</span><span style="font-family: 宋体;">这样的文件或目录）</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 14pt;"><strong>Try...except...</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　假如，我们已经知道这种类型的错误，那么就可以通过一个异常扑捉来扑捉这个错误。我们可以通过<span style="font-family: 'Times New Roman';">try...except&nbsp;</span><span style="font-family: 宋体;">来接收这个错误。打开文件写入：</span></span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">try</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:     open(</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">abc.txt</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>,<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">r</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">) </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">except</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> IOError:     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">pass</span> </pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;　　再来运行程序就会看不到任何错误，因为我们用<span style="font-family: 'Times New Roman';">except&nbsp;</span><span style="font-family: 宋体;">接收了这个</span><span style="font-family: 'Times New Roman';">IOError</span><span style="font-family: 宋体;">错误。</span><span style="font-family: 'Times New Roman';">pass&nbsp;</span><span style="font-family: 宋体;">表示实现了相应的实现，但什么也不做。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　假如我不是打开一个文件，而是输出一个没有定义的变量呢？</span>&nbsp;</p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">try</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span><span style="font-size: 12px !important; line-height: 1.5 !important;">  aa </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">except</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> IOError:     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">pass</span> </pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">显然，在上面的代码中我并没有对<span style="font-family: 'Times New Roman';">aa&nbsp;</span><span style="font-family: 宋体;">赋值，运行结果：</span></span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 12px !important; line-height: 1.5 !important;">Traceback (most recent call last):   File </span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">/home/fnngj/py_se/tryy.py</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>, line 3, <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">in</span> &lt;module&gt;     <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span><span style="font-size: 12px !important; line-height: 1.5 !important;">  aa NameError: name </span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">aa</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">is</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">not</span> defined</pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　我们已经用<span style="font-family: 'Times New Roman';">except&nbsp;</span><span style="font-family: 宋体;">接收错误了，为什么错误还是还是抛出来了。如果你细心会发现这一次的错误类型是</span>NameError&nbsp;，而我接收类型是<span style="font-family: 'Times New Roman';">IOError&nbsp;</span><span style="font-family: 宋体;">，所以要想接收这个</span><span style="font-family: 'Times New Roman';">print</span><span style="font-family: 宋体;">的错误，那么需要修改接收错误的类型为</span>NameError</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　虽然，我知道<span style="font-family: 'Times New Roman';">print&nbsp;</span><span style="font-family: 宋体;">语句是可能会抛一个</span>NameError&nbsp;类型的错误，虽然接收了这个类型错误，但我不知道具体的错误提示信息是什么。那么，我能不能把错误信息打印出来呢？当然可以：</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">try</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span><span style="font-size: 12px !important; line-height: 1.5 !important;">  aa </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">except</span><span style="font-size: 12px !important; line-height: 1.5 !important;">  NameError, msg:     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span>  msg</pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　我们在接收错误类型的后面定义一个变量<span style="font-family: 'Times New Roman';">msg</span><span style="font-family: 宋体;">用于接收具体错误信息</span><span style="font-family: 'Times New Roman';">,&nbsp;</span><span style="font-family: 宋体;">然后将</span><span style="font-family: 'Times New Roman';">msg</span><span style="font-family: 宋体;">接收的错误信息打印。再来运行程序：</span></span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;">name <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">aa</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">is</span> <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">not</span> defined</pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">现在只打印了一行具体错误信息。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>异常的抛出机制：</strong></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">1、如果在运行时发生异常，解释器会查找相应的处理语句（称为<span style="font-family: 'Times New Roman';">handler</span><span style="font-family: 宋体;">）</span><span style="font-family: 'Times New Roman';">.</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">2、要是在当前函数里没有找到的话，它会将异常传递给上层的调用函数，看看那里能不能处理。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">3、如果在最外层（全局<span style="font-family: 'Times New Roman';">&#8220;main&#8221;</span><span style="font-family: 宋体;">）还是没有找到的话，解释器就会退出，同时打印出</span><span style="font-family: 'Times New Roman';">traceback</span><span style="font-family: 宋体;">以便让用户找到错误产生的原因。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="color: #ff0000; font-size: 15px;">注意：虽然大多数错误会导致异常，但一个异常不一定代表错误，有时候它们只是一个警告，有时候它们可能是一个终止信号，比如退出循环等。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 14pt;">try...finally...</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">try...finally...子句用来表达这样的情况：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">我们不管线捕捉到的是什么错误，无论错误是不是发生，这些代码<span style="font-family: 'Times New Roman';">&#8220;</span><span style="font-family: 宋体;">必须</span><span style="font-family: 'Times New Roman';">&#8221;</span><span style="font-family: 宋体;">运行，比如文件关闭，释放锁，把数据库连接返还给连接池等。</span></span>&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">创建文件poem.txt</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-family: 宋体;"><img src="http://images.cnitblog.com/blog/311516/201401/132247343927.png" alt="" style="border: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">tryf.py</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">import</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> time  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">try</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:     f </span>= file(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">poem.txt</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">while</span> True: <span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;"> our usual file-reading idiom</span>         line =<span style="font-size: 12px !important; line-height: 1.5 !important;"> f.readline()         </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">if</span> len(line) ==<span style="font-size: 12px !important; line-height: 1.5 !important;"> 0:             </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">break</span><span style="font-size: 12px !important; line-height: 1.5 !important;">         time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">)         </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span><span style="font-size: 12px !important; line-height: 1.5 !important;"> line, <br /><br /></span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">finally</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:     f.close()     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">print</span> <span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">Cleaning up...closed the file</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">运行程序<span style="font-family: 'Times New Roman';">(</span><span style="font-family: 宋体;">在</span><span style="font-family: 'Times New Roman';">windows</span><span style="font-family: 宋体;">命令提示符或</span><span style="font-family: 'Times New Roman';">linux</span><span style="font-family: 宋体;">终端下运行</span><span style="font-family: 'Times New Roman';">)</span><span style="font-family: 宋体;">：</span></span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="font-size: 12px !important; line-height: 1.5 !important;">...$ python tryf.py  abc efg </span>^<span style="font-size: 12px !important; line-height: 1.5 !important;">CCleaning up...closed the file Traceback (most recent call last):   File </span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">tryy.py</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">"</span>, line 18, <span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">in</span> &lt;module&gt;<span style="font-size: 12px !important; line-height: 1.5 !important;">     time.sleep(</span>2<span style="font-size: 12px !important; line-height: 1.5 !important;">) KeyboardInterrupt</span></pre><div style="margin-top: 5px;"><span style="padding-right: 5px; font-size: 12px !important; line-height: 1.5 !important;"><a title="复制代码" style="color: #1d58d1; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　程序读poem.txt文件中每一行数据打印，但是我有意在每打印一行之前用<span style="font-family: 'Times New Roman';">time.sleep</span><span style="font-family: 宋体;">方法暂停</span><span style="font-family: 'Times New Roman';">2</span><span style="font-family: 宋体;">秒钟。这样做的原因是让程序运行得慢一些。在程序运行的时候，按</span><span style="font-family: 'Times New Roman';">Ctrl-c</span><span style="font-family: 宋体;">中断</span><span style="font-family: 'Times New Roman';">/</span><span style="font-family: 宋体;">取消程序。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　我们可以观察到<span style="font-family: 'Times New Roman';">KeyboardInterrupt</span><span style="font-family: 宋体;">异常被触发，程序退出。但是在程序退出之前，</span><span style="font-family: 'Times New Roman';">finally</span><span style="font-family: 宋体;">从句仍然被执行，把文件关闭</span>。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">到目前为止，我们只讨论了如何捕捉异常，那么如何抛出异常呢？</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 14pt;">Raise<span style="font-family: 宋体;">抛出异常</span></span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">使用<span style="font-family: 'Times New Roman';">raise</span><span style="font-family: 宋体;">来抛出一个异常：</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">tryr.py</span></p><div style="margin: 5px 0px; font-size: 12px; max-width: 900px; line-height: 18px;"><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px !important; font-family: 'Courier New' !important;"><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">#</span><span style="color: #008000; font-size: 12px !important; line-height: 1.5 !important;">coding=utf-8</span> filename = raw_input(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">please input file name:</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">)  </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">if</span> filename==<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">hello</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="font-size: 12px !important; line-height: 1.5 !important;">:     </span><span style="color: #0000ff; font-size: 12px !important; line-height: 1.5 !important;">raise</span> NameError(<span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">input file name error !</span><span style="color: #800000; font-size: 12px !important; line-height: 1.5 !important;">'</span>)</pre></div><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">　　<span style="font-size: 15px;">程序要求用户输入一个文件名，如果用户输入的文件名是<span style="font-family: 'Times New Roman';">hello&nbsp;</span><span style="font-family: 宋体;">，那么抛出一个</span><span style="font-family: 'Times New Roman';">NameError</span><span style="font-family: 宋体;">的异常，用户输入</span>hello&nbsp;和NameError异常之间没有任何必然联系，我只是人为的通过<span style="font-family: 'Times New Roman';">raise</span><span style="font-family: 宋体;">来这样定义，我当然也可以定义称</span><span style="font-family: 'Times New Roman';">TypeError&nbsp;</span><span style="font-family: 宋体;">，但我定义的异常类型必须是</span><span style="font-family: 'Times New Roman';">python</span><span style="font-family: 宋体;">提供的。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 15px;">附录：</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">常见的python异常类型</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffff;"><span style="font-family: 宋体;"><img src="http://images.cnitblog.com/blog/311516/201401/132257132203.png" alt="" style="border: 0px;" /></span></p><img src ="http://www.blogjava.net/qileilove/aggbug/410456.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-02-28 14:48 <a href="http://www.blogjava.net/qileilove/articles/410456.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python多线程thread</title><link>http://www.blogjava.net/qileilove/articles/410385.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Thu, 27 Feb 2014 06:39:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/410385.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/410385.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/410385.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/410385.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/410385.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 在使用多线程之前，我们首页要理解什么是进程和线程。什么是进程？计算机程序只不过是磁盘中可执行的，二进制（或其它类型）的数据。它们只有在被读取到内存中，被操作系统调用的时候才开始它们的生命期。进程（有时被称为重量级进程）是程序的一次执行。每个进程都有自己的地址空间，内存，数据栈以及其它记录其运行轨迹的辅助数据。操作系统管理在其上运行的所有进程，并为这些进程公平地分配时间。什么是线程？线程（有时被称为...&nbsp;&nbsp;<a href='http://www.blogjava.net/qileilove/articles/410385.html'>阅读全文</a><img src ="http://www.blogjava.net/qileilove/aggbug/410385.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-02-27 14:39 <a href="http://www.blogjava.net/qileilove/articles/410385.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UliPad 初体验----python 开发利器</title><link>http://www.blogjava.net/qileilove/articles/410326.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Wed, 26 Feb 2014 03:51:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/410326.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/410326.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/410326.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/410326.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/410326.html</trackback:ping><description><![CDATA[<h2><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">　<span style="font-size: 15px;">学习<span style="font-family: 'Times New Roman';">python&nbsp;</span><span style="font-family: 宋体;">有段时间，最近博客更新比较慢了，空闲时间在零零碎碎的学</span><span style="font-family: 'Times New Roman';">python&nbsp;</span><span style="font-family: 宋体;">，难成文，也就没整理成博客。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　学习<span style="font-family: 'Times New Roman';">python&nbsp;</span><span style="font-family: 宋体;">最苦恼的就是没有趁手</span><span style="font-family: 'Times New Roman';">IDE&nbsp;</span><span style="font-family: 宋体;">，之前学</span><span style="font-family: 'Times New Roman';">java&nbsp;</span><span style="font-family: 宋体;">时&nbsp;</span><span style="font-family: 'Times New Roman';">Eclipse&nbsp;</span><span style="font-family: 宋体;">肯定是不二之选。</span><span style="font-family: 'Times New Roman';">eclipse&nbsp;pydev&nbsp;</span><span style="font-family: 宋体;">也可以开发</span><span style="font-family: 'Times New Roman';">python</span><span style="font-family: 宋体;">，但觉太重了。&nbsp;所以一直用自带的</span><span style="font-family: 'Times New Roman';">IDLE&nbsp;</span><span style="font-family: 宋体;">，偶尔也用一下</span><span style="font-family: 'Times New Roman';">notepad++&nbsp;</span><span style="font-family: 宋体;">这种小巧的万能编辑器。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　IDLE&nbsp;<span style="font-family: 宋体;">写写单小程序很好，但一个程序与执行信息两个窗口，更别说多标签了，程序找开的多了，就乱了。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　pythonWin&nbsp;<span style="font-family: 宋体;">也用过，窗口有些老土，窗口布局我不会设置，所以觉得也不好用。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-family: 宋体; font-size: 15px;">&nbsp; &nbsp;&nbsp;</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 18pt; background-color: #c0c0c0;">安装<span style="font-family: 'Times New Roman';">wxPython &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span></span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">由于<span style="font-family: 'Times New Roman';">UliPad&nbsp;</span><span style="font-family: 宋体;">是由</span><span style="font-family: 'Times New Roman';">wxPython&nbsp;</span><span style="font-family: 宋体;">开发的，所以，需要先安装</span><span style="font-family: 'Times New Roman';">wxPython&nbsp;</span><span style="font-family: 宋体;">。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>windows</strong><span style="font-family: 宋体;">安装</span><span style="font-family: 'Times New Roman';">wxPython&nbsp;:</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><a href="http://www.wxpython.org/download.php#stable" style="color: #1d58d1; text-decoration: none;">http://www.wxpython.org/download.php#stable</a></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">exe&nbsp;<span style="font-family: 宋体;">程序，下载安装即可</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><strong>ubuntu</strong>&nbsp;<span style="font-family: 宋体;">安装</span><span style="font-family: 'Times New Roman';">wxPython</span><span style="font-family: 宋体;">：</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">打开控制台，输入命令<span style="font-family: Verdana;">:</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="color: #008000; font-size: 15px;">&gt; apt-cache&nbsp;search&nbsp;wxpython</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">如果能返回<span style="font-family: Verdana;">wxpython</span><span style="font-family: 宋体;">的信息，则直接输入命令</span><span style="font-family: Verdana;">:</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="color: #008000; font-size: 15px;">&gt; sudo&nbsp;apt-get&nbsp;install&nbsp;python-wxtools</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">更新完成，打开<span style="font-family: Verdana;">python&nbsp;</span><span style="font-family: 宋体;">输入</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&gt;&gt;&gt;&nbsp;<span style="color: #ff0000;">import</span>&nbsp;&nbsp;wx&nbsp;</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">没有报错，表示安装成功</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 18pt; background-color: #c0c0c0;"><strong>安装</strong><span style="font-family: 'Times New Roman';"><strong>ulipad</strong>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 14px;">下载地址：<a href="https://code.google.com/p/ulipad/downloads/list" style="color: #1d58d1; text-decoration: none;">https://code.google.com/p/ulipad/downloads/list</a></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">我的系统是<span style="font-family: 'Times New Roman';">window&nbsp;xp&nbsp;</span><span style="font-family: 宋体;">（别鄙视我的系统老，公司测试需要</span><span style="font-family: 'Times New Roman';">~</span><span style="font-family: 宋体;">！）</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">找了个<span style="font-family: 'Times New Roman';">4.1&nbsp;</span><span style="font-family: 宋体;">版本（目前最新版）的来安装，安装完成，启动的时候居然报了下面错误</span><span style="font-family: 'Times New Roman';">~</span><span style="font-family: 宋体;">！</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;<img src="http://images.cnitblog.com/blog/311516/201310/28222151-f00b2e0ca07744679de0e71bcbeaad55.jpg" alt="" style="border: 0px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">试着找了一下解决办法，不管用。豆瓣有人问这个问题，大多回答是卸载了装<span style="font-family: 'Times New Roman';">4.0&nbsp;</span><span style="font-family: 宋体;">版本的；</span>于是在<span style="font-family: 'Times New Roman';">CSDN&nbsp;</span><span style="font-family: 宋体;">上找了个</span><span style="font-family: 'Times New Roman';">4.0&nbsp;</span><span style="font-family: 宋体;">版本的，装上之后果然就可以正常打开了。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><a href="http://download.csdn.net/detail/yyyzlf/2410382" style="color: #1d58d1; text-decoration: none;">http://download.csdn.net/detail/yyyzlf/2410382</a></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">安装过程就不说了，<span style="font-family: 'Times New Roman';">exe</span><span style="font-family: 宋体;">程序一跳&#8220;</span><span style="font-family: 'Times New Roman';">next</span></span><span style="font-family: 宋体;"><span style="font-size: 15px;">&#8221;就行了</span>。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 14px;">ubuntu 安装：</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 14px;">下载<span style="color: #000080;">ulipad.4.1.zip</span>&nbsp;安装包</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 14px;">root@fnngj-H24X:/home/fnngj/python#&nbsp;<span style="color: #000080;">unzip ulipad.4.1.zip</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 14px;">root@fnngj-H24X:/home/fnngj/python#&nbsp;&nbsp;<span style="color: #000080;">cd ulipad.4.1</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 14px;">root@fnngj-H24X:/home/fnngj/python#&nbsp;&nbsp;<span style="color: #000080;">./UliPad.py</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 18pt; background-color: #c0c0c0;">运行 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">打开程序之后，界面非常友好，创建个程序运行一下，错误来了！</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><img src="http://images.cnitblog.com/blog/311516/201310/28222248-4e424093a1024c9bb3972125416abb94.png" alt="" style="border: 0px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;　　提示我没有设置python&nbsp;<span style="font-family: 宋体;">的解释器。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　于是，在菜单&nbsp;<span style="color: #800000;">编辑<span style="font-family: 'Times New Roman';">---</span><span style="font-family: 宋体;">参数</span></span><span style="font-family: 'Times New Roman';"><span style="color: #800000;">---python</span>&nbsp;</span><span style="font-family: 宋体;">中找到了&#8220;<span style="color: #800000;">设置</span></span><span style="font-family: 'Times New Roman'; color: #800000;">python</span><span style="font-family: 宋体;"><span style="color: #800000;">编辑</span>&#8221;按钮。点击找到自己</span><span style="font-family: 'Times New Roman';">python&nbsp;</span><span style="font-family: 宋体;">的安装目录，把</span><span style="font-family: 'Times New Roman';">python2.7.exe&nbsp;</span><span style="font-family: 宋体;">和</span><span style="font-family: 'Times New Roman';">pythonw2.7.exe&nbsp;</span><span style="font-family: 宋体;">的路径加进来，别忘了加&#8220;描述&#8221;&nbsp;</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;<img src="http://images.cnitblog.com/blog/311516/201310/28222349-ed76c2ae9bf545fdabda299d1b63569d.jpg" alt="" style="border: 0px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">再来运行程序，发现可以正常了。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 18pt; background-color: #c0c0c0;">窗口布局 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">工具栏上有三个按钮一看就是布局的；</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;<img src="http://images.cnitblog.com/blog/311516/201310/28222427-a85f6cbe778b4d83aae183136a2db008.jpg" alt="" style="border: 0px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;左侧显示目录结构，底部显示程序运行结果。这是使用<span style="font-family: 'Times New Roman';">IDE&nbsp;</span><span style="font-family: 宋体;">工具的标准布局，起码用习惯</span><span style="font-family: 'Times New Roman';">eclipse&nbsp;</span><span style="font-family: 宋体;">的同学会感觉很舒适。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">如果你已经有项目的话可以通过右键目录浏览空白处理，增加目录<span style="font-family: 'Times New Roman';">----</span><span style="font-family: 宋体;">打开新目录，将项目导入。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-family: 宋体;"><img src="http://images.cnitblog.com/blog/311516/201310/28222624-6e964caaa3324430b5238fec7a986633.jpg" alt="" style="border: 0px;" /></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-family: 宋体; font-size: 15px;"><span style="background-color: #000000; color: #ffffff;">Py&gt;</span>&nbsp;按钮提供的就是python交互模式，只是自带的版本是2.5.4&nbsp;的有点老了，还不知道怎么设置。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><img src="http://images.cnitblog.com/blog/311516/201310/28222449-f67baaa82b2f4c7186c08784ce06a918.jpg" alt="" style="border: 0px;" />&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 18pt; background-color: #c0c0c0;">代码 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">　<span style="font-size: 15px;">　有洁癖的同学一定对代码的着色有很高的要求，着色的代码&nbsp;会使用程序看去清晰明了。而且语法错误也很容易识别。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">　　自动补齐也是优秀开<span style="font-family: 'Times New Roman';">IDE&nbsp;</span><span style="font-family: 宋体;">必备的，可以提高开发效率，减少语法错误。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">设置语法参数：<span style="color: #800000;">文档<span style="font-family: 'Times New Roman';">----</span><span style="font-family: 宋体;">语法参数</span></span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="color: #800000;"><span style="font-family: 宋体;"><img src="http://images.cnitblog.com/blog/311516/201310/28222752-01e03ea844954654958cbab35c50c432.jpg" alt="" style="border: 0px;" /></span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">不得不说，UliPad&nbsp;的着色与自动补全功能很出色。</span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;<img src="http://images.cnitblog.com/blog/311516/201310/28222726-2b2dd88683c5477fb4c36e77fdaea2f3.jpg" alt="" style="border: 0px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 18pt; background-color: #c0c0c0;">调试 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">要想成为一名优秀的<span style="font-family: 'Times New Roman';">IDE&nbsp;</span><span style="font-family: 宋体;">怎么能没有调试功能呢。你能想到的，作者自然也想到了。工具栏上有个七星瓢虫的图标，自然就是调试按钮了。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><img src="http://images.cnitblog.com/blog/311516/201310/28222823-6526414abedc459ca9e577756f55c5f2.jpg" alt="" style="border: 0px;" />&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><strong><span style="font-size: 18pt; background-color: #c0c0c0;">插件 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">要想变了更强大，必须要支持插件扩展；我们可以结合插件&nbsp;实现脚本管理，正则、<span style="font-family: 'Times New Roman';">django&nbsp;</span><span style="font-family: 宋体;">项目开发等。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">菜单栏<span style="color: #800000;">工具<span style="font-family: 'Times New Roman';">----</span></span></span><span style="font-family: 宋体;"><span style="color: #800000; font-size: 15px;">插件管理</span><br /></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><img src="http://images.cnitblog.com/blog/311516/201310/28222855-6666a8f3a4e245b9b35eeac4b6f58b06.jpg" alt="" style="border: 0px;" />&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">最后，特意看了一下<span style="font-family: 'Times New Roman';">UliPad&nbsp;</span><span style="font-family: 宋体;">的内存占用很低</span><span style="font-family: 'Times New Roman';">10MB&nbsp;</span><span style="font-family: 宋体;">左右；几乎可忽略它的内存占用。好吧</span><span style="font-family: 宋体;">！就是它了。</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;">UliPad&nbsp;<span style="font-family: 宋体;">项目地址：</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; line-height: 18px; background-color: #ffffff;"><span style="font-size: 15px;"><a href="https://code.google.com/p/ulipad/" style="color: #1d58d1; text-decoration: none;">https://code.google.com/p/ulipad/</a></span></p></h2><img src ="http://www.blogjava.net/qileilove/aggbug/410326.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qileilove/" target="_blank">顺其自然EVO</a> 2014-02-26 11:51 <a href="http://www.blogjava.net/qileilove/articles/410326.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>