<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Selenium Python 综合教程 on 重定向学院</title><link>https://itest2025.pages.dev/categories/selenium-python-%E7%BB%BC%E5%90%88%E6%95%99%E7%A8%8B/</link><description>Recent content in Selenium Python 综合教程 on 重定向学院</description><generator>Hugo</generator><language>zh-CN</language><lastBuildDate>Tue, 17 Jun 2025 17:29:15 +0800</lastBuildDate><atom:link href="https://itest2025.pages.dev/categories/selenium-python-%E7%BB%BC%E5%90%88%E6%95%99%E7%A8%8B/index.xml" rel="self" type="application/rss+xml"/><item><title>（一）安装selenium</title><link>https://itest2025.pages.dev/tutorials/24-001-%E4%B8%80%E5%AE%89%E8%A3%85selenium/</link><pubDate>Fri, 30 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-001-%E4%B8%80%E5%AE%89%E8%A3%85selenium/</guid><description>&lt;br>
### 安装python
&lt;p>打开 &lt;a href="https://www.python.org/" target="_blank" rel="noopener noreffer ">Python官网&lt;/a>，找到“Download”, 在其下拉菜单中选择自己的平台（Windows/Mac）,一般的Linux平台已经自带的Python，所以不需要安装，通过打开“终端” ，输入“python”命令来验证。&lt;/p>
&lt;p>如果你是第一次接触Python，一定会迷惑Python为什么会提供Python2.x 和 Python3.x两个版本？那么，直接使用Python3.x的最新版本就好了。因为Python2.x预计到2020年不在维护。&lt;/p>
&lt;p>如果你是Windows平台用户，会遇到一个版本为什么会提供多种个下载链接。例如：&lt;/p>
&lt;ul>
&lt;li>Python 3.6.1 - 2017-03-21
&lt;ul>
&lt;li>Download Windows x86 web-based installer&lt;/li>
&lt;li>Download Windows x86 executable installer&lt;/li>
&lt;li>Download Windows x86 embeddable zip file&lt;/li>
&lt;li>Download Windows x86-64 web-based installer&lt;/li>
&lt;li>Download Windows x86-64 executable installer&lt;/li>
&lt;li>Download Windows x86-64 embeddable zip file&lt;/li>
&lt;li>Download Windows help file&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>x86 只支持32位的系统； x86-64 支持64位的系统。 web-based 在安装的过程中需要联网；executable 可执行文件(.exe)方式安装；embeddable zip file 嵌入式版本，可以集成到其它应用中。&lt;/p>
&lt;p>注意：在安装的过程中需要勾选：“Add Python 3.x to PATH” , 如果没有勾选，需要在安装完成之后，将Python的安装目录（如：C:\Python36）添加到环境变量PATH下面。&lt;/p>
&lt;p>打开Windows命令提示符（cmd）/ Linux终端输入：&lt;/p></description></item><item><title>（二）selenium3 浏览器驱动</title><link>https://itest2025.pages.dev/tutorials/24-002-%E4%BA%8Cselenium3-%E6%B5%8F%E8%A7%88%E5%99%A8%E9%A9%B1%E5%8A%A8/</link><pubDate>Thu, 29 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-002-%E4%BA%8Cselenium3-%E6%B5%8F%E8%A7%88%E5%99%A8%E9%A9%B1%E5%8A%A8/</guid><description>&lt;br>
### 下载浏览器驱动
&lt;p>当selenium升级到3.0之后，对不同的浏览器驱动进行了规范。如果想使用selenium驱动不同的浏览器，必须单独下载并设置不同的浏览器驱动。&lt;/p>
&lt;p>各浏览器下载地址：&lt;/p>
&lt;p>Firefox浏览器驱动：&lt;a href="https://github.com/mozilla/geckodriver/releases" target="_blank" rel="noopener noreffer ">geckodriver&lt;/a>&lt;/p>
&lt;p>Chrome浏览器驱动：&lt;a href="https://sites.google.com/a/chromium.org/chromedriver/home" target="_blank" rel="noopener noreffer ">chromedriver&lt;/a> , &lt;a href="https://npm.taobao.org/mirrors/chromedriver" target="_blank" rel="noopener noreffer ">taobao备用地址&lt;/a>&lt;/p>
&lt;p>IE浏览器驱动：&lt;a href="http://selenium-release.storage.googleapis.com/index.html" target="_blank" rel="noopener noreffer ">IEDriverServer&lt;/a>&lt;/p>
&lt;p>Edge浏览器驱动：&lt;a href="https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver" target="_blank" rel="noopener noreffer ">MicrosoftWebDriver&lt;/a>&lt;/p>
&lt;p>Opera浏览器驱动：&lt;a href="https://github.com/operasoftware/operachromiumdriver/releases" target="_blank" rel="noopener noreffer ">operadriver&lt;/a>&lt;/p>
&lt;p>PhantomJS浏览器驱动：&lt;a href="http://phantomjs.org" target="_blank" rel="noopener noreffer ">phantomjs&lt;/a>&lt;/p>
&lt;p>注：部分浏览器驱动地址需要科学上网。&lt;/p>
&lt;br>
### 设置浏览器驱动
&lt;p>设置浏览器的地址非常简单。
我们可以手动创建一个存放浏览器驱动的目录，如： C:\driver , 将下载的浏览器驱动文件（例如：chromedriver、geckodriver）丢到该目录下。&lt;/p>
&lt;p>我的电脑&amp;ndash;&amp;gt;属性&amp;ndash;&amp;gt;系统设置&amp;ndash;&amp;gt;高级&amp;ndash;&amp;gt;环境变量&amp;ndash;&amp;gt;系统变量&amp;ndash;&amp;gt;Path，将“C:\driver”目录添加到Path的值中。&lt;/p>
&lt;ul>
&lt;li>Path&lt;/li>
&lt;li>;C:\driver&lt;/li>
&lt;/ul>
&lt;br>
### 设置浏览器驱动
&lt;p>验证不同的浏览器驱动是否正常使用。&lt;/p>
&lt;pre tabindex="0">&lt;code>from selenium import webdriver


driver = webdriver.Firefox() # Firefox浏览器

driver = webdriver.Chrome() # Chrome浏览器

driver = webdriver.Ie() # Internet Explorer浏览器

driver = webdriver.Edge() # Edge浏览器

driver = webdriver.Opera() # Opera浏览器

driver = webdriver.PhantomJS() # PhantomJS

……
&lt;/code>&lt;/pre>&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/34/BA1yLjNnQCI1yisIZGEi_2013-07-16_1922_IMG_9873.jpg?w=300"
 data-srcset="https://images.unsplash.com/34/BA1yLjNnQCI1yisIZGEi_2013-07-16_1922_IMG_9873.jpg?w=300, https://images.unsplash.com/34/BA1yLjNnQCI1yisIZGEi_2013-07-16_1922_IMG_9873.jpg?w=300 1.5x, https://images.unsplash.com/34/BA1yLjNnQCI1yisIZGEi_2013-07-16_1922_IMG_9873.jpg?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/34/BA1yLjNnQCI1yisIZGEi_2013-07-16_1922_IMG_9873.jpg?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>（三）selenium元素定位</title><link>https://itest2025.pages.dev/tutorials/24-003-%E4%B8%89selenium%E5%85%83%E7%B4%A0%E5%AE%9A%E4%BD%8D/</link><pubDate>Wed, 28 Jun 2017 07:37:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-003-%E4%B8%89selenium%E5%85%83%E7%B4%A0%E5%AE%9A%E4%BD%8D/</guid><description>&lt;br>
