﻿<?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-文章分类-自动化测试</title><link>http://www.blogjava.net/qileilove/category/54128.html</link><description>不想做屌丝的码农，不是好项目经理！屌丝生涯从此开始！</description><language>zh-cn</language><lastBuildDate>Thu, 15 May 2014 23:41:22 GMT</lastBuildDate><pubDate>Thu, 15 May 2014 23:41:22 GMT</pubDate><ttl>60</ttl><item><title>简单介绍如何使用robotium进行自动化测试</title><link>http://www.blogjava.net/qileilove/articles/413706.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Thu, 15 May 2014 13:20:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/413706.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/413706.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/413706.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/413706.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/413706.html</trackback:ping><description><![CDATA[<p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 18pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -18pt;"><strong style="word-break: break-all;"><span style="word-break: break-all;"><span style="word-break: break-all;">1.<span style="word-break: break-all; font-weight: normal; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></strong><strong style="word-break: break-all;"><span style="word-break: break-all; font-family: 宋体;">创建一个简单的工程</span></strong><span style="word-break: break-all; font-family: 宋体;">（可参考本人的<a target="_self" style="word-break: break-all; text-decoration: underline; color: #009999;"><u style="word-break: break-all;"><strong style="word-break: break-all;">日志</strong></u></a>《第一个应用</span><span style="word-break: break-all;">HelloWorld</span><span style="word-break: break-all; font-family: 宋体;">》，需要对该工程稍做修改）<strong style="word-break: break-all;">作为被测程序，</strong>运行后显示如下界面<strong style="word-break: break-all;">：</strong></span><strong style="word-break: break-all;"></strong></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; line-height: normal !important;">&nbsp;<a href="http://www.51testing.com/batch.download.php?aid=22397" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171139341jxxn.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 18pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -18pt;"><strong style="word-break: break-all;"><span style="word-break: break-all;"><span style="word-break: break-all;">2.<span style="word-break: break-all; font-weight: normal; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></strong><strong style="word-break: break-all;"><span style="word-break: break-all; font-family: 宋体;">创建一个</span><span style="word-break: break-all;"><a target="_self" style="word-break: break-all; text-decoration: underline; color: #009999;"><u style="word-break: break-all;"><strong style="word-break: break-all;">Test</strong></u></a>&nbsp;Project</span></strong></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">1).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">打开</span><span style="word-break: break-all;">eclipse</span><span style="word-break: break-all; font-family: 宋体;">，选择</span><span style="word-break: break-all;">File-&gt;New-&gt;Project&#8230;-&gt;Android Test Project</span><span style="word-break: break-all; font-family: 宋体;">，点击</span><span style="word-break: break-all;">Next</span><span style="word-break: break-all; font-family: 宋体;">。</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; line-height: normal !important;">&nbsp;<a href="http://www.51testing.com/batch.download.php?aid=22398" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171140281CxeH.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">2).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">在</span><span style="word-break: break-all;">Test Project Name</span><span style="word-break: break-all; font-family: 宋体;">中输入<a target="_self" style="word-break: break-all; text-decoration: underline; color: #009999;"><u style="word-break: break-all;"><strong style="word-break: break-all;">测试</strong></u></a>工程的名称，如：</span><span style="word-break: break-all;">HelloWorldTest</span><span style="word-break: break-all; font-family: 宋体;">。选择</span><span style="word-break: break-all;">An existing&nbsp;<a target="_self" style="word-break: break-all; text-decoration: underline; color: #009999;"><u style="word-break: break-all;"><strong style="word-break: break-all;">Android</strong></u></a>&nbsp;Project</span><span style="word-break: break-all; font-family: 宋体;">，点击右边的</span><span style="word-break: break-all;">Browse&#8230;</span><span style="word-break: break-all; font-family: 宋体;">按钮。</span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all; font-family: 宋体;"><a href="http://www.51testing.com/batch.download.php?aid=22399" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171141291TwGt.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">3).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">选择被测工程，如之前创建的</span><span style="word-break: break-all;">HelloWorldnp</span><span style="word-break: break-all; font-family: 宋体;">，点击</span><span style="word-break: break-all;">OK</span><span style="word-break: break-all; font-family: 宋体;">按钮。</span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all; font-family: 宋体;"><a href="http://www.51testing.com/batch.download.php?aid=22400" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171142231zUo6.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">4).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">会自动选择和输入</span><span style="word-break: break-all;">Build Target</span><span style="word-break: break-all; font-family: 宋体;">和</span><span style="word-break: break-all;">Properties</span><span style="word-break: break-all; font-family: 宋体;">，可以按照默认的，不用修改。</span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all; font-family: 宋体;"><a href="http://www.51testing.com/batch.download.php?aid=22401" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_2010111711431619omJ.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">5).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">一个新建的测试工程（</span><span style="word-break: break-all;">HelloWorldTest</span><span style="word-break: break-all; font-family: 宋体;">）就创建好了，如下图：</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; line-height: normal !important;">&nbsp;<a href="http://www.51testing.com/batch.download.php?aid=22402" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171143581wkfC.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 18pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -18pt;"><strong style="word-break: break-all;"><span style="word-break: break-all;"><span style="word-break: break-all;">3.<span style="word-break: break-all; font-weight: normal; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></strong><strong style="word-break: break-all;"><span style="word-break: break-all; font-family: 宋体;">创建一个</span><span style="word-break: break-all;">Test Case</span></strong></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">1).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">在</span><span style="word-break: break-all;">HelloWorldTest-&gt;src-&gt;com.liliandroid.helloworld.test</span><span style="word-break: break-all; font-family: 宋体;">上，右击选择</span><span style="word-break: break-all;">New-&gt;Class</span><span style="word-break: break-all; font-family: 宋体;">，创建一个</span><span style="word-break: break-all;">Test Case</span><span style="word-break: break-all; font-family: 宋体;">。</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; line-height: normal !important;">&nbsp;<a href="http://www.51testing.com/batch.download.php?aid=22403" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_2010111711444916FRr.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">2).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">在</span><span style="word-break: break-all;">Name</span><span style="word-break: break-all; font-family: 宋体;">中输入</span><span style="word-break: break-all;">Test Case</span><span style="word-break: break-all; font-family: 宋体;">的名字，如：</span><span style="word-break: break-all;">UITextTest</span><span style="word-break: break-all; font-family: 宋体;">。</span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all; font-family: 宋体;"><a href="http://www.51testing.com/batch.download.php?aid=22404" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171146061ZqbX.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">3).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">创建好的</span><span style="word-break: break-all;">Test Case</span><span style="word-break: break-all; font-family: 宋体;">如下图：</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; line-height: normal !important;">&nbsp;<a href="http://www.51testing.com/batch.download.php?aid=22405" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_2010111711470517ry2.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 18pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -18pt;"><strong style="word-break: break-all;"><span style="word-break: break-all;"><span style="word-break: break-all;">4.<span style="word-break: break-all; font-weight: normal; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></strong><strong style="word-break: break-all;"><span style="word-break: break-all; font-family: 宋体;">导入</span><span style="word-break: break-all;">robotium.jar</span></strong></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">1).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">选择测试工程</span><span style="word-break: break-all;">HelloWorldTest</span><span style="word-break: break-all; font-family: 宋体;">右击，选择</span><span style="word-break: break-all;">Build Path-&gt;Configure Build Path&#8230;</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; line-height: normal !important;">&nbsp;<a href="http://www.51testing.com/batch.download.php?aid=22406" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171147451aNGl.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">2).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">在打开的</span><span style="word-break: break-all;">Properties for HelloWorldTest</span><span style="word-break: break-all; font-family: 宋体;">中点击</span><span style="word-break: break-all;">Add External JARs&#8230;</span><span style="word-break: break-all; font-family: 宋体;">按钮。</span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all; font-family: 宋体;"><a href="http://www.51testing.com/batch.download.php?aid=22407" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171148351A19g.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">3).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">因为之前创建的工程的</span><span style="word-break: break-all;">SDK</span><span style="word-break: break-all; font-family: 宋体;">是</span><span style="word-break: break-all;">2.2</span><span style="word-break: break-all; font-family: 宋体;">的，所以选择</span><span style="word-break: break-all;">robotium-solo-1.8.0.jar</span><span style="word-break: break-all; font-family: 宋体;">。</span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all; font-family: 宋体;"><a href="http://www.51testing.com/batch.download.php?aid=22408" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_2010111711492218gV7.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">4).<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">点击</span><span style="word-break: break-all;">OK</span><span style="word-break: break-all; font-family: 宋体;">按钮。</span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 21pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -21pt;"><span style="word-break: break-all; font-family: 宋体;"><a href="http://www.51testing.com/batch.download.php?aid=22409" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171150121INXF.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 18pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -18pt;"><strong style="word-break: break-all;"><span style="word-break: break-all;"><span style="word-break: break-all;">5.<span style="word-break: break-all; font-weight: normal; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></strong><strong style="word-break: break-all;"><span style="word-break: break-all; font-family: 宋体;">修改</span><span style="word-break: break-all;">Test Case</span></strong></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-family: 宋体;">打开</span><span style="word-break: break-all;">UITextTest.java</span><span style="word-break: break-all; font-family: 宋体;">，输入以下内容并保存：</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">package</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">com.liliandroid.helloworld.test;</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">import</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">android.test.ActivityInstrumentationTestCase2;</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">import</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">com.liliandroid.helloworld.ActivityMain;</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">import</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">com.jayway.android.robotium.solo.Solo;</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">public</span></strong><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">class</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">UITextTest</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">extends</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">ActivityInstrumentationTestCase2&lt;ActivityMain&gt;{</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">private</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">Solo</span><span style="word-break: break-all; line-height: normal !important; font-size: 10pt; color: #0000c0; font-family: 'Courier New';">solo</span><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">;</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">public</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">UITextTest() {</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">super</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">(</span><span style="word-break: break-all; line-height: normal !important; font-size: 10pt; color: #2a00ff; font-family: 'Courier New';">"com.liliandroid.helloworld"</span><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">, ActivityMain.</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">class</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">);</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;"><span style="word-break: break-all;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>}</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">public</span></strong><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">void</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">setUp()</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">throws</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">Exception {</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="word-break: break-all; line-height: normal !important; font-size: 10pt; color: #0000c0; font-family: 'Courier New';">solo</span><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">=</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">new</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">Solo(getInstrumentation(), getActivity());</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;"><span style="word-break: break-all;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="word-break: break-all;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;}</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;"><span style="word-break: break-all;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<span style="word-break: break-all;">&nbsp;&nbsp;</span></span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">public</span></strong><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">void</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">testUI()</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">throws</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">Exception {</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">boolean</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">expected =</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">true</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">;</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong style="word-break: break-all; line-height: normal !important;"><span style="word-break: break-all; font-size: 10pt; color: #7f0055; font-family: 'Courier New';">boolean</span></strong><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">actual =</span><span style="word-break: break-all; line-height: normal !important; font-size: 10pt; color: #0000c0; font-family: 'Courier New';">solo</span><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">.searchText(</span><span style="word-break: break-all; line-height: normal !important; font-size: 10pt; color: #2a00ff; font-family: 'Courier New';">"This"</span><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">) &amp;&amp;</span><span style="word-break: break-all; line-height: normal !important; font-size: 10pt; color: #0000c0; font-family: 'Courier New';">solo</span><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">.searchText(</span><span style="word-break: break-all; line-height: normal !important; font-size: 10pt; color: #2a00ff; font-family: 'Courier New';">"is"</span><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">);</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;"><span style="word-break: break-all;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;<em style="word-break: break-all;">assertEquals</em>(</span><span style="word-break: break-all; line-height: normal !important; font-size: 10pt; color: #2a00ff; font-family: 'Courier New';">"This and/or is are not found"</span><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">, expected, actual);</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;"><span style="word-break: break-all;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>}</span></p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p align="left" style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">&nbsp;&nbsp;&nbsp;</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-size: 10pt; font-family: 'Courier New'; line-height: normal !important;">}</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-family: 宋体;">注：这个</span><span style="word-break: break-all;">Case</span><span style="word-break: break-all; font-family: 宋体;">是测试运行了</span><span style="word-break: break-all;">ActivityMain</span><span style="word-break: break-all; font-family: 宋体;">后的界面上是否有</span><span style="word-break: break-all;">This</span><span style="word-break: break-all; font-family: 宋体;">和</span><span style="word-break: break-all;">is</span><span style="word-break: break-all; font-family: 宋体;">这两个文字。</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=22411" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171153451vQj9.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a>&nbsp;</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 18pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -18pt;"><strong style="word-break: break-all;"><span style="word-break: break-all;"><span style="word-break: break-all;">6.<span style="word-break: break-all; font-weight: normal; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span></strong><strong style="word-break: break-all;"><span style="word-break: break-all; font-family: 宋体;">运行测试程序</span></strong></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-family: 宋体;">选择测试工程</span><span style="word-break: break-all;">HelloWorldTest</span><span style="word-break: break-all; font-family: 宋体;">右击，选择</span><span style="word-break: break-all;">Run As-&gt;Android&nbsp;<a target="_self" style="word-break: break-all; text-decoration: underline; color: #009999;"><u style="word-break: break-all;"><strong style="word-break: break-all;">JUnit</strong></u></a>&nbsp;Test</span><span style="word-break: break-all; font-family: 宋体;">运行测试程序</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-family: 宋体;"><a href="http://www.51testing.com/batch.download.php?aid=22412" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171154471v8KZ.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-family: 宋体;">运行完后的测试结果界面（</span><span style="word-break: break-all;">Pass</span><span style="word-break: break-all; font-family: 宋体;">）</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; line-height: normal !important;">&nbsp;<a href="http://www.51testing.com/batch.download.php?aid=22413" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171155311zhue.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a></span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; font-family: 宋体;">运行完后的测试结果界面（</span><span style="word-break: break-all;">Fail</span><span style="word-break: break-all; font-family: 宋体;">）</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=22414" target="_blank" style="word-break: break-all; color: #009999;"><img src="http://www.51testing.com/attachments/2010/11/115892_201011171156021mehX.jpg" border="0" style="word-break: break-all; max-width: 400px;"  alt="" /></a>&nbsp;</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;"><span style="word-break: break-all; line-height: normal !important; font-family: 宋体;">注：</span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 18pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -18pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">1.<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">被测试的工程和测试的工程可以是不同的</span><span style="word-break: break-all;">SDK</span><span style="word-break: break-all; font-family: 宋体;">，但是不知道有什么影响。</span></p><p style="word-break: break-all; line-height: 21.59375px; margin: 0cm 0cm 0pt 18pt; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; text-indent: -18pt;"><span style="word-break: break-all;"><span style="word-break: break-all;">2.<span style="word-break: break-all; font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></span><span style="word-break: break-all; font-family: 宋体;">这是在</span><span style="word-break: break-all;">eclipse</span><span style="word-break: break-all; font-family: 宋体;">中进行测试的，也可以在模拟器或者<a target="_self" style="word-break: break-all; text-decoration: underline; color: #009999;"><u style="word-break: break-all;"><strong style="word-break: break-all;">手机</strong></u></a>上测试。如果需要直接在模拟器或者手机上测试，需要将测试包和被测试包改成相同的签名。（关于</span><span style="word-break: break-all;">apk</span><span style="word-break: break-all; font-family: 宋体;">包的签名和重签名，请参考本人日志《重新签名</span><span style="word-break: break-all;">APK</span><span style="word-break: break-all; font-family: 宋体;">文件》）</span></p><p style="word-break: break-all; line-height: 21.59375px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff;">&nbsp;</p><img src ="http://www.blogjava.net/qileilove/aggbug/413706.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-05-15 21:20 <a href="http://www.blogjava.net/qileilove/articles/413706.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Robotium环境搭建与新手入门教程</title><link>http://www.blogjava.net/qileilove/articles/413704.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Thu, 15 May 2014 13:15:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/413704.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/413704.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/413704.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/413704.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/413704.html</trackback:ping><description><![CDATA[<ul style="margin: 1.5em 2.2em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; list-style: none outside; line-height: 1.5; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif;"><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: disc;"><h1><strong style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent;">准备工具：<a href="http://www.robotium.cn/download" target="_blank" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Robotium资料下载</a></strong></h1></li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: disc;"><h1>知识准备：</h1></li></ul><ol style="margin: 1.5em 2.2em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; list-style: none outside; line-height: 1.5; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif;"><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">java基础知识，如基本的数据结构、语法结构、类、继承等</li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">对Android系统较为熟悉，了解四大组件，会编写简单的Android应用程序</li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">熟悉Eclipse IDE使用方法</li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">有足够的耐心与探索精神，遇到问题可以熟练的<a href="http://www.google.com/" target="_blank" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Google</a></li></ol><ul style="margin: 1.5em 2.2em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; list-style: none outside; line-height: 1.5; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif;"><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: disc;"><h1><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; line-height: 26px;">环境搭建</span></h1></li></ul><ol style="margin: 1.5em 2.2em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; list-style: none outside; line-height: 1.5; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif;"><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">安装jdk，配置环境变量，如果不会请自行<a href="http://www.google.com/" target="_blank" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Google</a></li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">下载安装Android SDK，并更新</li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">下载Eclipse，并安装ADT插件，集成Android SDK</li></ol><ul style="margin: 1.5em 2.2em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; list-style: none outside; line-height: 1.5; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif;"><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: disc;"><h1>Robotium新手入门</h1></li></ul><ol style="margin: 1.5em 2.2em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; list-style: none outside; line-height: 1.5; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif;"><ol style="margin: 1.5em 2.2em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style: none outside; line-height: 1.5;"><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">下载Robotium<a title=" ExampleTestProject_v4.0.zip" href="https://robotium.googlecode.com/files/ExampleTestProject_v4.0.zip" target="_blank" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">官方例子</a>以及最新的<a title=" robotium-solo-4.0.jar" href="https://robotium.googlecode.com/files/robotium-solo-4.0.jar" target="_blank" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">JAR</a>包</li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">打开Eclipse导入被测试工程<p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; orphans: 4;"></p><div id="attachment_211"  aligncenter"="" style="margin: 0px auto 1.5em; padding-top: 5px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; box-sizing: border-box; clear: both; text-align: center; max-width: 100%; height: auto; width: 310px;"><a href="http://www.robotium.cn/wp-content/uploads/2013/03/1.png" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img wp-image-211"="" title="点击查看大图" alt="导入被测工程" src="http://www.robotium.cn/wp-content/uploads/2013/03/1-300x180.png" width="300" height="180" style="margin: 0.75em 0px 5px; padding: 0px; border: 0px none; outline: 0px; vertical-align: baseline; background-color: transparent; max-width: 100%; height: auto;" /></a><p style="margin: 0.6em 0px -0.2em; padding: 0px; border: 0px; outline: 0px; font-size: 0.8em; vertical-align: baseline; background-color: transparent; orphans: 4; font-style: italic; background-position: initial initial; background-repeat: initial initial;">点击查看大图</p></div></li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">创建测试工程：选择Android Test Project，并点击下一步<p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; orphans: 4;"></p><div id="attachment_220"  aligncenter"="" style="margin: 0px auto 1.5em; padding-top: 5px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; box-sizing: border-box; clear: both; text-align: center; max-width: 100%; height: auto; width: 265px;"><a href="http://www.robotium.cn/wp-content/uploads/2013/03/11.png" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img wp-image-220"="" title="点击查看大图" alt="点击查看大图" src="http://www.robotium.cn/wp-content/uploads/2013/03/11-255x300.png" width="255" height="300" style="margin: 0.75em 0px 5px; padding: 0px; border: 0px none; outline: 0px; vertical-align: baseline; background-color: transparent; max-width: 100%; height: auto;" /></a><p style="margin: 0.6em 0px -0.2em; padding: 0px; border: 0px; outline: 0px; font-size: 0.8em; vertical-align: baseline; background-color: transparent; orphans: 4; font-style: italic; background-position: initial initial; background-repeat: initial initial;">点击查看大图</p></div></li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">输入工程名，并点击下一步<p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; orphans: 4;"></p><div id="attachment_220"  aligncenter"="" style="margin: 0px auto 1.5em; padding-top: 5px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; box-sizing: border-box; clear: both; text-align: center; max-width: 100%; height: auto; width: 265px;"><a href="http://www.robotium.cn/wp-content/uploads/2013/03/11.png" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img wp-image-220"="" alt="点击查看大图" src="http://www.robotium.cn/wp-content/uploads/2013/03/11-255x300.png" width="255" height="300" style="margin: 0.75em 0px 5px; padding: 0px; border: 0px none; outline: 0px; vertical-align: baseline; background-color: transparent; max-width: 100%; height: auto;" /></a><p style="margin: 0.6em 0px -0.2em; padding: 0px; border: 0px; outline: 0px; font-size: 0.8em; vertical-align: baseline; background-color: transparent; orphans: 4; font-style: italic; background-position: initial initial; background-repeat: initial initial;">点击查看大图</p></div></li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">选择被测工程NotePad，并点击完成<p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; orphans: 4;"></p><div id="attachment_214"  aligncenter"="" style="margin: 0px auto 1.5em; padding-top: 5px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; box-sizing: border-box; clear: both; text-align: center; max-width: 100%; height: auto; width: 310px;"><a href="http://www.robotium.cn/wp-content/uploads/2013/03/4.png" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img wp-image-214"="" alt="4" src="http://www.robotium.cn/wp-content/uploads/2013/03/4-300x285.png" width="300" height="285" style="margin: 0.75em 0px 5px; padding: 0px; border: 0px none; outline: 0px; vertical-align: baseline; background-color: transparent; max-width: 100%; height: auto;" /></a><p style="margin: 0.6em 0px -0.2em; padding: 0px; border: 0px; outline: 0px; font-size: 0.8em; vertical-align: baseline; background-color: transparent; orphans: 4; font-style: italic; background-position: initial initial; background-repeat: initial initial;">点击查看大图</p></div></li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">在测试工程中新建lib文件夹<p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; orphans: 4;"></p><div id="attachment_215"  aligncenter"="" style="margin: 0px auto 1.5em; padding-top: 5px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; box-sizing: border-box; clear: both; text-align: center; max-width: 100%; height: auto; width: 310px;"><a href="http://www.robotium.cn/wp-content/uploads/2013/03/5.jpg" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img wp-image-215"="" alt="5" src="http://www.robotium.cn/wp-content/uploads/2013/03/5-300x187.jpg" width="300" height="187" style="margin: 0.75em 0px 5px; padding: 0px; border: 0px none; outline: 0px; vertical-align: baseline; background-color: transparent; max-width: 100%; height: auto;" /></a><p style="margin: 0.6em 0px -0.2em; padding: 0px; border: 0px; outline: 0px; font-size: 0.8em; vertical-align: baseline; background-color: transparent; orphans: 4; font-style: italic; background-position: initial initial; background-repeat: initial initial;">点击查看大图</p></div></li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">将下载好的Robotium jar包拷入lib中，并在build path中将jar包引入工程<p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; orphans: 4;"></p><div id="attachment_217"  aligncenter"="" style="margin: 0px auto 1.5em; padding-top: 5px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; box-sizing: border-box; clear: both; text-align: center; max-width: 100%; height: auto; width: 120px;"><a href="http://www.robotium.cn/wp-content/uploads/2013/03/7.jpg" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img wp-image-217"="" alt="7" src="http://www.robotium.cn/wp-content/uploads/2013/03/7-110x300.jpg" width="110" height="300" style="margin: 0.75em 0px 5px; padding: 0px; border: 0px none; outline: 0px; vertical-align: baseline; background-color: transparent; max-width: 100%; height: auto;" /></a><p style="margin: 0.6em 0px -0.2em; padding: 0px; border: 0px; outline: 0px; font-size: 0.8em; vertical-align: baseline; background-color: transparent; orphans: 4; font-style: italic; background-position: initial initial; background-repeat: initial initial;">点击查看大图</p></div></li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">打开测试工程中的manifest.xml文件，修改如下<p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; orphans: 4;"></p><div id="attachment_221"  aligncenter"="" style="margin: 0px auto 1.5em; padding-top: 5px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; box-sizing: border-box; clear: both; text-align: center; max-width: 100%; height: auto; width: 310px;"><a href="http://www.robotium.cn/wp-content/uploads/2013/03/12.png" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img wp-image-221"="" alt="12" src="http://www.robotium.cn/wp-content/uploads/2013/03/12-300x98.png" width="300" height="98" style="margin: 0.75em 0px 5px; padding: 0px; border: 0px none; outline: 0px; vertical-align: baseline; background-color: transparent; max-width: 100%; height: auto;" /></a><p style="margin: 0.6em 0px -0.2em; padding: 0px; border: 0px; outline: 0px; font-size: 0.8em; vertical-align: baseline; background-color: transparent; orphans: 4; font-style: italic; background-position: initial initial; background-repeat: initial initial;">点击查看大图</p></div></li></ol></ol><pre line="1" style="margin: 1.5em; padding: 15px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; white-space: pre-line; word-wrap: break-word; font-family: monospace, sans-serif; color: #444444; line-height: 23px;"></pre><ol style="margin: 1.5em 2.2em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; list-style: none outside; line-height: 1.5; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif;"><ol style="margin: 1.5em 2.2em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style: none outside; line-height: 1.5;"><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">新建junit test case，选择junit4，并输入类名，点击完成<p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; orphans: 4;"></p><div id="attachment_222"  aligncenter"="" style="margin: 0px auto 1.5em; padding-top: 5px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; box-sizing: border-box; clear: both; text-align: center; max-width: 100%; height: auto; width: 262px;"><a href="http://www.robotium.cn/wp-content/uploads/2013/03/13.png" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img wp-image-222"="" alt="13" src="http://www.robotium.cn/wp-content/uploads/2013/03/13-252x300.png" width="252" height="300" style="margin: 0.75em 0px 5px; padding: 0px; border: 0px none; outline: 0px; vertical-align: baseline; background-color: transparent; max-width: 100%; height: auto;" /></a><p style="margin: 0.6em 0px -0.2em; padding: 0px; border: 0px; outline: 0px; font-size: 0.8em; vertical-align: baseline; background-color: transparent; orphans: 4; font-style: italic; background-position: initial initial; background-repeat: initial initial;">点击查看大图</p></div></li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">打开新建的测试类，修改继承ActivityInstrumentationTestCase2&lt;NotesList&gt;，创建构造方法，并编写测试代码，具体如图<p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; orphans: 4;"></p><div id="attachment_223"  aligncenter"="" style="margin: 0px auto 1.5em; padding-top: 5px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; box-sizing: border-box; clear: both; text-align: center; max-width: 100%; height: auto; width: 310px;"><a href="http://www.robotium.cn/wp-content/uploads/2013/03/14.png" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img wp-image-223"="" alt="14" src="http://www.robotium.cn/wp-content/uploads/2013/03/14-300x164.png" width="300" height="164" style="margin: 0.75em 0px 5px; padding: 0px; border: 0px none; outline: 0px; vertical-align: baseline; background-color: transparent; max-width: 100%; height: auto;" /></a><p style="margin: 0.6em 0px -0.2em; padding: 0px; border: 0px; outline: 0px; font-size: 0.8em; vertical-align: baseline; background-color: transparent; orphans: 4; font-style: italic; background-position: initial initial; background-repeat: initial initial;">点击查看大图</p></div></li></ol></ol><pre style="margin: 1.5em; padding: 15px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; white-space: pre-line; word-wrap: break-word; font-family: monospace, sans-serif; color: #444444; line-height: 23px;">package com.example.android.notepad.test;  import android.test.ActivityInstrumentationTestCase2;  import com.example.android.notepad.NotesList; import com.jayway.android.robotium.solo.Solo;  public class NotePadTest extends ActivityInstrumentationTestCase2 { private Solo solo;//声明Solo public NotePadTest()//构造方法 { super(NotesList.class);  }  @Override public void setUp() throws Exception { solo = new Solo(getInstrumentation(), getActivity()); }  @Override public void tearDown() throws Exception { solo.finishOpenedActivities(); }  public void testAddNote() throws Exception { //点击add note按钮 solo.clickOnMenuItem("Add note"); //比对结果 solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); //在第一个TextEdit控件上输入内容 solo.enterText(0, "Note 1"); //返回上个界面 solo.goBack(); //点击菜单中的Add note solo.clickOnMenuItem("Add note"); //在第一个EditText中输入内容 solo.enterText(0, "Note 2"); //返回NotesList activity solo.goBackToActivity("NotesList"); //截屏 solo.takeScreenshot(); boolean expected = true; boolean actual = solo.searchText("Note 1") ; solo.searchText("Note 2"); assertEquals("Note 1 and/or Note 2 are not found", expected, actual);  }  }</pre><ol style="margin: 1.5em 2.2em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; list-style: none outside; line-height: 1.5; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif;"><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">编写完成后，打开模拟器或者连接上真机，右击测试类，选择Android junit Test<p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; orphans: 4;"></p><div id="attachment_224"  aligncenter"="" style="margin: 0px auto 1.5em; padding-top: 5px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; box-sizing: border-box; clear: both; text-align: center; max-width: 100%; height: auto; width: 310px;"><a href="http://www.robotium.cn/wp-content/uploads/2013/03/15.png" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img wp-image-224"="" title="点击查看大图" alt="15" src="http://www.robotium.cn/wp-content/uploads/2013/03/15-300x180.png" width="300" height="180" style="margin: 0.75em 0px 5px; padding: 0px; border: 0px none; outline: 0px; vertical-align: baseline; background-color: transparent; max-width: 100%; height: auto;" /></a><p style="margin: 0.6em 0px -0.2em; padding: 0px; border: 0px; outline: 0px; font-size: 0.8em; vertical-align: baseline; background-color: transparent; orphans: 4; font-style: italic; background-position: initial initial; background-repeat: initial initial;">点击查看大图</p></div></li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">Eclipse会切换到junit界面，此时模拟器（真机）会自动执行测试，通过左边界面可以看到运行结果<p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; orphans: 4;"></p><div id="attachment_225"  aligncenter"="" style="margin: 0px auto 1.5em; padding-top: 5px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; box-sizing: border-box; clear: both; text-align: center; max-width: 100%; height: auto; width: 310px;"><a href="http://www.robotium.cn/wp-content/uploads/2013/03/16.png" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img wp-image-225"="" title="点击查看大图" alt="16" src="http://www.robotium.cn/wp-content/uploads/2013/03/16-300x180.png" width="300" height="180" style="margin: 0.75em 0px 5px; padding: 0px; border: 0px none; outline: 0px; vertical-align: baseline; background-color: transparent; max-width: 100%; height: auto;" /></a><p style="margin: 0.6em 0px -0.2em; padding: 0px; border: 0px; outline: 0px; font-size: 0.8em; vertical-align: baseline; background-color: transparent; orphans: 4; font-style: italic; background-position: initial initial; background-repeat: initial initial;">点击查看大图</p></div></li><li style="margin: 0px 0px 0.75em; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent; list-style-type: decimal;">一个简单的测试就结束了。如果你没有遇到问题，那么恭喜你继续更深一步学习，如果你遇到问题了，不要急~请先确保你的操作过程没有出任何错误，其次我列出你可能遇到的错误，如果还是没有解决请<a href="http://www.google.com/" target="_blank" style="margin: 0px; padding: 0px; vertical-align: baseline; background-color: transparent; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Google</a>或者联系我们</li></ol><h2>1、最常见的：java.lang.NoClassDefFoundError: com.jayway.android.robotium.solo.Solo</h2><h2>解决方法：如图操作后再clean工程<a href="http://www.robotium.cn/wp-content/uploads/2013/03/34.png" style="margin: 0px; padding: 0px; font-size: 24px; vertical-align: baseline; color: #e01222; text-decoration: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><img size-medium=""  wp-image-229"="" alt="34" src="http://www.robotium.cn/wp-content/uploads/2013/03/34-300x224.png" width="300" height="224" style="margin: 0.75em auto 5px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; display: block; clear: both; max-width: 100%; height: auto;" /></a></h2><p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; orphans: 4; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif; line-height: 23px;">&nbsp;</p><p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; orphans: 4; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif; line-height: 23px;">&nbsp;</p><p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; orphans: 4; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif; line-height: 23px;">&nbsp;</p><p style="margin: 0px 0px 1.5em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; orphans: 4; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif; line-height: 23px;">&nbsp;</p><p style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: #ffffff; orphans: 4; color: #444444; font-family: Georgia, 'Times New Roman', Times, serif; line-height: 23px;">&nbsp;</p><img src ="http://www.blogjava.net/qileilove/aggbug/413704.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-05-15 21:15 <a href="http://www.blogjava.net/qileilove/articles/413704.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><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>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) (十四) -- 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>Maven实战（六）依赖</title><link>http://www.blogjava.net/qileilove/articles/411279.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Thu, 20 Mar 2014 06:15:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/411279.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/411279.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/411279.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/411279.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/411279.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 我们项目中用到的jar包可以通过依赖的方式引入，构建项目的时候从Maven仓库下载即可。&nbsp;1. 依赖配置&nbsp;&nbsp;&nbsp; 依赖可以声明如下：&nbsp;&nbsp;&nbsp;Xml代码&nbsp;&nbsp;&lt;project&gt;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependenci...&nbsp;&nbsp;<a href='http://www.blogjava.net/qileilove/articles/411279.html'>阅读全文</a><img src ="http://www.blogjava.net/qileilove/aggbug/411279.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:15 <a href="http://www.blogjava.net/qileilove/articles/411279.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>一个用于网站自动化测试的生态系统实现</title><link>http://www.blogjava.net/qileilove/articles/410974.html</link><dc:creator>顺其自然EVO</dc:creator><author>顺其自然EVO</author><pubDate>Thu, 13 Mar 2014 05:01:00 GMT</pubDate><guid>http://www.blogjava.net/qileilove/articles/410974.html</guid><wfw:comment>http://www.blogjava.net/qileilove/comments/410974.html</wfw:comment><comments>http://www.blogjava.net/qileilove/articles/410974.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qileilove/comments/commentRss/410974.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qileilove/services/trackbacks/410974.html</trackback:ping><description><![CDATA[<h2><a id="viewpost1_TitleUrl" href="http://www.blogjava.net/qileilove/archive/2014/03/13/410968.html" style="color: #0066aa; text-decoration: none;">一个用于网站自动化测试的生态系统实现</a></h2><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;">　　这是我在从事网站<a target="_self" style="word-break: break-all; color: #202859; line-height: normal !important;"><u style="word-break: break-all;"><strong style="word-break: break-all;">自动化测试</strong></u></a>的<a target="_self" style="word-break: break-all; color: #202859; line-height: normal !important;"><u style="word-break: break-all;"><strong style="word-break: break-all;">工作</strong></u></a>当中构建出的一个&#8220;生态系统&#8221;。&#8220;生态系统&#8221;这个概念是我从公司的前辈身上学到的，他一直以来都认为自动化测试人员不应仅仅局限于编写测试代码，还应该让整个自动化测试的过程（测试代码的持续集成、分发、执行等）都自动化，形成一个&#8220;系统&#8221;，这个系统的自动化程度越高，自动化测试人员就越省力。</div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;">　　<strong style="word-break: break-all; line-height: normal !important;">一、概念</strong></div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;">　　这里我画了一张示意图：</div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;"><a href="http://www.51testing.com/batch.download.php?aid=44896" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_201403101350321E3JG.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;">　　之所以称之为&#8220;生态系统&#8221;，是因为建成之后需要的人为干涉很少，其余的时间都是系统内部循环运作。作为自动化测试人员的你只需要提交代码，之后便可以在AutomationDashboard上看到运行的结果了，其余的事情都由系统内部消化。当然，结果的分析还是需要人来完成，机器还没有聪明到可以灵活分析出各种各样让case fail掉的原因。</div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;">　　我们可以把整个系统看作一个黑盒子，那么上面的图可以变成：</div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;"><a href="http://www.51testing.com/batch.download.php?aid=44897" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_201403101350322xbVu.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;">　　实际上这里画的人不仅限于自动化测试人员，也可以是：</div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;">　　（1）产品的管理者，比如产品经理需要从自动化回归测试知道这次release有无推迟风险；</div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;">　　（2）团队的管理者，比如开发经理、QA经理需要从自动化的daily/weekly regression知道最近的代码质量如何；</div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;">　　（3）开发人员，他们也许会想通过quick regression（提交的产品代码被部署到测试环境之后运行的测试）知道自己刚提交的代码有没有破坏系统的基本功能；</div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;">　　（4）其他帮忙做自动化测试的开发人员、刚刚开始<a target="_self" style="word-break: break-all; color: #202859; line-height: normal !important;"><u style="word-break: break-all;"><strong style="word-break: break-all;">学习</strong></u></a>编写自动化测试代码的手动测试人员，他们不必关心生态系统的内部实现。</div><div style="font-size: 11.818181991577148px; word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; color: #333333; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;"><div style="word-break: break-all; margin: 10px 0px; line-height: normal !important;">　<strong style="word-break: break-all;">　二、实现</strong></div><div style="word-break: break-all; margin: 10px 0px; line-height: normal !important;">　　说完概念，接下来该说说具体实现了。我这里讲的是我认为最适合我所测试的产品的实现，工具不止一种，方式不止一种。Jenkins可以用TeamCity或其它CI替换，git也可以是svn或tfs，AutomationDahsboard可以用.NET、SpringMVC、ROR等等实现，运行测试的slave可以是Windows/Linux/Mac（土豪！），总之选择一种最适合你所测试的产品的实现。还有一点就是自动化测试代码是用关键字驱动思想实现的，这是另外一个话题了，有时间另外写篇文。</div><div style="word-break: break-all; margin: 10px 0px; line-height: normal !important;">　　<strong style="word-break: break-all;">好，进入正题。依次说说系统的每个重要组成部分吧：</strong></div><div style="word-break: break-all; margin: 10px 0px; line-height: normal !important;">　　1、SCM（Source Code Management）。我选的是git，可以是git服务器（公司自己搭建了一个git&nbsp;<a target="_self" style="word-break: break-all; color: #202859;"><u style="word-break: break-all;"><strong style="word-break: break-all;">server</strong></u></a>），也可以是一个bare repo（http://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/） 。</div><div style="word-break: break-all; margin: 10px 0px; line-height: normal !important;">　　2、CI（continuous integration）。我选的是部署方便、插件丰富的Jenkins。</div><div style="word-break: break-all; margin: 10px 0px; line-height: normal !important;">　　<strong style="word-break: break-all;">它的职责是：</strong></div><div style="word-break: break-all; margin: 10px 0px; line-height: normal !important;">　　（1）从git上取出代码，build（.NET对应msbuild，如果是ruby则不用build了，直接部署即可）；</div><div style="word-break: break-all; margin: 10px 0px; line-height: normal !important;">　　（2）把build好的*.dll部署（这里即是拷贝）到所有的slave上；</div><div style="word-break: break-all; margin: 10px 0px; line-height: normal !important;">　　（3）启动或停止所有slave上的AutomationService（后面还会讲到AutomationService），从而控制测试的执行。<span style="font-size: 11.818181991577148px; line-height: 21.60000228881836px;">我在Jenkins的这些个job配置起来还是比较繁琐的，要细讲又可以另外写一篇文了。这里就特别提到两个很实用的插件吧：</span></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　（1）Parameterized Trigger Plugin（https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Trigger+Plugin）：可以在一个build step中触发其它project的build。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><a href="http://www.51testing.com/batch.download.php?aid=44898" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_201403101350323MpQL.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　它最有用的就是这个&#8220;Block until the triggered projects finish their builds&#8221;选项，勾上的话Jenkins就能在所有trigger的project完成build之后（而非仅仅trigger其它project的build，不等它们完成就继续下一个build step）再继续下一个build step，做到真正的依次执行每个build step。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><a href="http://www.51testing.com/batch.download.php?aid=44899" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_201403101350324cdMj.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　（2）NodeLabel Parameter Plugin（https://wiki.jenkins-ci.org/display/JENKINS/NodeLabel+Parameter+Plugin）：在所有&#8220;Possible nodes&#8221;标有指定标签（&#8220;Label&#8221;）的Jenkins节点（就是Jenkins master或Jenkins slave）上触发指定project（被触发的project是参数化的）。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　比如我有一个project叫&#8220;StartClassicROLATServiceOnAllNodes&#8221;，它有一个build step是这样设定的：</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><a href="http://www.51testing.com/batch.download.php?aid=44900" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_201403101350325p0dk.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　再来看看&#8220;StartClassicROLATServiceOnASingleNode&#8221;这个project的设定：</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><a href="http://www.51testing.com/batch.download.php?aid=44901" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_2014031013503261mEf.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　这个project有一个Node类型的参数，参数名&#8220;NodeX&#8221;与之前Label Factory中的&#8220;NodeX&#8221;对应，&#8220;Possible nodes&#8221;选的是&#8220;ALL&#8221;，那么列出的所有node（master、10.107.122.152、10.107.122.153、10.107.122.154）都在判断范围之内（判断其是否有&#8220;Node&#8221;标签，有则执行project）。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　另外，列出的所有node我都为其加了一个&#8220;Node&#8221;标签。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><a href="http://www.51testing.com/batch.download.php?aid=44902" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_201403101350327qC46.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　这样，当我trigger &#8220;StartClassicROLATServiceOnAllNodes&#8221;之后，就会在master、10.107.122.152、10.107.122.153、10.107.122.154这4个node上同时执行&#8220;StartClassicROLATServiceOnASingleNode&#8221;。<br /><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><strong style="word-break: break-all; line-height: normal !important;">　3、AutomationDashboard，这里姑且译作&#8220;自动化测试控制面板&#8221;吧。</strong>实际上它应该和Jenkins一起并称控制面板，不过因为Jenkins有API可以调用，所以想做的画两者也是可以统一成一个web界面的。这个dashboard完全是用.NET+IIS+SQLServer一点点从数据库设计构建、数据访问层、业务层、表现层做起来的，要细讲&#8230;&#8230;额&#8230;&#8230;又会是另外一篇文了（Oh man, not again!）。反正我觉得，虽然我是做自动化测试工作的，但不应该把自己局限于测试。为了更好地进行自动化测试，开发网站、安装配置虚拟机以及其它要用到的工具，都应该抽时间去学习、掌握。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　好，来说说这个dashboard。这里只讲两个主要组成部分，一个网站（以下简称dashboard）、一个Windows Service（以下简称ATService）和一个console application（以下称ConsoleRunner）：</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　<strong style="word-break: break-all; line-height: normal !important;">（1）dashboard，它的主要功能：</strong></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　a、展示测试的运行状况：有多少正在运行/执行完毕，分别在哪台slave上执行等等。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　b、通过call Jenkins的API来trigger Jenkins的job，间接控制测试的执行。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　c、展示测试的结果：发生错误的是哪个case、出错时间、错误信息、代码回溯（stack trace）、甚至可以包含一张出错时的截图。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　主要界面如下：</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　a、Summary，顾名思义是汇总信息，case有多少pass多少fail、case按分类每一类有多少等等。（其实这里我少做了一张很重要的图，就是coverage饼状图）</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><a href="http://www.51testing.com/batch.download.php?aid=44903" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_20140310135032827KJ.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　b、Queue，测试队列，包含当前正在运行的、运行完的、等待运行的test fixture或test case（依据测试工具的不同，NUnit、JUnit、RSpec等，fixture的叫法可能不同，总之就是包含多个test case的集合）。可以启动、停止、终止（终止之后可以清空）测试执行或清空当前队列。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><a href="http://www.51testing.com/batch.download.php?aid=44904" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_201403101350329deYC.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　c、TestCase，生态系统中的所有测试用例会展示在这里，可以看到它们最后一次执行的时间和状态（pass/fail），点击某条case可以跳转到该条case的所有test result。可以按状态（pass/fail/other）筛选用例，可以勾选部分用例重新执行、或重新执行所有fail的case。&#8220;Reload Test Cases&#8221;主要是考虑到*.dll文件中的test case可能会在某次部署之后发生变化，需要重新加载。不过后来我修改了Jenkins里的job在每次部署之后都自动重新加载，所以这个按钮其实没什么用了。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><a href="http://www.51testing.com/batch.download.php?aid=44905" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_201403101350321010y4.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　d、TestSuite，包含多个fixture的集合是一个suite。勾选多个suite点击&#8220;Run Suite&#8221;即可把这些suite中包含的fixture添加到Queue。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><a href="http://www.51testing.com/batch.download.php?aid=44906" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_20140310135032111vpi.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　这里的suite是对NUnit中的Category的一个补充，点击&#8220;New Suite&#8221;你可以任意选择fixture来组成自己想要的suite：</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><a href="http://www.51testing.com/batch.download.php?aid=44907" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_2014031013503212Z8mE.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　e、TestResult，展示所有test case的运行结果，可以按test case id进行筛选，点击TC#这一列的id就只显示这条case的结果。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><a href="http://www.51testing.com/batch.download.php?aid=44908" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all; line-height: normal !important;"><img src="http://www.51testing.com/attachments/2014/03/14982672_20140310135032132kv4.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;">　　点右边的蓝色&#8220;i&#8221;图标可以跳到这条结果的详细页面，截图功能暂未启用，根据RunnerMessage和RunnerStackTrace可以知道报错的代码位置，进而尝试重现问题。</div><div style="word-break: break-all; line-height: 21.60000228881836px; margin: 10px 0px; font-size: 11.818181991577148px;"><span style="line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;"><img src="http://www.51testing.com/attachments/2014/03/14982672_2014031013503214aCLS.jpg" border="0" alt="" style="word-break: break-all; list-style: none outside none; margin: 0px; padding: 0px; border: none; max-width: 500px;" /></a></span><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　（2）ATService。这个Windows Service（slave都是Windows，稍后会讲）被安装到了每个slave上，用以向dashboard询问&#8220;现在有没有分配给我的test fixture/case？&#8221;，如果有且当前slave空闲的话就抓过来运行，运行完毕汇报结果。</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　还记得Queue（队列）吧？无论你在TestCase还是TestSuite页面挑选了test case/suite想要运行，都只是把它们添加到队列当中（准确地说就是往Queue这张数据库表中INSERT记录），而不会给它们分配slave。只有当Jenkins启动了slave上的ATService之后，ATService才会去Queue表中自己抓取（就是打上标记说这条fixture/case已经有主了，其它slave就不会再去抓）还没有运行过且没有分配有slave的test fixture/case。</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　（3）ConsoleRunner，最开始的那个图中没有画出来。这个console程序主要供Jenkins调用。Jenkins不是可以让job定时运行么？正好，定时调用这个console application，传几个参数，就可以在指定时间往Queue里填充fixture/case，然后再启动ATService开始执行测试。这样就能实现quick/daily/weekly/full regression的无人值守运行了。</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　<strong style="word-break: break-all;">　4、Slave</strong></a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　我选择在Windows上运行测试：</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　（1）公司IT一般只提供Windows操作系统的虚拟机</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　（2）产品在Windows上的用户占绝大多数（其实这个有点废话，桌面操作系统Windows依然是世界王者。诚然，我自己业余时用Linux做开发，Mac在国内外也是相当流行的，但GoogleAnalytics显示的统计结果就是大部分访问都来自Windows。什么，你说iOS/Android？额&#8230;&#8230;移动端现在仍然是产品的短板&#8230;&#8230;）</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　如果选择Linux的话要注意下selenium webdriver的native event设定（http://code.google.com/p/selenium/wiki/AdvancedUserInteractions#Native_events_versus_synthetic_events）。</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　关于浏览器，Firefox、Chrome、IE皆可，webdriver的浏览器兼容性已经很不错了。浏览器兼容性是个有点头疼的问题，想支持很多浏览器的话有时会增加很多开发、测试成本，我一般在Firefox上跑就足够了。什么？数字？马桶？企鹅？您想多了，selenium官方不支持。</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　你能找到多少台slave来执行测试？多多益善哦！找不到那么多实体机就自己配虚拟机吧，分布式运行可以给你的自动化测试生态系统装上火箭！在更短的时间内运行完更多测试，从而更快地从测试中获得反馈！</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　嗯，差不多就这么多了。还有很多细节就留在之后的文中再说了。自我感觉这个生态系统还是有很多可以完善、增加的功能，而且这个实现方式、运作机制可能也并非适用于你所测试的产品，不过现在对于我测的产品来说是够用的了。</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　不管怎样实现，我想表达的核心观点是：</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　做自动化测试不要局限于自动化测试代码的编写，我们要自动化的不仅仅是manual test case，还应包括整个automation test的process！测试代码持续集成、部署（分发）、执行、结果展示，自动化的环节越多、越彻底，为你节约的时间就越多，你可以用这些节约的时间做更有意义的事情。人类发明计算机，用代码编写程序，其实就是一种自动化的过程。以前要靠手工劳动完成的现在都交给电脑做了&#8212;&#8212;服务器不正是勤勤恳恳地重复执行着我们写好的程序么？构建自动化测试生态系统是同样的道理，因为机器能比人更可靠地完成重复劳动。</a></div><div style="word-break: break-all; margin: 10px 0px; font-size: 11.818181991577148px; line-height: normal !important;"><a href="http://www.51testing.com/batch.download.php?aid=44909" target="_blank" style="color: #202859; text-decoration: none; word-break: break-all;">　　如果你还在手动拷贝*.dll，还需要打开NUnit手动执行测试，还在1台机器上运行测试，那么，现在就是该提高生产力的时候了！</a></div><div></div></div></div></div><img src ="http://www.blogjava.net/qileilove/aggbug/410974.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:01 <a href="http://www.blogjava.net/qileilove/articles/410974.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></channel></rss>