### selenium定位方法
&lt;p>Selenium提供了8种定位方式。&lt;/p>
&lt;ul>
&lt;li>id&lt;/li>
&lt;li>name&lt;/li>
&lt;li>class name&lt;/li>
&lt;li>tag name&lt;/li>
&lt;li>link text&lt;/li>
&lt;li>partial link text&lt;/li>
&lt;li>xpath&lt;/li>
&lt;li>css selector&lt;/li>
&lt;/ul>
&lt;p>这8种定位方式在Python selenium中所对应的方法为：&lt;/p>
&lt;ul>
&lt;li>find_element_by_id()&lt;/li>
&lt;li>find_element_by_name()&lt;/li>
&lt;li>find_element_by_class_name()&lt;/li>
&lt;li>find_element_by_tag_name()&lt;/li>
&lt;li>find_element_by_link_text()&lt;/li>
&lt;li>find_element_by_partial_link_text()&lt;/li>
&lt;li>find_element_by_xpath()&lt;/li>
&lt;li>find_element_by_css_selector()&lt;/li>
&lt;/ul>
&lt;br>
### 定位方法的用法
&lt;p>假如我们有一个Web页面，通过前端工具（如，Firebug）查看到一个元素的属性是这样的。&lt;/p>
&lt;pre tabindex="0">&lt;code>&amp;lt;html&amp;gt;
 &amp;lt;head&amp;gt;
 &amp;lt;body link=&amp;#34;#0000cc&amp;#34;&amp;gt;
 &amp;lt;a id=&amp;#34;result_logo&amp;#34; href=&amp;#34;/&amp;#34; onmousedown=&amp;#34;return c({&amp;#39;fm&amp;#39;:&amp;#39;tab&amp;#39;,&amp;#39;tab&amp;#39;:&amp;#39;logo&amp;#39;})&amp;#34;&amp;gt;
 &amp;lt;form id=&amp;#34;form&amp;#34; class=&amp;#34;fm&amp;#34; name=&amp;#34;f&amp;#34; action=&amp;#34;/s&amp;#34;&amp;gt;
 &amp;lt;span class=&amp;#34;soutu-btn&amp;#34;&amp;gt;&amp;lt;/span&amp;gt;
 &amp;lt;input id=&amp;#34;kw&amp;#34; class=&amp;#34;s_ipt&amp;#34; name=&amp;#34;wd&amp;#34; value=&amp;#34;&amp;#34; maxlength=&amp;#34;255&amp;#34; autocomplete=&amp;#34;off&amp;#34;&amp;gt;
&lt;/code>&lt;/pre>&lt;p>我们的目的是要定位input标签的输入框。&lt;/p>
&lt;ul>
&lt;li>通过id定位:&lt;/li>
&lt;/ul>
&lt;pre tabindex="0">&lt;code>dr.find_element_by_id(&amp;#34;kw&amp;#34;)
&lt;/code>&lt;/pre>&lt;ul>
&lt;li>通过name定位:&lt;/li>
&lt;/ul>
&lt;pre tabindex="0">&lt;code>dr.find_element_by_name(&amp;#34;wd&amp;#34;)
&lt;/code>&lt;/pre>&lt;ul>
&lt;li>通过class name定位:&lt;/li>
&lt;/ul>
&lt;pre tabindex="0">&lt;code>dr.find_element_by_class_name(&amp;#34;s_ipt&amp;#34;)
&lt;/code>&lt;/pre>&lt;ul>
&lt;li>通过tag name定位:&lt;/li>
&lt;/ul>
&lt;pre tabindex="0">&lt;code>dr.find_element_by_tag_name(&amp;#34;input&amp;#34;)
&lt;/code>&lt;/pre>&lt;ul>
&lt;li>通过xpath定位，xpath定位有N种写法，这里列几个常用写法:&lt;/li>
&lt;/ul>
&lt;pre tabindex="0">&lt;code>dr.find_element_by_xpath(&amp;#34;//*[@id=&amp;#39;kw&amp;#39;]&amp;#34;)
dr.find_element_by_xpath(&amp;#34;//*[@name=&amp;#39;wd&amp;#39;]&amp;#34;)
dr.find_element_by_xpath(&amp;#34;//input[@class=&amp;#39;s_ipt&amp;#39;]&amp;#34;)
dr.find_element_by_xpath(&amp;#34;/html/body/form/span/input&amp;#34;)
dr.find_element_by_xpath(&amp;#34;//span[@class=&amp;#39;soutu-btn&amp;#39;]/input&amp;#34;)
dr.find_element_by_xpath(&amp;#34;//form[@id=&amp;#39;form&amp;#39;]/span/input&amp;#34;)
dr.find_element_by_xpath(&amp;#34;//input[@id=&amp;#39;kw&amp;#39; and @name=&amp;#39;wd&amp;#39;]&amp;#34;)
&lt;/code>&lt;/pre>&lt;ul>
&lt;li>通过css定位，css定位有N种写法，这里列几个常用写法:&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="n">dr&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_css_selector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;#kw&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">dr&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_css_selector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;[name=wd]&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">dr&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_css_selector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;.s_ipt&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">dr&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_css_selector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;html &amp;gt; body &amp;gt; form &amp;gt; span &amp;gt; input&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">dr&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_css_selector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;span.soutu-btn&amp;gt; input#kw&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">dr&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_css_selector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;form#form &amp;gt; span &amp;gt; input&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>接下来，我们的页面上有一组文本链接。&lt;/p></description></item><item><title>（四）控制浏览器操作</title><link>https://itest2025.pages.dev/tutorials/24-004-%E5%9B%9B%E6%8E%A7%E5%88%B6%E6%B5%8F%E8%A7%88%E5%99%A8%E6%93%8D%E4%BD%9C/</link><pubDate>Tue, 27 Jun 2017 07:37:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-004-%E5%9B%9B%E6%8E%A7%E5%88%B6%E6%B5%8F%E8%A7%88%E5%99%A8%E6%93%8D%E4%BD%9C/</guid><description>&lt;br>
### 控制浏览器窗口大小
&lt;p>有时候我们希望能以某种浏览器尺寸打开，让访问的页面在这种尺寸下运行。例如可以将浏览器设置成移动端大小(480* 800)，然后访问移动站点，对其样式进行评估；WebDriver提供了set_window_size()方法来设置浏览器的大小。&lt;/p>
&lt;pre tabindex="0">&lt;code>from selenium import webdriver

driver = webdriver.Firefox()
driver.get(&amp;#34;http://m.baidu.com&amp;#34;)

# 参数数字为像素点
print(&amp;#34;设置浏览器宽480、高800显示&amp;#34;)
driver.set_window_size(480, 800)
driver.quit()
&lt;/code>&lt;/pre>&lt;p>在PC端执行自动化测试脚本大多的情况下是希望浏览器在全屏幕模式下执行，那么可以使用maximize_window()方法使打开的浏览器全屏显示，其用法与set_window_size() 相同，但它不需要参数。&lt;/p>
&lt;br>
### 控制浏览器后退、前进
&lt;p>在使用浏览器浏览网页时，浏览器提供了后退和前进按钮，可以方便地在浏览过的网页之间切换，WebDriver也提供了对应的back()和forward()方法来模拟后退和前进按钮。下面通过例子来演示这两个方法的使用。&lt;/p>
&lt;pre tabindex="0">&lt;code>from selenium import webdriver

driver = webdriver.Firefox()

#访问百度首页
first_url= &amp;#39;http://www.baidu.com&amp;#39;
print(&amp;#34;now access %s&amp;#34; %(first_url))
driver.get(first_url)

#访问新闻页面
second_url=&amp;#39;http://news.baidu.com&amp;#39;
print(&amp;#34;now access %s&amp;#34; %(second_url))
driver.get(second_url)

#返回（后退）到百度首页
print(&amp;#34;back to %s &amp;#34;%(first_url))
driver.back()

#前进到新闻页
print(&amp;#34;forward to %s&amp;#34;%(second_url))
driver.forward()

driver.quit()
&lt;/code>&lt;/pre>&lt;p>为了看清脚本的执行过程，下面每操作一步都通过print()来打印当前的URL地址。&lt;/p>
&lt;br>
### 刷新页面
&lt;p>有时候需要手动刷新（F5） 页面。&lt;/p>
&lt;pre tabindex="0">&lt;code>……
driver.refresh() #刷新当前页面
……
&lt;/code>&lt;/pre>&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/photo-1431887773042-803ed52bed26?w=300"
 data-srcset="https://images.unsplash.com/photo-1431887773042-803ed52bed26?w=300, https://images.unsplash.com/photo-1431887773042-803ed52bed26?w=300 1.5x, https://images.unsplash.com/photo-1431887773042-803ed52bed26?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/photo-1431887773042-803ed52bed26?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>（五）WebDriver常用方法</title><link>https://itest2025.pages.dev/tutorials/24-005-%E4%BA%94webdriver%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95/</link><pubDate>Mon, 26 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-005-%E4%BA%94webdriver%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95/</guid><description>&lt;br>
### 点击和输入
前面我们已经学习了定位元素， 定位只是第一步， 定位之后需要对这个元素进行操作， 或单击（按钮）
或输入（输入框） ， 下面就来认识 WebDriver 中最常用的几个方法：
&lt;ul>
&lt;li>
&lt;p>clear()： 清除文本。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>send_keys (value)： 模拟按键输入。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>click()： 单击元素。&lt;/p>
&lt;/li>
&lt;/ul>
&lt;pre tabindex="0">&lt;code>from selenium import webdriver

driver = webdriver.Chrome()
driver.get(&amp;#34;https://www.baidu.com&amp;#34;)

driver.find_element_by_id(&amp;#34;kw&amp;#34;).clear()
driver.find_element_by_id(&amp;#34;kw&amp;#34;).send_keys(&amp;#34;selenium&amp;#34;)
driver.find_element_by_id(&amp;#34;su&amp;#34;).click()

driver.quit()
&lt;/code>&lt;/pre>&lt;br>
### 提交
&lt;ul>
&lt;li>submit()&lt;/li>
&lt;/ul>
&lt;p>submit()方法用于提交表单。 例如， 在搜索框输入关键字之后的“回车” 操作， 就可以通过该方法模拟。&lt;/p>
&lt;pre tabindex="0">&lt;code>from selenium import webdriver

driver = webdriver.Chrome()
driver.get(&amp;#34;https://www.baidu.com&amp;#34;)

search_text = driver.find_element_by_id(&amp;#39;kw&amp;#39;)
search_text.send_keys(&amp;#39;selenium&amp;#39;)
search_text.submit()

driver.quit()
&lt;/code>&lt;/pre>&lt;p>有时候 submit()可以与 click()方法互换来使用， submit()同样可以提交一个按钮， 但 submit()的应用范围远不及 click()广泛。&lt;/p>
&lt;br>
### 其他常用方法
&lt;ul>
&lt;li>
&lt;p>size： 返回元素的尺寸。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>text： 获取元素的文本。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>get_attribute(name)： 获得属性值。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>is_displayed()： 设置该元素是否用户可见。&lt;/p>
&lt;/li>
&lt;/ul>
&lt;pre tabindex="0">&lt;code>from selenium import webdriver

driver = webdriver.Chrome()
driver.get(&amp;#34;http://www.baidu.com&amp;#34;)

# 获得输入框的尺寸
size = driver.find_element_by_id(&amp;#39;kw&amp;#39;).size
print(size)

# 返回百度页面底部备案信息
text = driver.find_element_by_id(&amp;#34;cp&amp;#34;).text
print(text)

# 返回元素的属性值， 可以是 id、 name、 type 或其他任意属性
attribute = driver.find_element_by_id(&amp;#34;kw&amp;#34;).get_attribute(&amp;#39;type&amp;#39;)
print(attribute)

# 返回元素的结果是否可见， 返回结果为 True 或 False
result = driver.find_element_by_id(&amp;#34;kw&amp;#34;).is_displayed()
print(result)

driver.quit()
&lt;/code>&lt;/pre>&lt;p>输出结果：&lt;/p></description></item><item><title>（六）鼠标事件</title><link>https://itest2025.pages.dev/tutorials/24-006-%E5%85%AD%E9%BC%A0%E6%A0%87%E4%BA%8B%E4%BB%B6/</link><pubDate>Sun, 25 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-006-%E5%85%AD%E9%BC%A0%E6%A0%87%E4%BA%8B%E4%BB%B6/</guid><description>&lt;br>
&lt;p>在 WebDriver 中， 将这些关于鼠标操作的方法封装在 ActionChains 类提供。&lt;/p>
&lt;p>ActionChains 类提供了鼠标操作的常用方法：&lt;/p>
&lt;ul>
&lt;li>
&lt;p>perform()： 执行所有 ActionChains 中存储的行为；&lt;/p>
&lt;/li>
&lt;li>
&lt;p>context_click()： 右击；&lt;/p>
&lt;/li>
&lt;li>
&lt;p>double_click()： 双击；&lt;/p>
&lt;/li>
&lt;li>
&lt;p>drag_and_drop()： 拖动；&lt;/p>
&lt;/li>
&lt;li>
&lt;p>move_to_element()： 鼠标悬停。&lt;/p>
&lt;/li>
&lt;/ul>
&lt;br>
### 鼠标悬停操作
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="http://orru5lls3.bkt.clouddn.com/xuanting.jpg"
 data-srcset="http://orru5lls3.bkt.clouddn.com/xuanting.jpg, http://orru5lls3.bkt.clouddn.com/xuanting.jpg 1.5x, http://orru5lls3.bkt.clouddn.com/xuanting.jpg 2x"
 data-sizes="auto"
 alt="http://orru5lls3.bkt.clouddn.com/xuanting.jpg"
 title="http://orru5lls3.bkt.clouddn.com/xuanting.jpg" />&lt;/p>
&lt;pre tabindex="0">&lt;code>from selenium import webdriver
# 引入 ActionChains 类
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get(&amp;#34;https://www.baidu.cn&amp;#34;)

# 定位到要悬停的元素
above = driver.find_element_by_link_text(&amp;#34;设置&amp;#34;)
# 对定位到的元素执行鼠标悬停操作
ActionChains(driver).move_to_element(above).perform()

……
&lt;/code>&lt;/pre>&lt;ul>
&lt;li>from selenium.webdriver import ActionChains&lt;/li>
&lt;/ul>
&lt;p>导入提供鼠标操作的 ActionChains 类。&lt;/p>
&lt;ul>
&lt;li>ActionChains(driver)&lt;/li>
&lt;/ul>
&lt;p>调用 ActionChains()类， 将浏览器驱动 driver 作为参数传入。&lt;/p>
&lt;ul>
&lt;li>move_to_element(above)&lt;/li>
&lt;/ul>
&lt;p>context_click()方法用于模拟鼠标右键操作， 在调用时需要指定元素定位。&lt;/p></description></item><item><title>（七）键盘事件</title><link>https://itest2025.pages.dev/tutorials/24-007-%E4%B8%83%E9%94%AE%E7%9B%98%E4%BA%8B%E4%BB%B6/</link><pubDate>Sat, 24 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-007-%E4%B8%83%E9%94%AE%E7%9B%98%E4%BA%8B%E4%BB%B6/</guid><description>&lt;br>
Keys()类提供了键盘上几乎所有按键的方法。 前面了解到， send_keys()方法可以用来模拟键盘输入， 除此
之外， 我们还可以用它来输入键盘上的按键， 甚至是组合键， 如 Ctrl+A、 Ctrl+C 等。
&lt;pre tabindex="0">&lt;code>from selenium import webdriver
# 引入 Keys 模块
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get(&amp;#34;http://www.baidu.com&amp;#34;)

# 输入框输入内容
driver.find_element_by_id(&amp;#34;kw&amp;#34;).send_keys(&amp;#34;seleniumm&amp;#34;)

# 删除多输入的一个 m
driver.find_element_by_id(&amp;#34;kw&amp;#34;).send_keys(Keys.BACK_SPACE)


# 输入空格键+“教程”
driver.find_element_by_id(&amp;#34;kw&amp;#34;).send_keys(Keys.SPACE)
driver.find_element_by_id(&amp;#34;kw&amp;#34;).send_keys(&amp;#34;教程&amp;#34;)

# ctrl+a 全选输入框内容
driver.find_element_by_id(&amp;#34;kw&amp;#34;).send_keys(Keys.CONTROL, &amp;#39;a&amp;#39;)

# ctrl+x 剪切输入框内容
driver.find_element_by_id(&amp;#34;kw&amp;#34;).send_keys(Keys.CONTROL, &amp;#39;x&amp;#39;)

# ctrl+v 粘贴内容到输入框
driver.find_element_by_id(&amp;#34;kw&amp;#34;).send_keys(Keys.CONTROL, &amp;#39;v&amp;#39;)

# 通过回车键来代替单击操作
driver.find_element_by_id(&amp;#34;su&amp;#34;).send_keys(Keys.ENTER)
driver.quit()
&lt;/code>&lt;/pre>&lt;p>需要说明的是， 上面的脚本没有什么实际意义， 仅向我们展示模拟键盘各种按键与组合键的用法。&lt;/p>
&lt;ul>
&lt;li>from selenium.webdriver.common.keys import Keys&lt;/li>
&lt;/ul>
&lt;p>在使用键盘按键方法前需要先导入 keys 类。&lt;/p>
&lt;br>
&lt;p>以下为常用的键盘操作：&lt;/p>
&lt;ul>
&lt;li>
&lt;p>send_keys(Keys.BACK_SPACE) 删除键（BackSpace）&lt;/p>
&lt;/li>
&lt;li>
&lt;p>send_keys(Keys.SPACE) 空格键(Space)&lt;/p></description></item><item><title>（八）获取断言信息</title><link>https://itest2025.pages.dev/tutorials/24-008-%E5%85%AB%E8%8E%B7%E5%8F%96%E6%96%AD%E8%A8%80%E4%BF%A1%E6%81%AF/</link><pubDate>Fri, 23 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-008-%E5%85%AB%E8%8E%B7%E5%8F%96%E6%96%AD%E8%A8%80%E4%BF%A1%E6%81%AF/</guid><description>&lt;br>
不管是在做功能测试还是自动化测试，最后一步需要拿实际结果与预期进行比较。这个比较的称之为__断言__。
&lt;p>我们通常可以通过获取title 、URL和text等信息进行断言。text方法在前面已经讲过，它用于获取标签对之间的文本信息。
下面同样以百度为例，介绍如何获取这些信息。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">webdriver&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">time&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">sleep&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Firefox&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;https://www.baidu.com&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;Before search================&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 打印当前页面title&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">title&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">title&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">title&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 打印当前页面URL&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">now_url&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">current_url&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">now_url&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_id&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;kw&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">send_keys&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;selenium&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_id&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;su&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">sleep&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">1&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;After search================&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 再次打印当前页面title&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">title&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">title&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">title&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 打印当前页面URL&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">now_url&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">current_url&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">now_url&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 获取结果数目&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">user&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_class_name&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;nums&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">text&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">user&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">quit&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>脚本运行结果如下：&lt;/p>
&lt;pre tabindex="0">&lt;code>Before search================
百度一下，你就知道
https://www.baidu.com/
After search================
selenium_百度搜索
https://www.baidu.com/s?ie=utf-8&amp;amp;f=8&amp;amp;rsv_bp=0&amp;amp;rsv_idx...
搜索工具
百度为您找到相关结果约5,380,000个
&lt;/code>&lt;/pre>&lt;ul>
&lt;li>title：用于获得当前页面的标题。&lt;/li>
&lt;li>current_url：用户获得当前页面的URL。&lt;/li>
&lt;li>text：获取搜索条目的文本信息。&lt;/li>
&lt;/ul>
&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/photo-1485470733090-0aae1788d5af?w=300"
 data-srcset="https://images.unsplash.com/photo-1485470733090-0aae1788d5af?w=300, https://images.unsplash.com/photo-1485470733090-0aae1788d5af?w=300 1.5x, https://images.unsplash.com/photo-1485470733090-0aae1788d5af?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/photo-1485470733090-0aae1788d5af?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>（九）设置元素等待</title><link>https://itest2025.pages.dev/tutorials/24-009-%E4%B9%9D%E8%AE%BE%E7%BD%AE%E5%85%83%E7%B4%A0%E7%AD%89%E5%BE%85/</link><pubDate>Thu, 22 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-009-%E4%B9%9D%E8%AE%BE%E7%BD%AE%E5%85%83%E7%B4%A0%E7%AD%89%E5%BE%85/</guid><description>&lt;br>
WebDriver提供了两种类型的等待：__显式等待__和__隐式等待__。
&lt;h3 id="显式等待">显式等待&lt;/h3>
&lt;p>显式等待使WebdDriver等待某个条件成立时继续执行，否则在达到最大时长时抛出超时异常（TimeoutException）。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">webdriver&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium.webdriver.common.by&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">By&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium.webdriver.support.ui&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">WebDriverWait&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium.webdriver.support&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">expected_conditions&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="n">EC&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Firefox&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;http://www.baidu.com&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">element&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">WebDriverWait&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">driver&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">5&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mf">0.5&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">until&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">EC&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">presence_of_element_located&lt;/span>&lt;span class="p">((&lt;/span>&lt;span class="n">By&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">ID&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;kw&amp;#34;&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">element&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">send_keys&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;selenium&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">quit&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>WebDriverWait类是由WebDirver 提供的等待方法。在设置时间内，默认每隔一段时间检测一次当前页面元素是否存在，如果超过设置时间检测不到则抛出异常。具体格式如下：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="n">WebDriverWait&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">driver&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">timeout&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">poll_frequency&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">0.5&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">ignored_exceptions&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="kc">None&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>driver ：浏览器驱动。&lt;/li>
&lt;li>timeout ：最长超时时间，默认以秒为单位。&lt;/li>
&lt;li>poll_frequency ：检测的间隔（步长）时间，默认为0.5S。&lt;/li>
&lt;li>ignored_exceptions ：超时后的异常信息，默认情况下抛NoSuchElementException异常。&lt;/li>
&lt;/ul>
&lt;p>WebDriverWait()一般由until()或until_not()方法配合使用，下面是until()和until_not()方法的说明。&lt;/p>
&lt;ul>
&lt;li>until(method, message=‘’)
调用该方法提供的驱动程序作为一个参数，直到返回值为True。&lt;/li>
&lt;li>until_not(method, message=‘’)
调用该方法提供的驱动程序作为一个参数，直到返回值为False。&lt;/li>
&lt;/ul>
&lt;p>在本例中，通过as关键字将expected_conditions 重命名为EC，并调用presence_of_element_located()方法判断元素是否存在。&lt;/p>
&lt;h3 id="隐式等待">隐式等待&lt;/h3>
&lt;p>WebDriver提供了implicitly_wait()方法来实现隐式等待，默认设置为0。它的用法相对来说要简单得多。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">webdriver&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium.common.exceptions&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">NoSuchElementException&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">time&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">ctime&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Firefox&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 设置隐式等待为10秒&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">implicitly_wait&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">10&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;http://www.baidu.com&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">try&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctime&lt;/span>&lt;span class="p">())&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_id&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;kw22&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">send_keys&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;selenium&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">except&lt;/span> &lt;span class="n">NoSuchElementException&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="n">e&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">e&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">finally&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctime&lt;/span>&lt;span class="p">())&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">quit&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>implicitly_wait()默认参数的单位为秒，本例中设置等待时长为10秒。首先这10秒并非一个固定的等待时间，它并不影响脚本的执行速度。其次，它并不针对页面上的某一元素进行等待。当脚本执行到某个元素定位时，如果元素可以定位，则继续执行；如果元素定位不到，则它将以轮询的方式不断地判断元素是否被定位到。假设在第6秒定位到了元素则继续执行，若直到超出设置时长（10秒）还没有定位到元素，则抛出异常。&lt;/p>
&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/photo-1459695452562-46cc57bef5f6?w=300"
 data-srcset="https://images.unsplash.com/photo-1459695452562-46cc57bef5f6?w=300, https://images.unsplash.com/photo-1459695452562-46cc57bef5f6?w=300 1.5x, https://images.unsplash.com/photo-1459695452562-46cc57bef5f6?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/photo-1459695452562-46cc57bef5f6?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>（十）定位一组元素</title><link>https://itest2025.pages.dev/tutorials/24-010-%E5%8D%81%E5%AE%9A%E4%BD%8D%E4%B8%80%E7%BB%84%E5%85%83%E7%B4%A0/</link><pubDate>Wed, 21 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-010-%E5%8D%81%E5%AE%9A%E4%BD%8D%E4%B8%80%E7%BB%84%E5%85%83%E7%B4%A0/</guid><description>&lt;br>
WebDriver还提供了8种用于定位一组元素的方法。
```python
find_elements_by_id()
find_elements_by_name()
find_elements_by_class_name()
find_elements_by_tag_name()
find_elements_by_link_text()
find_elements_by_partial_link_text()
find_elements_by_xpath()
find_elements_by_css_selector()
```
定位一组元素的方法与定位单个元素的方法类似，唯一的区别是在单词element后面多了一个s表示复数。
&lt;p>接下来通过例子演示定位一组元素的使用：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">webdriver&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">time&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">sleep&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Chrome&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;https://www.baidu.com&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_id&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;kw&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">send_keys&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;selenium&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_id&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;su&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">sleep&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">1&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 定位一组元素&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">texts&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_elements_by_xpath&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;//div/h3/a&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 循环遍历出每一条搜索结果的标题&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">for&lt;/span> &lt;span class="n">t&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="n">texts&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">t&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">text&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">quit&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>程序运行结果：&lt;/p>
&lt;pre tabindex="0">&lt;code>Selenium - Web Browser Automation
官网
功能自动化测试工具——Selenium篇
selenium + python自动化测试环境搭建 - 虫师 - 博客园
selenium是什么?_百度知道
怎样开始用selenium进行自动化测试(个人总结)_百度经验
Selenium_百度百科
selenium_百度翻译
Selenium官网教程_selenium自动化测试实践_Selenium_领测软件测试网
Selenium(浏览器自动化测试框架)_百度百科
自动化基础普及之selenium是啥? - 虫师 - 博客园
python十大主流开源框架 「菜鸟必看」
&lt;/code>&lt;/pre>&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/photo-1523712999610-f77fbcfc3843?w=300"
 data-srcset="https://images.unsplash.com/photo-1523712999610-f77fbcfc3843?w=300, https://images.unsplash.com/photo-1523712999610-f77fbcfc3843?w=300 1.5x, https://images.unsplash.com/photo-1523712999610-f77fbcfc3843?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/photo-1523712999610-f77fbcfc3843?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>（十一）多表单切换</title><link>https://itest2025.pages.dev/tutorials/24-011-%E5%8D%81%E4%B8%80%E5%A4%9A%E8%A1%A8%E5%8D%95%E5%88%87%E6%8D%A2/</link><pubDate>Tue, 20 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-011-%E5%8D%81%E4%B8%80%E5%A4%9A%E8%A1%A8%E5%8D%95%E5%88%87%E6%8D%A2/</guid><description>&lt;br>
在Web应用中经常会遇到frame/iframe表单嵌套页面的应用，WebDriver只能在一个页面上对元素识别与定位，对于frame/iframe表单内嵌页面上的元素无法直接定位。这时就需要通过switch_to.frame()方法将当前定位的主体切换为frame/iframe表单的内嵌页面中。
```html
&lt;html>
 &lt;body>
 ...
 &lt;iframe id="x-URS-iframe" ...>
 &lt;html>
 &lt;body>
 ...
 &lt;input name="email" >
```
126邮箱登录框的结构大概是这样子的，想要操作登录框必须要先切换到iframe表单。
```python
from selenium import webdriver
&lt;p>driver = webdriver.Chrome()
driver.get(&amp;ldquo;&lt;a href="http://www.126.com" target="_blank" rel="noopener noreffer ">http://www.126.com&lt;/a>&amp;rdquo;)&lt;/p>
&lt;p>driver.switch_to.frame(&amp;lsquo;x-URS-iframe&amp;rsquo;)
driver.find_element_by_name(&amp;ldquo;email&amp;rdquo;).clear()
driver.find_element_by_name(&amp;ldquo;email&amp;rdquo;).send_keys(&amp;ldquo;username&amp;rdquo;)
driver.find_element_by_name(&amp;ldquo;password&amp;rdquo;).clear()
driver.find_element_by_name(&amp;ldquo;password&amp;rdquo;).send_keys(&amp;ldquo;password&amp;rdquo;)
driver.find_element_by_id(&amp;ldquo;dologin&amp;rdquo;).click()
driver.switch_to.default_content()&lt;/p>
&lt;p>driver.quit()&lt;/p>
&lt;pre tabindex="0">&lt;code>switch_to.frame() 默认可以直接取表单的id 或name属性。如果iframe没有可用的id和name属性，则可以通过下面的方式进行定位。
```python
……
#先通过xpth定位到iframe
xf = driver.find_element_by_xpath(&amp;#39;//*[@id=&amp;#34;x-URS-iframe&amp;#34;]&amp;#39;)

#再将定位对象传给switch_to.frame()方法
driver.switch_to.frame(xf)
……
driver.switch_to.parent_frame()
&lt;/code>&lt;/pre>&lt;p>除此之外，在进入多级表单的情况下，还可以通过switch_to.default_content()跳回最外层的页面。&lt;/p>
&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/photo-1496297485239-4265d2ba2105?w=300"
 data-srcset="https://images.unsplash.com/photo-1496297485239-4265d2ba2105?w=300, https://images.unsplash.com/photo-1496297485239-4265d2ba2105?w=300 1.5x, https://images.unsplash.com/photo-1496297485239-4265d2ba2105?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/photo-1496297485239-4265d2ba2105?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>（十二）多窗口切换</title><link>https://itest2025.pages.dev/tutorials/24-012-%E5%8D%81%E4%BA%8C%E5%A4%9A%E7%AA%97%E5%8F%A3%E5%88%87%E6%8D%A2/</link><pubDate>Mon, 19 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-012-%E5%8D%81%E4%BA%8C%E5%A4%9A%E7%AA%97%E5%8F%A3%E5%88%87%E6%8D%A2/</guid><description>&lt;br>
在页面操作过程中有时候点击某个链接会弹出新的窗口，这时就需要主机切换到新打开的窗口上进行操作。WebDriver提供了switch_to.window()方法，可以实现在不同的窗口之间切换。
以百度首页和百度注册页为例，在两个窗口之间的切换如下图。
![](http://orru5lls3.bkt.clouddn.com/more_windows_n.png)
```python
from selenium import webdriver
import time
&lt;p>driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.get(&amp;ldquo;&lt;a href="http://www.baidu.com" target="_blank" rel="noopener noreffer ">http://www.baidu.com&lt;/a>&amp;rdquo;)&lt;/p>
&lt;h1 id="获得百度搜索窗口句柄">获得百度搜索窗口句柄&lt;/h1>
&lt;p>sreach_windows = driver.current_window_handle&lt;/p>
&lt;p>driver.find_element_by_link_text(&amp;lsquo;登录&amp;rsquo;).click()
driver.find_element_by_link_text(&amp;ldquo;立即注册&amp;rdquo;).click()&lt;/p>
&lt;h1 id="获得当前所有打开的窗口的句柄">获得当前所有打开的窗口的句柄&lt;/h1>
&lt;p>all_handles = driver.window_handles&lt;/p>
&lt;h1 id="进入注册窗口">进入注册窗口&lt;/h1>
&lt;p>for handle in all_handles:
if handle != sreach_windows:
driver.switch_to.window(handle)
print(&amp;rsquo;now register window!&amp;rsquo;)
driver.find_element_by_name(&amp;ldquo;account&amp;rdquo;).send_keys(&amp;lsquo;username&amp;rsquo;)
driver.find_element_by_name(&amp;lsquo;password&amp;rsquo;).send_keys(&amp;lsquo;password&amp;rsquo;)
time.sleep(2)
# ……&lt;/p>
&lt;p>driver.quit()&lt;/p>
&lt;pre tabindex="0">&lt;code>在本例中所涉及的新方法如下：

* current_window_handle：获得当前窗口句柄。
* window_handles：返回所有窗口的句柄到当前会话。
* switch_to.window()：用于切换到相应的窗口，与上一节的switch_to.frame()类似，前者用于不同窗口的切换，后者用于不同表单之间的切换。




原始封面

![课程图片](https://images.unsplash.com/photo-1434873740857-1bc5653afda8?w=300)
&lt;/code>&lt;/pre></description></item><item><title>（十三）警告框处理</title><link>https://itest2025.pages.dev/tutorials/24-013-%E5%8D%81%E4%B8%89%E8%AD%A6%E5%91%8A%E6%A1%86%E5%A4%84%E7%90%86/</link><pubDate>Sun, 18 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-013-%E5%8D%81%E4%B8%89%E8%AD%A6%E5%91%8A%E6%A1%86%E5%A4%84%E7%90%86/</guid><description>&lt;br>
在WebDriver中处理JavaScript所生成的alert、confirm以及prompt十分简单，具体做法是使用 switch_to.alert 方法定位到 alert/confirm/prompt，然后使用text/accept/dismiss/ send_keys等方法进行操作。
&lt;ul>
&lt;li>
&lt;p>text：返回 alert/confirm/prompt 中的文字信息。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>accept()：接受现有警告框。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>dismiss()：解散现有警告框。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>send_keys(keysToSend)：发送文本至警告框。keysToSend：将文本发送至警告框。&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>如下图，百度搜索设置弹出的窗口是不能通过前端工具对其进行定位的，这个时候就可以通过switch_to_alert()方法接受这个弹窗。
&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="http://orru5lls3.bkt.clouddn.com/alert_windows.png"
 data-srcset="http://orru5lls3.bkt.clouddn.com/alert_windows.png, http://orru5lls3.bkt.clouddn.com/alert_windows.png 1.5x, http://orru5lls3.bkt.clouddn.com/alert_windows.png 2x"
 data-sizes="auto"
 alt="http://orru5lls3.bkt.clouddn.com/alert_windows.png"
 title="http://orru5lls3.bkt.clouddn.com/alert_windows.png" />&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">webdriver&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium.webdriver.common.action_chains&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">ActionChains&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">time&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Firefox&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">implicitly_wait&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">10&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;http://www.baidu.com&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 鼠标悬停至“设置”链接&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">link&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_link_text&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;设置&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">ActionChains&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">driver&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">move_to_element&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">link&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">perform&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 打开搜索设置&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_link_text&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;搜索设置&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 保存设置&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_class_name&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;prefpanelgo&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">time&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">sleep&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">2&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 接受警告框&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">switch_to&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">alert&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">accept&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">quit&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>通过switch_to_alert()方法获取当前页面上的警告框，并使用accept()方法接受警告框。&lt;/p>
&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/photo-1459213599465-03ab6a4d5931?w=300"
 data-srcset="https://images.unsplash.com/photo-1459213599465-03ab6a4d5931?w=300, https://images.unsplash.com/photo-1459213599465-03ab6a4d5931?w=300 1.5x, https://images.unsplash.com/photo-1459213599465-03ab6a4d5931?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/photo-1459213599465-03ab6a4d5931?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>（十四）下拉框选择</title><link>https://itest2025.pages.dev/tutorials/24-014-%E5%8D%81%E5%9B%9B%E4%B8%8B%E6%8B%89%E6%A1%86%E9%80%89%E6%8B%A9/</link><pubDate>Sat, 17 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-014-%E5%8D%81%E5%9B%9B%E4%B8%8B%E6%8B%89%E6%A1%86%E9%80%89%E6%8B%A9/</guid><description>&lt;br>
有时我们会碰到下拉框，WebDriver提供了Select类来处理下拉框。
如百度搜索设置的下拉框，如下图：
![](http://orru5lls3.bkt.clouddn.com/select.png)
```python
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
&lt;p>driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get(&amp;lsquo;&lt;a href="http://www.baidu.com" target="_blank" rel="noopener noreffer ">http://www.baidu.com&lt;/a>&amp;rsquo;)&lt;/p>
&lt;h1 id="鼠标悬停至设置链接">鼠标悬停至“设置”链接&lt;/h1>
&lt;p>driver.find_element_by_link_text(&amp;lsquo;设置&amp;rsquo;).click()
sleep(1)&lt;/p>
&lt;h1 id="打开搜索设置">打开搜索设置&lt;/h1>
&lt;p>driver.find_element_by_link_text(&amp;ldquo;搜索设置&amp;rdquo;).click()
sleep(2)&lt;/p>
&lt;h1 id="搜索结果显示条数">搜索结果显示条数&lt;/h1>
&lt;p>sel = driver.find_element_by_xpath(&amp;quot;//select[@id=&amp;lsquo;nr&amp;rsquo;]&amp;quot;)
Select(sel).select_by_value(&amp;lsquo;50&amp;rsquo;) # 显示50条&lt;/p>
&lt;h1 id="heading">……&lt;/h1>
&lt;p>driver.quit()&lt;/p>
&lt;pre tabindex="0">&lt;code>Select类用于定位select标签。
select_by_value() 方法用于定位下接选项中的value值。




原始封面

![课程图片](https://images.unsplash.com/photo-1431887773042-803ed52bed26?w=300)
&lt;/code>&lt;/pre></description></item><item><title>（十五）文件上传</title><link>https://itest2025.pages.dev/tutorials/24-015-%E5%8D%81%E4%BA%94%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/</link><pubDate>Fri, 16 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-015-%E5%8D%81%E4%BA%94%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/</guid><description>&lt;br>
对于通过input标签实现的上传功能，可以将其看作是一个输入框，即通过send_keys()指定本地文件路径的方式实现文件上传。
&lt;p>创建upfile.html文件，代码如下：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">html&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">head&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">meta&lt;/span> &lt;span class="na">http-equiv&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;content-type&amp;#34;&lt;/span> &lt;span class="na">content&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;text/html;charset=utf-8&amp;#34;&lt;/span> &lt;span class="p">/&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">title&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>upload_file&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">title&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">link&lt;/span> &lt;span class="na">href&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css&amp;#34;&lt;/span> &lt;span class="na">rel&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;stylesheet&amp;#34;&lt;/span> &lt;span class="p">/&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">head&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">body&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">div&lt;/span> &lt;span class="na">class&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;row-fluid&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">	&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">div&lt;/span> &lt;span class="na">class&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;span6 well&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">	&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">h3&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>upload_file&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">h3&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">	 &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">input&lt;/span> &lt;span class="na">type&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;file&amp;#34;&lt;/span> &lt;span class="na">name&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;file&amp;#34;&lt;/span> &lt;span class="p">/&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">	&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">div&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">div&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">body&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">script&lt;/span> &lt;span class="na">src&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>&lt;span class="err">&amp;lt;/scrip&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">html&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>通过浏览器打开upfile.html文件，功能如下图。
&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="http://orru5lls3.bkt.clouddn.com/upfile.png"
 data-srcset="http://orru5lls3.bkt.clouddn.com/upfile.png, http://orru5lls3.bkt.clouddn.com/upfile.png 1.5x, http://orru5lls3.bkt.clouddn.com/upfile.png 2x"
 data-sizes="auto"
 alt="http://orru5lls3.bkt.clouddn.com/upfile.png"
 title="http://orru5lls3.bkt.clouddn.com/upfile.png" />&lt;/p>
&lt;p>接下来通过send_keys()方法来实现文件上传。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">webdriver&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">os&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Firefox&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">file_path&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s1">&amp;#39;file:///&amp;#39;&lt;/span> &lt;span class="o">+&lt;/span> &lt;span class="n">os&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">path&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">abspath&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;upfile.html&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">file_path&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 定位上传按钮，添加本地文件&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_name&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;file&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">send_keys&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;D:&lt;/span>&lt;span class="se">\\&lt;/span>&lt;span class="s1">upload_file.txt&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">quit&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/photo-1445964047600-cdbdb873673d?w=300"
 data-srcset="https://images.unsplash.com/photo-1445964047600-cdbdb873673d?w=300, https://images.unsplash.com/photo-1445964047600-cdbdb873673d?w=300 1.5x, https://images.unsplash.com/photo-1445964047600-cdbdb873673d?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/photo-1445964047600-cdbdb873673d?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>（十六）cookie操作</title><link>https://itest2025.pages.dev/tutorials/24-016-%E5%8D%81%E5%85%ADcookie%E6%93%8D%E4%BD%9C/</link><pubDate>Thu, 15 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-016-%E5%8D%81%E5%85%ADcookie%E6%93%8D%E4%BD%9C/</guid><description>&lt;br>
有时候我们需要验证浏览器中cookie是否正确，因为基于真实cookie的测试是无法通过白盒和集成测试进行的。WebDriver提供了操作Cookie的相关方法，可以读取、添加和删除cookie信息。
&lt;p>WebDriver操作cookie的方法：&lt;/p>
&lt;ul>
&lt;li>
&lt;p>get_cookies()：			获得所有cookie信息。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>get_cookie(name)：		返回字典的key为“name”的cookie信息。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>add_cookie(cookie_dict)	：	添加cookie。“cookie_dict”指字典对象，必须有name 和value 值。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>delete_cookie(name,optionsString)：删除cookie信息。“name”是要删除的cookie的名称，“optionsString”是该cookie的选项，目前支持的选项包括“路径”，“域”。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>delete_all_cookies()：		删除所有cookie信息。&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>下面通过get_cookies()来获取当前浏览器的cookie信息。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">webdriver&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Firefox&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;http://www.youdao.com&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 获得cookie信息&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">cookie&lt;/span>&lt;span class="o">=&lt;/span> &lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get_cookies&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 将获得cookie的信息打印&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">cookie&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">quit&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>从执行结果可以看出，cookie数据是以字典的形式进行存放的。知道了cookie的存放形式，接下来我们就可以按照这种形式向浏览器中写入cookie信息。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">webdriver&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Firefox&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;http://www.youdao.com&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 向cookie的name 和value中添加会话信息&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_cookie&lt;/span>&lt;span class="p">({&lt;/span>&lt;span class="s1">&amp;#39;name&amp;#39;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s1">&amp;#39;key-aaaaaaa&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;value&amp;#39;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s1">&amp;#39;value-bbbbbb&amp;#39;&lt;/span>&lt;span class="p">})&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 遍历cookies中的name 和value信息并打印，当然还有上面添加的信息&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">for&lt;/span> &lt;span class="n">cookie&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get_cookies&lt;/span>&lt;span class="p">():&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="si">%s&lt;/span>&lt;span class="s2"> -&amp;gt; &lt;/span>&lt;span class="si">%s&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> &lt;span class="o">%&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">cookie&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;name&amp;#39;&lt;/span>&lt;span class="p">],&lt;/span> &lt;span class="n">cookie&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s1">&amp;#39;value&amp;#39;&lt;/span>&lt;span class="p">]))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">quit&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">输出结果&lt;/span>&lt;span class="err">：&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="o">========================&lt;/span> &lt;span class="n">RESTART&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="o">=========================&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">YOUDAO_MOBILE_ACCESS_TYPE&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="mi">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">_PREF_ANONYUSER__MYTH&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="n">aGFzbG9nZ2VkPXRydWU&lt;/span>&lt;span class="o">=&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">OUTFOX_SEARCH_USER_ID&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="o">-&lt;/span>&lt;span class="mi">1046383847&lt;/span>&lt;span class="o">@&lt;/span>&lt;span class="mf">218.17.158.115&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">JSESSIONID&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="n">abc7qSE_SBGsVgnVLBvcu&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">key&lt;/span>&lt;span class="o">-&lt;/span>&lt;span class="n">aaaaaaa&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="n">value&lt;/span>&lt;span class="o">-&lt;/span>&lt;span class="n">bbbbbb&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>从执行结果可以看到，最后一条cookie信息是在脚本执行过程中通过add_cookie()方法添加的。通过遍历得到所有的cookie信息，从而找到key为“name”和“value”的特定cookie的value。&lt;/p>
&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/uploads/1412026095116d2b0c90e/3bf33993?w=300"
 data-srcset="https://images.unsplash.com/uploads/1412026095116d2b0c90e/3bf33993?w=300, https://images.unsplash.com/uploads/1412026095116d2b0c90e/3bf33993?w=300 1.5x, https://images.unsplash.com/uploads/1412026095116d2b0c90e/3bf33993?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/uploads/1412026095116d2b0c90e/3bf33993?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>（十七）调用JavaScript代码</title><link>https://itest2025.pages.dev/tutorials/24-017-%E5%8D%81%E4%B8%83%E8%B0%83%E7%94%A8javascript%E4%BB%A3%E7%A0%81/</link><pubDate>Wed, 14 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-017-%E5%8D%81%E4%B8%83%E8%B0%83%E7%94%A8javascript%E4%BB%A3%E7%A0%81/</guid><description>&lt;br>
虽然WebDriver提供了操作浏览器的前进和后退方法，但对于浏览器滚动条并没有提供相应的操作方法。在这种情况下，就可以借助JavaScript来控制浏览器的滚动条。WebDriver提供了execute_script()方法来执行JavaScript代码。
&lt;p>用于调整浏览器滚动条位置的JavaScript代码如下：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="line">&lt;span class="cl">&lt;span class="c">&amp;lt;!-- window.scrollTo(左边距,上边距); --&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">window.scrollTo(0,450);
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>window.scrollTo()方法用于设置浏览器窗口滚动条的水平和垂直位置。方法的第一个参数表示水平的左间距，第二个参数表示垂直的上边距。其代码如下：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">webdriver&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">time&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">sleep&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 访问百度&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Firefox&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;http://www.baidu.com&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 设置浏览器窗口大小&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">set_window_size&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">500&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">500&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 搜索&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_id&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;kw&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">send_keys&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;selenium&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">find_element_by_id&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;su&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">click&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">sleep&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">2&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 通过javascript设置浏览器窗口的滚动条位置&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">js&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;window.scrollTo(100,450);&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">execute_script&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">js&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">sleep&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">3&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">quit&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>通过浏览器打开百度进行搜索，并且提前通过set_window_size()方法将浏览器窗口设置为固定宽高显示，目的是让窗口出现水平和垂直滚动条。然后通过execute_script()方法执行JavaScripts代码来移动滚动条的位置。&lt;/p>
&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/photo-1464039397811-476f652a343b?w=300"
 data-srcset="https://images.unsplash.com/photo-1464039397811-476f652a343b?w=300, https://images.unsplash.com/photo-1464039397811-476f652a343b?w=300 1.5x, https://images.unsplash.com/photo-1464039397811-476f652a343b?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/photo-1464039397811-476f652a343b?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>（十八）窗口截图</title><link>https://itest2025.pages.dev/tutorials/24-018-%E5%8D%81%E5%85%AB%E7%AA%97%E5%8F%A3%E6%88%AA%E5%9B%BE/</link><pubDate>Tue, 13 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-018-%E5%8D%81%E5%85%AB%E7%AA%97%E5%8F%A3%E6%88%AA%E5%9B%BE/</guid><description>&lt;br>
自动化用例是由程序去执行的，因此有时候打印的错误信息并不十分明确。如果在脚本执行出错的时候能对当前窗口截图保存，那么通过图片就可以非常直观地看出出错的原因。WebDriver提供了截图函数get_screenshot_as_file()来截取当前窗口。
```python
from selenium import webdriver
from time import sleep
&lt;p>driver = webdriver.Firefox()
driver.get(&amp;lsquo;&lt;a href="http://www.baidu.com" target="_blank" rel="noopener noreffer ">http://www.baidu.com&lt;/a>&amp;rsquo;)&lt;/p>
&lt;p>driver.find_element_by_id(&amp;lsquo;kw&amp;rsquo;).send_keys(&amp;lsquo;selenium&amp;rsquo;)
driver.find_element_by_id(&amp;lsquo;su&amp;rsquo;).click()
sleep(2)&lt;/p>
&lt;h1 id="截取当前窗口并指定截图图片的保存位置">截取当前窗口，并指定截图图片的保存位置&lt;/h1>
&lt;p>driver.get_screenshot_as_file(&amp;ldquo;D:\baidu_img.jpg&amp;rdquo;)&lt;/p>
&lt;p>driver.quit()&lt;/p>
&lt;pre tabindex="0">&lt;code>脚本运行完成后打开D盘，就可以找到baidu_img.jpg图片文件了。




原始封面

![课程图片](https://images.unsplash.com/photo-1513836279014-a89f7a76ae86?w=300)
&lt;/code>&lt;/pre></description></item><item><title>（十九）关闭浏览器</title><link>https://itest2025.pages.dev/tutorials/24-019-%E5%8D%81%E4%B9%9D%E5%85%B3%E9%97%AD%E6%B5%8F%E8%A7%88%E5%99%A8/</link><pubDate>Mon, 12 Jun 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-019-%E5%8D%81%E4%B9%9D%E5%85%B3%E9%97%AD%E6%B5%8F%E8%A7%88%E5%99%A8/</guid><description>&lt;br>
在前面的例子中我们一直使用quit()方法，其含义为退出相关的驱动程序和关闭所有窗口。除此之外，WebDriver还提供了close()方法，用来关闭当前窗口。例多窗口的处理，在用例执行的过程中打开了多个窗口，我们想要关闭其中的某个窗口，这时就要用到close()方法进行关闭了。
&lt;ul>
&lt;li>
&lt;p>close() 关闭单个窗口&lt;/p>
&lt;/li>
&lt;li>
&lt;p>quit() 关闭所有窗口&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>原始封面&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="https://images.unsplash.com/photo-1532274402911-5a369e4c4bb5?w=300"
 data-srcset="https://images.unsplash.com/photo-1532274402911-5a369e4c4bb5?w=300, https://images.unsplash.com/photo-1532274402911-5a369e4c4bb5?w=300 1.5x, https://images.unsplash.com/photo-1532274402911-5a369e4c4bb5?w=300 2x"
 data-sizes="auto"
 alt="https://images.unsplash.com/photo-1532274402911-5a369e4c4bb5?w=300"
 title="课程图片" />&lt;/p></description></item><item><title>常见问题</title><link>https://itest2025.pages.dev/tutorials/24-000-%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98/</link><pubDate>Mon, 16 Oct 2017 12:59:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-000-%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98/</guid><description>&lt;h3 id="什么是firefox的profileprofile有什么用">什么是firefox的profile，profile有什么用？&lt;/h3>
&lt;hr>
&lt;p>&lt;a href="https://support.mozilla.org/en-US/kb/profiles-where-firefox-stores-user-data?redirectlocale=en-US&amp;amp;redirectslug=Profiles" target="_blank" rel="noopener noreffer ">官方解释&lt;/a>。&lt;/p>
&lt;p>简单来说，profile保存了&lt;/p>
&lt;ul>
&lt;li>书签&lt;/li>
&lt;li>记住的password&lt;/li>
&lt;li>其他信息，比如cookie&lt;/li>
&lt;/ul>
&lt;p>webidrver启动浏览器的时候会产生临时的profile，这也是为什么每次浏览器启动以后我们都要重新登录系统的原因，profile重置了，登录态也就没保存下来了。&lt;/p>
&lt;p>我们在使用selenium做测试的时候可以指定使用1个已经存在的profile，从而实现一段时间的免登录功能。&lt;/p>
&lt;p>&lt;a href="https://support.mozilla.org/en-US/kb/profiles-where-firefox-stores-user-data?redirectlocale=en-US&amp;amp;redirectslug=Profiles#w_how-do-i-find-my-profile" target="_blank" rel="noopener noreffer ">如何找到firefox的profile文件&lt;/a>&lt;/p>
&lt;h4 id="如何在selenium中指定使用已存在的profile">如何在selenium中指定使用已存在的profile&lt;/h4>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 传入profile所在的绝对路径&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">profile&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">FirefoxProfile&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;/home/jmunsch/.mozilla/firefox/yxjwk1py.default&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Firefox&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">profile&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="chrome-headless-模式">Chrome headless 模式&lt;/h3>
&lt;hr>
&lt;p>我们在通过Selenium运行自动化测试时，必须要启动浏览器，浏览器的启动与关闭必然会影响执行效率，而且还会干扰你做其它事情（本机运行的话）。&lt;/p>
&lt;p>那能不能把自动化测试的运行放在后台？当然可以！&lt;/p>
&lt;ul>
&lt;li>
&lt;p>htmlunit 项目可以模拟浏览器运行，是一个没有界面的浏览器，运行速度快。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>PhantomJS 是一个基于webkit的JavaScript API。它使用QtWebKit作为它核心浏览器的功能，使用webkit来编译解释执行JavaScript代码。任何你可以在基于webkit浏览器做的事情，它都能做到。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Chrome-headless 模式，Google 自己出的无头浏览器模式。&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>htmlunit&lt;/strong> 在Selenium 下面，我都没跑通过一个 baidu 的 demo, 因为它不打开 UI 界面，所以，你也不知道它后台是如何渲染页面的。 弃之！&lt;/p>
&lt;p>&lt;strong>PhantomJS&lt;/strong> 非常不错，因为是使用的 QtWebKit 浏览器内核渲染页面，基本可以和真正浏览器保持一致。&lt;/p>
&lt;p>&lt;strong>Chrome-headless 模式&lt;/strong>， Google 针对 Chrome 浏览器新增加的一种模式，可以让你不打开UI界面的情况下使用 Chrome 浏览器，所以运行效果与 Chrome 保持完美一致。PhantomJS作者说，你这么搞我失业了啊！&lt;/p>
&lt;h4 id="用法">用法&lt;/h4>
&lt;p>Python Selenium 用法:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">webdriver&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">selenium.webdriver.chrome.options&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">Options&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">chrome_options&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">Options&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">chrome_options&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_argument&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;--headless&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">driver&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">webdriver&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Chrome&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">chrome_options&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">chrome_options&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="o">...&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Java Selenium用法:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-Java" data-lang="Java">&lt;span class="line">&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="kn">import&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nn">org.openqa.selenium.WebDriver&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="kn">import&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nn">org.openqa.selenium.chrome.ChromeDriver&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="kn">import&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nn">org.openqa.selenium.chrome.ChromeOptions&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="kd">public&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="kd">class&lt;/span> &lt;span class="nc">WebTest&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="kd">public&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="kd">static&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="kt">void&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nf">main&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">String&lt;/span>&lt;span class="o">[]&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">args&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="kd">throws&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">InterruptedException&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">ChromeOptions&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">chromeOptions&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">new&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">ChromeOptions&lt;/span>&lt;span class="p">();&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">chromeOptions&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="na">addArguments&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s">&amp;#34;--headless&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">WebDriver&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">driver&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">new&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">ChromeDriver&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">chromeOptions&lt;/span>&lt;span class="p">);&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="p">...&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="p">}&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="p">}&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>原始封面&lt;/p></description></item><item><title>选读：Selenium的发展史</title><link>https://itest2025.pages.dev/tutorials/24-000-%E9%80%89%E8%AF%BBselenium%E7%9A%84%E5%8F%91%E5%B1%95%E5%8F%B2/</link><pubDate>Fri, 25 Aug 2017 02:20:11 +0800</pubDate><guid>https://itest2025.pages.dev/tutorials/24-000-%E9%80%89%E8%AF%BBselenium%E7%9A%84%E5%8F%91%E5%B1%95%E5%8F%B2/</guid><description>&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="http://img.testclass.net/selenium-logo.png"
 data-srcset="http://img.testclass.net/selenium-logo.png, http://img.testclass.net/selenium-logo.png 1.5x, http://img.testclass.net/selenium-logo.png 2x"
 data-sizes="auto"
 alt="http://img.testclass.net/selenium-logo.png"
 title="http://img.testclass.net/selenium-logo.png" />&lt;/p>
&lt;p>Jason Huggins在2004年发起了Selenium项目，当时身处ThoughtWorks的他，为了不想让自己的时间浪费在无聊的重复性工作中，幸运的是，所有被测试的浏览器都支持Javascript。Jason和他所在的团队采用Javascript编写一种测试工具来验证浏览器页面的行为；这个JavaScript类库就是Selenium core，同时也是seleniumRC、Selenium IDE的核心组件。Selenium由此诞生。&lt;/p>
&lt;p>关于Selenium的命名比较有意思，当时QTP mercury是主流的商业自化工具，是化学元素汞（俗称水银），而Selenium是开源自动化工具，是化学元素硒，硒可以对抗汞。&lt;/p>
&lt;h3 id="selenium-10">Selenium 1.0&lt;/h3>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="http://img.testclass.net/selenium_1.jpg"
 data-srcset="http://img.testclass.net/selenium_1.jpg, http://img.testclass.net/selenium_1.jpg 1.5x, http://img.testclass.net/selenium_1.jpg 2x"
 data-sizes="auto"
 alt="http://img.testclass.net/selenium_1.jpg"
 title="http://img.testclass.net/selenium_1.jpg" />&lt;/p>
&lt;p>用简单的公式：&lt;/p>
&lt;pre tabindex="0">&lt;code>Selenium 1.0 = Selenium IDE + Selenium Grid + Selenium RC
&lt;/code>&lt;/pre>&lt;p>&lt;strong>Selenium IDE&lt;/strong>&lt;/p>
&lt;p>Selenium IDE是嵌入到Firefox浏览器中的一个插件，实现简单的浏览器操作的录制与回放功能。&lt;/p>
&lt;p>&lt;strong>Selenium Grid&lt;/strong>&lt;/p>
&lt;p>Selenium Grid是一种自动化的测试辅助工具，Grid通过利用现有的计算机基础设施，能加快Web-App的功能测试。利用Grid可以很方便地实现在多台机器上和异构环境中运行测试用例。&lt;/p>
&lt;p>&lt;strong>Selenium RC&lt;/strong>&lt;/p>
&lt;p>Selenium RC（Remote Control）是Selenium家族的核心部分。Selenium RC 支持多种不同语言编写的自动化测试脚本，通过Selenium RC的服务器作为代理服务器去访问应用，从而达到测试的目的。&lt;/p>
&lt;p>Selenium RC分为Client Libraries和Selenium Server。Client Libraries库主要用于编写测试脚本，用来控制Selenium Server的库。Selenium Server负责控制浏览器行为。&lt;/p>
&lt;p>所以，我们在学习Selenium1.0的时候，核心应该是学习RC，它的工作原理是这样的：&lt;/p>
&lt;p>&lt;img
 class="lazyload"
 src="https://itest2025.pages.dev/svg/loading.min.svg"
 data-src="http://img.testclass.net/selenium_RC.jpg"
 data-srcset="http://img.testclass.net/selenium_RC.jpg, http://img.testclass.net/selenium_RC.jpg 1.5x, http://img.testclass.net/selenium_RC.jpg 2x"
 data-sizes="auto"
 alt="http://img.testclass.net/selenium_RC.jpg"
 title="http://img.testclass.net/selenium_RC.jpg" />&lt;/p></description></item></channel></rss>