-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
175 lines (83 loc) · 93.9 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Helios'blog</title>
<link href="http://example.com/atom.xml" rel="self"/>
<link href="http://example.com/"/>
<updated>2024-12-15T10:55:23.898Z</updated>
<id>http://example.com/</id>
<author>
<name>Helios</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>Selenium的CSS层叠样式表选择器(二)</title>
<link href="http://example.com/2024/12/15/selenium/selenium03/"/>
<id>http://example.com/2024/12/15/selenium/selenium03/</id>
<published>2024-12-15T10:45:54.334Z</published>
<updated>2024-12-15T10:55:23.898Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p><strong>Selenium的CSS层叠样式表选择器(二)</strong></p><p><strong>实验目的:</strong>了解CSS Selector的选择语法的联合使用方法,掌握组选择、按顺序选择子节点和兄弟节点选择的方法。能够使用CSS 选择器选择语法的联合使用完成要求的元素定位。</p><p><strong>实验要求:</strong>在pycharm 环境下完成实验目的中所述各项任务 </p><p><strong>实验条件:</strong>win7/10、pycharm、selenium4.4.0 </p><p><strong>实验内容及步骤:</strong></p><p>使用CSS选择器选择元素时,是具有很强的灵活性的。比如,要选择界面中的一个元素,既可以使用它的某个唯一的属性值,也可以根据父子关系按照层级定位它。CSS选择器除了具有灵活性外,另一个强大之处在于:选择语法可以联合使用。</p><p>一、 组选择:同时选择多个元素,各个选择条件用逗号(,)隔开,允许逗号两端的属性不一致</p><p>如果要同时选择所有class名 为 plant 和 animal 的元素,则应写为: </p><p><code>find_element(By.CSS_SELECTOR, '.plant, .animal')</code></p><p>注意:通过组选择能够完成同时选择多个元素的工作,其选中元素不是按照组表达式的次序排序的,而是先选择出符合这些表达式的元素,然后按照它们在HTML文档中的出现的次序排列。</p><ol><li>在浏览器中打开百度新闻<a href="https://news.baidu.com/%E7%BD%91%E7%AB%99%EF%BC%8C%E9%80%89%E6%8B%A9%E2%80%9C%E7%83%AD%E7%82%B9%E8%A6%81%E9%97%BB%E2%80%9D%E6%A0%8F%E7%9B%AE%E4%B8%AD%E7%9A%84class%E5%90%8D">https://news.baidu.com/网站,选择“热点要闻”栏目中的class名</a> 为 focuslistnews 和 hotnews的元素,计算满足此条件的元素共有多少项,输出:“热点要闻栏目中有XX项焦点列表和热点新闻项”。使用<code>qiut()</code>关闭浏览器。</li></ol><p>二、 按次序选择子节点:(练习网页:<a href="https://cdn2.byhy.net/files/selenium/sample1b.html">https://cdn2.byhy.net/files/selenium/sample1b.html</a> )</p><p><img src="/img/selenium/se03-00.jpg" alt="选择子节点">指定选择的元素是父元素的第几个子节点,语法为 :nth-child(),括号中填写第几个子节点的数字。</p><p>例如:要选择唐诗和宋词的第一个作者,也就是选择第2个子元素,且为span类型,CSS Selector表达式可写为:</p><p><code>span:nth-child(2)</code> </p><p>或 </p><p><code>\#t1 :nth-child(2) , #t2 :nth-child(2)</code></p><p>又或者</p><p><code>\#t1 > :nth-child(2) , #t2 > :nth-child(2)</code></p><p>指定选择的元素 是父元素的倒数第几个子节点,语法为 :<code>nth-last-child()</code>,括号中填写第几个子节点的数字。</p><p>例如:要选择最后一首唐诗和宋词的题目,CSS Selector表达式可写为:<code>p:nth-last-child(1)</code></p><p>指定选择的元素是父元素第几个某类型的子节点,语法为 :nth-of-type(),括号中填写第几个子节点的数字。</p><p>例如:要使用类型限定选择唐诗和宋词的第一个作者,由于它们是父元素下第一个span类型的子元素,所以CSS Selector表达式可写为: <code>span:nth-of-type(1)</code></p><p>指定选择的元素是父元素倒数第几个某类型的子节点,语法为 <code>:nth-last-of-type()</code>,括号中填写第几个子节点的数字。</p><p>例如:要选择唐诗和宋词的倒数第一个作者,CSS Selector表达式可写为: <code>span:nth-last-of-type(1)</code></p><p>指定选择的元素是父元素的奇数或偶数的子节点,语法为 <code>:nth-child(odd)</code> 及 <code>:nth-child(even)</code></p><p>例如:<code>#t1 :nth-child(odd)</code></p><p>指定选择的元素 是父元素某种类型的奇数或偶数的子节点,语法为 <code>:nth-of-type(odd)</code> 及 <code>:nth-of-type(even)</code></p><p>例如:<code>#t2 p:nth-of-type(even)</code></p><ol start="2"><li>在上个操作步骤的关闭浏览器之前,添加:按照次序选择子节点的任意两种不同的方式,完成对“热点要闻”栏目中的class名为hotnews的元素中的第四个新闻标题的获取,并输出显示文本信息。</li></ol><p>三、 兄弟节点选择:</p><p>相邻兄弟节点选择,就是选择一个元素平级的后面紧跟着的兄弟节点,相邻关系语法用加号+表示。</p><p>例如:要选择唐诗和宋词的第一个作者,因为唐诗和宋词的标签名都是h3,紧跟着它的平级兄弟节点span就是作者,所以CSS Selector表达式可以写为:<code>h3 + span</code></p><p>也就是选择紧跟 <code><h3></code> 元素的首个 <code><span></code> 元素</p><p>后续所有兄弟节点选择,用波浪线~表示。</p><p>例如:要选择唐诗和宋词的所有作者,CSS Selector表达式可以写为:<code>h3 ~ span</code></p><ol start="3"><li><p>在上个操作步骤的关闭浏览器之前,添加:按照兄弟节点选择方式,完成对“热点要闻”栏目中的class名为hotnews的元素的相邻兄弟节点的选择,并输出此兄弟节点的全部可见文本。</p></li><li><p>完成上述三个实验步骤(1,2,3),提交代码</p></li></ol><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.common.by <span class="keyword">import</span> By</span><br><span class="line"></span><br><span class="line">driver = webdriver.Chrome()</span><br><span class="line"><span class="comment"># 练习一</span></span><br><span class="line">driver.get(<span class="string">"https://news.baidu.com/"</span>)</span><br><span class="line">focuslistnews_items = driver.find_elements(By.CSS_SELECTOR,<span class="string">'.focuslistnews , .hotnews'</span>)</span><br><span class="line"><span class="comment"># 计算满足条件的元素数量</span></span><br><span class="line">total_items = <span class="built_in">len</span>(focuslistnews_items)</span><br><span class="line"><span class="comment"># 输出计算结果</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f"热点要闻栏目中有<span class="subst">{total_items}</span>项焦点列表和热点新闻项"</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">"###################################"</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 练习二</span></span><br><span class="line"><span class="comment">#1. 父元素的第几个子节点</span></span><br><span class="line">hotnews_items = driver.find_element(By.CSS_SELECTOR, <span class="string">'.hotnews'</span>)</span><br><span class="line">title4_text = hotnews_items.find_element(By.CSS_SELECTOR, <span class="string">"ul>li:nth-child(4)"</span>).text</span><br><span class="line"><span class="comment"># 输出第四个标题的文本信息</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f"热点要闻中第四个新闻标题为:<span class="subst">{title4_text}</span>"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#2.父元素第几个某类型的子节点</span></span><br><span class="line">hotnews_items = driver.find_element(By.CSS_SELECTOR, <span class="string">'.hotnews'</span>)</span><br><span class="line">title4_text = hotnews_items.find_element(By.CSS_SELECTOR, <span class="string">"ul > li:nth-of-type(4)"</span>).text</span><br><span class="line"><span class="comment"># 输出第四个标题的文本信息</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f"热点要闻中第四个新闻标题为:<span class="subst">{title4_text}</span>"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#3.通过子元素类型,父元素获取子元素的方法</span></span><br><span class="line">hotnews_items = driver.find_elements(By.CSS_SELECTOR, <span class="string">'.hotnews'</span>)</span><br><span class="line">hotnews_element = hotnews_items[<span class="number">0</span>]</span><br><span class="line">hotnews_titles = hotnews_element.find_elements(By.TAG_NAME, <span class="string">"li"</span>)</span><br><span class="line">title4_text = hotnews_titles[<span class="number">3</span>].text</span><br><span class="line"><span class="comment"># 输出第四个标题的文本信息</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f"热点要闻中第四个新闻标题为:<span class="subst">{title4_text}</span>"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#4.通过索引获取子元素的方法</span></span><br><span class="line">hotnews_element = hotnews_items[<span class="number">0</span>]</span><br><span class="line">title4_text = hotnews_element.find_element(By.XPATH, <span class="string">"./ul/li[4]"</span>).text</span><br><span class="line"><span class="comment"># 输出第四个标题的文本信息</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f"热点要闻中第四个新闻标题为:<span class="subst">{title4_text}</span>"</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">"###################################"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 练习三</span></span><br><span class="line"><span class="comment"># 查找热点要闻元素的相邻兄弟元素</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用CSS选择器(元素+元素)</span></span><br><span class="line">sibling_element = driver.find_element(By.CSS_SELECTOR, <span class="string">'.hotnews+ul'</span>)</span><br><span class="line"><span class="comment"># 输出相邻的兄弟元素的全部可见文本</span></span><br><span class="line"><span class="built_in">print</span>(sibling_element.text)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用XPATH</span></span><br><span class="line"><span class="comment"># hotnews_element = driver.find_element(By.CSS_SELECTOR, '.hotnews')</span></span><br><span class="line"><span class="comment"># sibling_element = hotnews_element.find_element(By.XPATH, "./following-sibling::*[1]")</span></span><br><span class="line"><span class="comment"># 输出相邻兄弟元素的全部可见文本</span></span><br><span class="line"><span class="comment"># print(f"热点要闻元素的相邻兄弟元素的全部可见文本为:{sibling_element.text}")</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 关闭浏览器</span></span><br><span class="line">driver.quit()</span><br><span class="line"></span><br></pre></td></tr></table></figure><ol start="2"><li>在浏览器中打开:京东商城网页<a href="https://www.jd.com/%EF%BC%8C%E5%BA%94%E7%94%A8%E6%8C%89%E7%85%A7%E6%AC%A1%E5%BA%8F%E9%80%89%E6%8B%A9%E5%85%83%E7%B4%A0%E7%9A%84%E6%96%B9%E6%B3%95%E5%8E%BB%E6%A3%80%E6%9F%A5%E9%A1%B5%E9%9D%A2%E5%B7%A6%E4%BE%A7%E5%AF%BC%E8%88%AA%E6%A0%8F%E4%B8%AD%E6%98%AF%E5%90%A6%E6%9C%89%E5%A6%82%E4%B8%8B%E8%8F%9C%E5%8D%95%EF%BC%9A%E2%80%9C%E5%AE%B6%E7%94%A8%E7%94%B5%E5%99%A8|%E7%94%B5%E8%84%91">https://www.jd.com/,应用按照次序选择元素的方法去检查页面左侧导航栏中是否有如下菜单:“家用电器|电脑</a> / 办公|男装 / 女装 / 童装 / 内衣|女鞋 / 箱包 / 钟表 / 珠宝|房产 / 汽车 / 汽车用品|食品 / 酒类 / 生鲜 / 特产|医药保健 / 计生情趣|机票 / 酒店 / 旅游 / 生活|安装 / 维修 / 清洗 / 二手”,再使用兄弟节点选择方法定位页面头部导航栏中“京东超市”后三项菜单是否为“秒杀”、“便宜包邮”、“京东生鲜”,输出检查结果。关闭浏览器。</li></ol><h4 id="提交代码"><a href="#提交代码" class="headerlink" title="提交代码"></a>提交代码</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.common.by <span class="keyword">import</span> By</span><br><span class="line"></span><br><span class="line">driver = webdriver.Chrome()</span><br><span class="line">driver.get(<span class="string">'https://www.jd.com/'</span>)</span><br><span class="line"><span class="comment"># 1:检查左侧导航栏中是否有指定菜单</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">menu_list = [<span class="string">"家用电器"</span>, <span class="string">"电脑"</span>, <span class="string">"男装"</span>, <span class="string">"女装"</span>, <span class="string">"童装"</span>, <span class="string">"内衣"</span>, <span class="string">"女鞋"</span>, <span class="string">"箱包"</span>, <span class="string">"钟表"</span>, <span class="string">"珠宝"</span>,</span><br><span class="line"> <span class="string">"房产"</span>, <span class="string">"汽车"</span>, <span class="string">"汽车用品"</span>, <span class="string">"食品"</span>, <span class="string">"酒类"</span>, <span class="string">"生鲜"</span>, <span class="string">"特产"</span>, <span class="string">"医药保健"</span>, <span class="string">"计生情趣"</span>, <span class="string">"机票"</span>,</span><br><span class="line"> <span class="string">"酒店"</span>, <span class="string">"旅游"</span>, <span class="string">"生活"</span>, <span class="string">"安装"</span>, <span class="string">"维修"</span>, <span class="string">"清洗"</span>, <span class="string">"二手"</span>]</span><br><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> menu_list:</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> element = driver.find_elements(By.CSS_SELECTOR, <span class="string">f"#J_cate > ul> li[title='<span class="subst">{item}</span>']"</span>)</span><br><span class="line"> <span class="comment"># J_cate//*[@id="J_cate"]/ul</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f"找到菜单:<span class="subst">{item}</span>"</span>)</span><br><span class="line"> <span class="keyword">except</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f"未找到菜单:<span class="subst">{item}</span>"</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">"##############################################################"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2:检查头部导航栏中指定菜单后三项是否正确</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#CSS后兄弟节点选择</span></span><br><span class="line"></span><br><span class="line">brother_menus = [<span class="string">"秒杀"</span>, <span class="string">"便宜包邮"</span>, <span class="string">"京东生鲜"</span>]</span><br><span class="line">array=[]</span><br><span class="line"></span><br><span class="line">target_element = driver.find_elements(By.CSS_SELECTOR, <span class="string">"#navitems-group1 > li.fore1~li"</span>)</span><br><span class="line"><span class="keyword">for</span> element <span class="keyword">in</span> target_element:</span><br><span class="line"> array.append(element.text)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line"> <span class="keyword">if</span> array[i] == brother_menus[i]:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">f"第<span class="subst">{i+<span class="number">1</span>}</span>个:<span class="subst">{brother_menus[i]}</span>检查通过!"</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"检查未通过"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># XPATH</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># try:</span></span><br><span class="line"><span class="comment"># target_element = driver.find_element(By.XPATH, "//a[contains(text(), '京东超市')]/following-sibling::a[1]")</span></span><br><span class="line"><span class="comment"># for i in range(3):</span></span><br><span class="line"><span class="comment"># element_text = target_element.get_attribute("text")</span></span><br><span class="line"><span class="comment"># if element_text == brother_menus[i]:</span></span><br><span class="line"><span class="comment"># print(f"第{i + 1}个菜单:{element_text},检查通过!")</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># print(f"第{i + 1}个菜单:{element_text},检查未通过!")</span></span><br><span class="line"><span class="comment"># # target_element = target_element.find_element(By.CSS_SELECTOR, "+ li")</span></span><br><span class="line"><span class="comment"># except:</span></span><br><span class="line"><span class="comment"># print("找不到京东超市菜单!")</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 关闭浏览器</span></span><br><span class="line">driver.quit()</span><br></pre></td></tr></table></figure><ol start="3"><li><p>应用选择语法的联合使用方法编写测试用例:以管理员身份登录 <a href="http://127.0.0.1:8047/mgr/sign.html%EF%BC%8C%E7%94%A8%E6%88%B7%E5%90%8D">http://127.0.0.1:8047/mgr/sign.html,用户名</a> :byhy 密码: 88888888。点击添加药品,输入正确格式的药品名、编号和描述,点击创建。预期结果为:成功登录后,检查药品列表第一项结果中 药品名、编号和描述都是正确的。 </p><h4 id="提交代码-1"><a href="#提交代码-1" class="headerlink" title="提交代码"></a>提交代码</h4></li></ol><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.common.by <span class="keyword">import</span> By</span><br><span class="line"></span><br><span class="line">driver = webdriver.Chrome()</span><br><span class="line"><span class="comment"># 打开待测试环境</span></span><br><span class="line">driver.get(<span class="string">'http://127.0.0.1:8047/mgr/sign.html'</span>)</span><br><span class="line">elementuser = driver.find_element(By.ID, <span class="string">'username'</span>)</span><br><span class="line">elementuser.send_keys(<span class="string">'byhy'</span>)</span><br><span class="line">elementpass = driver.find_element(By.ID, <span class="string">'password'</span>)</span><br><span class="line">elementpass.send_keys(<span class="string">'88888888'</span>)</span><br><span class="line">elementbutton=driver.find_element(By.TAG_NAME, <span class="string">'button'</span>).click()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">sleep(<span class="number">3</span>)</span><br><span class="line"><span class="comment"># 创建药品</span></span><br><span class="line"><span class="comment"># 点击菜单栏药品,进入药品信息页面</span></span><br><span class="line">medicines_menu = driver.find_element(By.XPATH, <span class="string">'//*[@id="root"]/aside/section/ul/li[3]/a'</span>).click()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 找到添加药品按钮并点击</span></span><br><span class="line">medicines_button = driver.find_element(By.CSS_SELECTOR, <span class="string">'#root > div > section.content.container-fluid > div.col-lg-12.col-md-12.col-sm-12.add-one-area > button'</span>).click()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 输入药品信息,药品名,编号,描述</span></span><br><span class="line">medicines_name = driver.find_element(By.CSS_SELECTOR,<span class="string">'.col-lg-8>:nth-child(1)>input'</span>).send_keys(<span class="string">'阿莫西林'</span>)</span><br><span class="line">medicines_number = driver.find_element(By.CSS_SELECTOR,<span class="string">'.col-lg-8>:nth-child(2)>input'</span>).send_keys(<span class="string">'CAS : 26787-78-0'</span>)</span><br><span class="line">medicines_describe = driver.find_element(By.CSS_SELECTOR,<span class="string">'.col-lg-8>:nth-child(3)>textarea'</span>).send_keys(<span class="string">'阿莫西林片:0.125 g;0.25 g。'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 点击创建按钮</span></span><br><span class="line">create_button = driver.find_element(By.CSS_SELECTOR,<span class="string">'.col-lg-12>.btn-xs:nth-child(1)'</span>).click()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 点击取消按钮</span></span><br><span class="line"><span class="comment"># sleep(1)</span></span><br><span class="line"><span class="comment"># create_button = driver.find_element(By.CSS_SELECTOR, '.content>.col-lg-12>.col-lg-12 button:nth-child(2)')</span></span><br><span class="line"><span class="comment"># create_button.click()</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 实际结果</span></span><br><span class="line">actual_result = []</span><br><span class="line"><span class="comment"># actual_result = ''</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定位左侧菜单栏</span></span><br><span class="line">elements = driver.find_elements(By.CSS_SELECTOR, <span class="string">'#root > div > section.content.container-fluid > div:nth-child(3)> *'</span>)</span><br><span class="line"><span class="keyword">for</span> element <span class="keyword">in</span> elements:</span><br><span class="line"> actual_result.append(element.text)</span><br><span class="line">actual_result.remove(actual_result[-<span class="number">1</span>])</span><br><span class="line">new_string_array = [s[<span class="number">3</span>:] <span class="keyword">for</span> s <span class="keyword">in</span> actual_result]</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(new_string_array)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 预期结果</span></span><br><span class="line">expected_result = [<span class="string">'阿莫西林'</span>,<span class="string">'CAS : 26787-78-0'</span>,<span class="string">'阿莫西林片:0.125 g;0.25 g。'</span>]</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'预期结果为: '</span>, expected_result)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过try抛出异常进行断言判断</span></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> <span class="keyword">assert</span> expected_result == new_string_array</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'PASS,实际结果与预期结果一致!'</span>)</span><br><span class="line"><span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'FAIL,实际结果与预期结果不一致!'</span>, <span class="built_in">format</span>(e))</span><br><span class="line"></span><br><span class="line"><span class="comment"># sleep(1)</span></span><br><span class="line">driver.quit()</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<summary type="html"><link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla</summary>
<category term="selenium" scheme="http://example.com/categories/selenium/"/>
</entry>
<entry>
<title>Selenium的CSS层叠样式表选择器(一)</title>
<link href="http://example.com/2024/12/15/selenium/selenium02/"/>
<id>http://example.com/2024/12/15/selenium/selenium02/</id>
<published>2024-12-15T08:31:01.603Z</published>
<updated>2024-12-15T10:55:44.468Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p><strong>Selenium的CSS层叠样式表选择器(一)</strong></p><p><strong>实验目的:</strong>了解在selenium中可以通过CSS Selector选择界面元素,重点掌握通过选择器根据id、class 、tag这三个常用属性选择元素的语法,以及通过父子元素进行选择的方法。了解CSS 选择器支持的通过任何属性来选择元素的用法。</p><p><strong>实验要求:</strong>在pycharm 环境下完成实验目的中所述各项任务 </p><p><strong>实验条件:</strong>win7/10、pycharm、selenium4.4.0 </p><p><strong>实验内容及步骤:</strong></p><p>HTML中经常要为某些元素指定显示效果,比如:前景文字颜色是红色,背景颜色是黑色,字体是微软雅黑等。用来表现HTML样式的计算机语言就是CSS(Cascading Style Sheets)层叠样式表。CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。于是,在执行WEB界面自动化测试开发时,除了前面讲的直接使用id、class属性、tag名来选择元素外,Selenium还专门提供了CSS Selector语法选择元素。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">find_element(By.CSS_SELECTOR, CSS Selector参数)</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">find_elements(By.CSS_SELECTOR, CSS Selector参数)</span><br></pre></td></tr></table></figure><p>一、 根据tag名选择元素的语法:直接写tag名</p><p>如果要选择所有的tag名为div的元素,则应写为: </p><p><code>find_element(By.CSS_SELECTOR, 'div')</code></p><p>等价于 <code>find_element(By.TAG_NAME, 'div')</code></p><p>二、 根据id属性选择元素的语法:在id号前面加上一个#号(#id值)</p><p>例如:一个输入框的Html元素定义如下:</p><input id="kw" name="wd" class="s_ipt" value maxlength="255" autocomplete="off"><p>想要根据id属性选择这个输入框,则应写为:</p><p><code>find_element(By.CSS_SELECTOR, '#kw')</code></p><p>等价于 <code>find_element(By.ID, 'kw')</code></p><p>三、 根据class属性选择元素的语法:在class值前面加上一个点(.class值)</p><p>想要根据class属性选择上面的输入框,则应写为:</p><p><code>find_element(By.CSS_SELECTOR, '.s_ipt')</code></p><p>等价于 <code>find_element(By.CLASS_NAME, 's_ipt')</code></p><p>四、 通过父子元素选择</p><p>HTML中,元素内部可以包含其他元素,具有层级关系。</p><p>如果元素2是元素1的直接子元素,要选择元素2时, CSS Selector 选择子元素的语法是:元素1 >元素2(中间用大于号可理解为箭头)。</p><p>也支持更多层级的选择: 元素1 >元素2 >元素3 >元素4 (选择的是元素4 )</p><p>如果元素2是元素1的后代元素,要选择元素2时, CSS Selector 选择后代元素的语法是: <code>元素1 元素2</code>(中间是一个或者多个空格隔开),同样支持更多层级的选择:</p><p><code>元素1 元素2 元素3 元素4</code></p><p>五、 通过任何属性来选择元素</p><p>语法是用一个方括号 [ ] 括起属性及取值,外部用单引号或双引号都可以,但要与方括号内部的引号区分开,多个属性时用多个 [ ] 连接在一起。</p><p>例如:一个超链接的Html元素定义如下:</p><p><a href="http://www.miitbeian.gov.cn">苏ICP备88885574号</a></p><p>想要利用href属性来选择a元素,应写为:</p><p><code>find_element(By.CSS_SELECTOR, '[href]')</code></p><p>也可通过选择属性href值等于 <a href="http://www.miitbeian.gov.cn/">http://www.miitbeian.gov.cn</a> 找到与上面相同的元素,具体写作: <code>find_element(By.CSS_SELECTOR, '[href="http://www.miitbeian.gov.cn"]')</code></p><p>CSS Selector可以选择属性值包含某个字符串的元素:语法(*=)</p><p>选择上面的超链接元素可写为:<code>find_element(By.CSS_SELECTOR, '[href*="miitbeian"]')</code></p><p>CSS Selector可以选择属性值以某个字符串开头的元素:语法(^=)</p><p>选择上面的超链接元素可写为:<code>find_element(By.CSS_SELECTOR, '[href^="http"]')</code></p><p>CSS Selector可以选择属性值以某个字符串结尾的元素:语法($=)</p><p>选择上面的超链接元素可写为:<code>find_element(By.CSS_SELECTOR, '[href$="gov.cn"]')</code></p><p>CSS Selector指定选择的元素要同时具有多个属性的限制时,要将属性连在一起写。</p><p>例如:某Html元素定义为:<code><div class="misc" ctype="gun">沙漠之鹰</div></code></p><p>想要选择此元素应写为:</p><p><code>find_element(By.CSS_SELECTOR, 'div[class="misc"][ctype="gun"]')</code></p><p>注意多个属性之间不能加空格,否则就变成了父子关系。</p><p>六、 验证 CSS Selector 语法的正确性</p><p>在python代码中使用print输出语句并运行,可以验证 CSS Selector 的语法是否正确选择了我们要选择的元素,但每次选择都进行这样的验证非常耗时。</p><p>由于 CSS Selector 是浏览器直接支持的,一个简便的方法是:在浏览器开发者工具栏中进行验证。在开发者工具栏,点击 Elements 元素标签后,同时按 Ctrl 键 和 F 键,就会出现一个查询框,在其中输入任何 CSS Selector 表达式,如果能选择到元素,输入框右边会显示出类似 1 of 1 (第几个of共选择到几个)这样的内容,且选中的元素高亮显示。若选择到的元素有多个,第一个被高亮显示。</p><p>同学们可以使用这个方法,加速编程。同时在熟练应用后,努力做到编写的CSS Selector 表达式简洁且选中的元素唯一。</p><p> 练习1:要求如下</p><p>在<a href="https://cdn2.byhy.net/files/selenium/sample1.html">https://cdn2.byhy.net/files/selenium/sample1.html</a> 网页中,使用CSS Selector ;</p><p>①根据tag名选择此页中的输入框并输入“查看完毕!”,</p><p>②根据id属性选择此页面底部的版权信息并输出,</p><p>③选择此页中所有class属性值为plant的元素并依次输出。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># import time</span></span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.common.by <span class="keyword">import</span> By</span><br><span class="line"></span><br><span class="line">driver = webdriver.Chrome()</span><br><span class="line"></span><br><span class="line">driver.get(<span class="string">"https://cdn2.byhy.net/files/selenium/sample1.html"</span>)</span><br><span class="line">searchtext = driver.find_element(By.CSS_SELECTOR, <span class="string">'#searchtext'</span>)</span><br><span class="line">searchtext.send_keys(<span class="string">'查看完毕!'</span>)</span><br><span class="line"><span class="comment"># time.sleep(2)</span></span><br><span class="line">copyright = driver.find_element(By.CSS_SELECTOR, <span class="string">'.copyright'</span>)</span><br><span class="line"><span class="built_in">print</span>(copyright.text)</span><br><span class="line">date = driver.find_element(By.CSS_SELECTOR, <span class="string">'.date'</span>)</span><br><span class="line"><span class="built_in">print</span>(date.text)</span><br><span class="line">banben = driver.find_element(By.CSS_SELECTOR, <span class="string">'#bottom > div.footer2 > span'</span>)</span><br><span class="line"><span class="built_in">print</span>(banben.text)</span><br><span class="line">elements = driver.find_elements(By.CSS_SELECTOR,<span class="string">'.plant'</span>)</span><br><span class="line"><span class="keyword">for</span> element <span class="keyword">in</span> elements:</span><br><span class="line"> <span class="built_in">print</span>(element.text)</span><br></pre></td></tr></table></figure><p> 练习2:要求如下</p><p>打开2345天气王网站<a href="https://tianqi.2345.com/%EF%BC%8C%E8%8E%B7%E5%8F%96%E7%BD%91%E9%A1%B5%E4%B8%AD%E5%BD%93%E5%89%8D%E5%9F%8E%E5%B8%82%E5%90%8D%E7%A7%B0%EF%BC%8C%E6%9F%A5%E7%9C%8B%E4%BB%8A%E5%A4%A9%E4%BB%A5%E5%8F%8A%E4%B9%8B%E5%90%8E%E4%B8%80%E5%91%A8%E5%86%85%E7%9A%84%E5%A4%A9%E6%B0%94%E6%83%85%E5%86%B5%EF%BC%8C%E8%BE%93%E5%87%BA%E5%BD%93%E5%89%8D%E5%9F%8E%E5%B8%82%E4%B8%80%E5%91%A8%E5%86%85%E7%9A%84%E6%9C%80%E9%AB%98%E6%B0%94%E6%B8%A9%E5%92%8C%E6%9C%80%E4%BD%8E%E6%B0%94%E6%B8%A9%EF%BC%8C%E8%BE%93%E5%87%BA%E6%A0%BC%E5%BC%8F%E4%B8%BA%EF%BC%9A">https://tianqi.2345.com/,获取网页中当前城市名称,查看今天以及之后一周内的天气情况,输出当前城市一周内的最高气温和最低气温,输出格式为:</a></p><p><code>AAA(当前城市名称)一周内最高气温为XX摄氏度,最低气温为YY摄氏度。</code></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.common.by <span class="keyword">import</span> By</span><br><span class="line"></span><br><span class="line">driver = webdriver.Chrome()</span><br><span class="line">driver.get(<span class="string">"https://tianqi.2345.com/"</span>)</span><br><span class="line">didian = driver.find_element(By.CSS_SELECTOR,<span class="string">'body > div.banner > div.w1200.clearfix > div > div.banner-city > div > a:nth-child(1) > span'</span>)</span><br><span class="line">time.sleep(<span class="number">2</span>)</span><br><span class="line"><span class="built_in">print</span>(didian.text)</span><br><span class="line"></span><br><span class="line">weather_list = driver.find_elements(By.CSS_SELECTOR, <span class="string">'.banner-right-con-list-temp'</span>)</span><br><span class="line">arr1 = []</span><br><span class="line"><span class="keyword">for</span> element <span class="keyword">in</span> weather_list:</span><br><span class="line"> <span class="comment"># print(element.text)</span></span><br><span class="line"> element1 = re.sub(<span class="string">'([^\u0030-\u0039\'-])'</span>, <span class="string">' '</span>, element.text)</span><br><span class="line"> element3 = element1.rstrip()</span><br><span class="line"> <span class="comment"># print(element1)</span></span><br><span class="line"> str_list = element3.split(<span class="string">" "</span>)</span><br><span class="line"> <span class="keyword">for</span> arr3 <span class="keyword">in</span> str_list:</span><br><span class="line"> arr1.append(arr3)</span><br><span class="line">arr2 = <span class="built_in">map</span>(<span class="built_in">int</span>, arr1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取最高气温和最低气温</span></span><br><span class="line">high_temps = []</span><br><span class="line">low_temps = []</span><br><span class="line"><span class="keyword">for</span> element2 <span class="keyword">in</span> arr2:</span><br><span class="line"> high_temps.append(element2)</span><br><span class="line"> low_temps.append(element2)</span><br><span class="line"> <span class="comment"># print(element2)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 输出结果</span></span><br><span class="line">max_temp = <span class="built_in">max</span>(high_temps)</span><br><span class="line">min_temp = <span class="built_in">min</span>(low_temps)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f'<span class="subst">{didian.text}</span>一周内最高气温为<span class="subst">{max_temp}</span>摄氏度,最低气温为<span class="subst">{min_temp}</span>摄氏度。'</span>)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<summary type="html"><link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla</summary>
<category term="selenium" scheme="http://example.com/categories/selenium/"/>
</entry>
<entry>
<title>初识Selenium和设置被测系统</title>
<link href="http://example.com/2024/12/15/selenium/selenium00/"/>
<id>http://example.com/2024/12/15/selenium/selenium00/</id>
<published>2024-12-15T07:39:24.305Z</published>
<updated>2024-12-15T10:54:31.373Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="初识Selenium和设置被测系统"><a href="#初识Selenium和设置被测系统" class="headerlink" title="初识Selenium和设置被测系统"></a><strong>初识Selenium和设置被测系统</strong></h2><p><strong>实验目的:</strong>了解Web功能测试的基本操作过程,掌握按照运行selenium的方法,能在PyCharm环境下使用selenium操纵浏览器执行简单操作,安装并熟悉被测系统软件.</p><p><strong>实验要求:</strong>在pycharm 环境下完成实验目的中所述各项任务 .</p><p><strong>实验条件:</strong>win10、pycharm、selenium4.4.0 </p><p><strong>实验内容及步骤:</strong></p><p>Selenium是一个用于Web<a href="https://baike.baidu.com/item/%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F/5985445?fromModule=lemma_inlink">应用程序</a>测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括<a href="https://baike.baidu.com/item/IE/69103?fromModule=lemma_inlink">IE</a>(7, 8, 9, 10, 11),[Mozilla Firefox](<a href="https://baike.baidu.com/item/Mozilla">https://baike.baidu.com/item/Mozilla</a> Firefox/3504923?fromModule=lemma_inlink),<a href="https://baike.baidu.com/item/Safari/597?fromModule=lemma_inlink">Safari</a>,<a href="https://baike.baidu.com/item/Google/86964?fromModule=lemma_inlink">Google</a> <a href="https://baike.baidu.com/item/Chrome/5633839?fromModule=lemma_inlink">Chrome</a>,<a href="https://baike.baidu.com/item/Opera/25140?fromModule=lemma_inlink">Opera</a>,<a href="https://baike.baidu.com/item/Edge/16194485?fromModule=lemma_inlink">Edge</a>等。这个工具的主要功能包括:测试与浏览器的<a href="https://baike.baidu.com/item/%E5%85%BC%E5%AE%B9%E6%80%A7/1446869?fromModule=lemma_inlink">兼容性</a>——测试应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试<a href="https://baike.baidu.com/item/%E7%B3%BB%E7%BB%9F%E5%8A%9F%E8%83%BD/10394740?fromModule=lemma_inlink">系统功能</a>——创建回归测试检验软件功能和<a href="https://baike.baidu.com/item/%E7%94%A8%E6%88%B7%E9%9C%80%E6%B1%82/56497046?fromModule=lemma_inlink">用户需求</a>。支持自动录制动作和自动生成.Net、<a href="https://baike.baidu.com/item/Java/85979?fromModule=lemma_inlink">Java</a>、<a href="https://baike.baidu.com/item/Perl/851577?fromModule=lemma_inlink">Perl</a>等不同语言的<a href="https://baike.baidu.com/item/%E6%B5%8B%E8%AF%95%E8%84%9A%E6%9C%AC/10921546?fromModule=lemma_inlink">测试脚本</a>。</p><p>一、 Selenium环境配置步骤:</p><ol><li>正常配置Python环境 Pycharm</li><li>安装selenium → 在Pycharm中新建一个虚拟项目,在Terminal中输入<code>pip install selenium==4.4.0</code> 实现selenium的安装,使用pip show selenium 验证安装是否成功。</li><li>浏览器驱动的下载与配置 → 以谷歌浏览器为例:</li></ol><p>下载谷歌浏览器: <a href="https://www.google.cn/chrome/">https://www.google.cn/chrome/</a></p><p>下载谷歌浏览器驱动: <a href="https://chromedriver.storage.googleapis.com/index.html">https://chromedriver.storage.googleapis.com/index.html</a></p><p>注意:下载的浏览器驱动一定要与浏览器版本号(最近似)匹配,记录浏览器驱动文件的存放地址(可以放到磁盘根目录中,方便在脚本中输入完整路径)。</p><ol start="4"><li>在代码中import 对应的依赖</li></ol><p>二、 Selenium对浏览器的操控:</p><ol><li>在pycharm已建立的项目中新建一个webauto.py文件,并运行观察selenium对浏览器的控制</li></ol><p><img src="/img/selenium/se00-00.png" alt="Selenium对浏览器的操控"></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 导入 selenium 包</span></span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个webdriver对象,即将Chromedriver 实例化。</span></span><br><span class="line"><span class="comment"># Chrome() 函数将按照指定的内容寻找浏览器驱动</span></span><br><span class="line">driver = webdriver.Chrome(<span class="string">"D:\\chromedriver.exe"</span>)</span><br><span class="line"><span class="comment"># 上面改成你的chromedriver.exe的存放路径</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 打开指定网页 比如打开百度网页</span></span><br><span class="line">driver.get(<span class="string">"https://www.baidu.com"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 关闭 driver</span></span><br><span class="line">driver.quit()</span><br><span class="line"></span><br></pre></td></tr></table></figure><ol start="2"><li>运行其它webdriver对象可执行的浏览器窗口操作及导航操作:</li></ol><p>获取当前页面的url:<code>driver.current_url</code></p><p>获取当前页面的title:<code>driver.title</code></p><p>窗口操作:设置窗口大小 <code>driver.set_window_size(1920,1080)</code></p><p>窗口最小化 <code>driver.minimize_window()</code></p><p>窗口最大化 <code>driver.maximize_window()</code></p><p>全屏窗口 <code>driver.fullscreen_window()</code></p><p>导航操作:页面返回 <code>driver.back()</code></p><p>页面前进 <code>driver.forward()</code></p><p>页面刷新 <code>driver.refresh()</code></p><p>窗口位置操作:设置窗口位置坐标 <code>driver.set_window_position(x=500, y=400)</code></p><p> 设置窗口坐标及宽度和高度 <code>driver.set_window_rect(x=30, y=30, width=450, height=450)</code></p><p> 获取当前窗口位置坐标 <code>driver.get_window_position()</code></p><p> 获取当前窗口的长和宽 <code>driver.get_window_size()</code></p><p>关闭窗口操作:<code>driver.close()</code> 关闭当前窗口,或最后打开的窗口</p><p> <code>driver.quit()</code> 关闭所有关联窗口,并且安全关闭session</p><p>三、 安装并熟悉被测系统软件</p><ol><li><p>解压 bysms.zip 后,进入 bysms目录,双击运行 runserver.bat 即可启动。</p></li><li><p>浏览器访问 登录页面 <a href="http://127.0.0.1:8047/mgr/sign.html">http://127.0.0.1:8047/mgr/sign.html</a></p></li><li><p>输入管理员账号登录——用户名 :byhy 密码: 88888888</p></li></ol><p>练习:Selenium对浏览器的操控</p><h6 id="提交代码:"><a href="#提交代码:" class="headerlink" title="提交代码:"></a>提交代码:</h6><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.chrome.service <span class="keyword">import</span> Service</span><br><span class="line"></span><br><span class="line">path = Service(<span class="string">r"C:\\chromedriver.exe"</span>)</span><br><span class="line">driver = webdriver.Chrome(service=path)</span><br><span class="line">driver.get(<span class="string">"https://www.baidu.com"</span>)</span><br><span class="line">driver.set_window_size(<span class="number">1920</span> ,<span class="number">1080</span>)</span><br><span class="line">driver.minimize_window()</span><br><span class="line">driver.maximize_window()</span><br><span class="line">driver.fullscreen_window()</span><br><span class="line">driver.back()</span><br><span class="line">driver.forward()</span><br><span class="line">driver.refresh()</span><br><span class="line">driver.set_window_position(x=<span class="number">500</span>, y=<span class="number">400</span>)</span><br><span class="line">driver.set_window_rect(x=<span class="number">30</span>, y=<span class="number">30</span>, width=<span class="number">450</span>, height=<span class="number">450</span>)</span><br><span class="line">driver.get_window_position()</span><br><span class="line">driver.get_window_size()</span><br><span class="line">driver.close()</span><br><span class="line">driver.quit()</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html"><link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla</summary>
<category term="selenium" scheme="http://example.com/categories/selenium/"/>
</entry>
<entry>
<title>操控元素的基本方法</title>
<link href="http://example.com/2024/12/15/selenium/selenium01/"/>
<id>http://example.com/2024/12/15/selenium/selenium01/</id>
<published>2024-12-15T07:29:50.467Z</published>
<updated>2024-12-15T10:54:38.550Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="selenium操控元素的基本方法"><a href="#selenium操控元素的基本方法" class="headerlink" title="selenium操控元素的基本方法"></a><strong>selenium操控元素的基本方法</strong></h2><p>**实验目的:**了解在 selenium中选择界面元素后操控的基础方法,能在PyCharm环境下使用sclenium 中 WebElement的相关函数实现元紊的.<br>**基本操控:**元素鼠标左键点击、输入框传入文本及清空文本、获取元素文本内容、获取元素各种属性值.</p><p>**实验要求:**在pycharm环境下完成实验目的中所述各项任务.</p><p><strong>实验条件</strong>: win7/10、pycharm. selenium4.4.0 .</p><p>操控元素通常包括:</p><ul><li><code>点击元素</code></li><li><code>在元素中输入字符串,通常是对输入框这样的元素</code></li><li><code>获取元素包含的信息,比如文本内容,元素的属性</code></li></ul><h4 id="点击元素"><a href="#点击元素" class="headerlink" title="点击元素"></a>点击元素</h4><p><code>点击元素</code> 顾名思义,通过调用元素WebElement对象的click()方法,来实现点击这个动作。在调用方法的时候,浏览器收到自动化命令,默认点击所选元素的中间位置</p><!-- ![selenium选择元素](/img/selenium01.jpg) --><p><img src="/img/selenium/se01-00.png" alt="selenium选择元素"></p><p>我们要点击 <code>更多</code> 这个按钮,相当于点击右边这条相关链接。</p><h4 id="输入框"><a href="#输入框" class="headerlink" title="输入框"></a>输入框</h4><p>要在输入框中输入字符串,需要调用元素WebElement对象的<code>send_keys()</code>方法。如果输入框中存在字符串,可以使用WebElement对象的clear()方法来将内容清除。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">element.clear() <span class="comment"># 清除已有字符串</span></span><br><span class="line">element.send_keys(<span class="string">'新字符串'</span>) <span class="comment"># 输入新字符串</span></span><br></pre></td></tr></table></figure><h4 id="获取元素的信息"><a href="#获取元素的信息" class="headerlink" title="获取元素的信息"></a>获取元素的信息</h4><ul><li><code>获取元素的文本内容</code></li><li><code>获取元素属性</code></li><li><code>获取整个元素对应的HTML</code></li><li><code>获取输入框里面的文字</code></li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 通过WebElement对象的text属性获取文本内容</span></span><br><span class="line">element = wd.find_element(By.ID, <span class="string">'ID值'</span>)</span><br><span class="line"><span class="built_in">print</span>(element.text)</span><br><span class="line"><span class="comment"># 若文本内容没有完全展示在界面上,可以使用下面两种方式</span></span><br><span class="line">element.get_attribute(<span class="string">'innerText'</span>)</span><br><span class="line">element.get_attribute(<span class="string">'textContent'</span>)</span><br><span class="line"><span class="comment"># 区别在于 innerText只显示元素可见文本内容,textContent显示所有内容</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 通过WebElement对象的get_attribute方法来获取元素的属性值</span></span><br><span class="line">element = wd.find_element(By.ID. <span class="string">'ID值'</span>)</span><br><span class="line"><span class="built_in">print</span>(element.get_attribute(<span class="string">'class'</span>))</span><br><span class="line"><span class="comment"># 执行完自动化代码后,可以使用WebDriver对象的quit()方法关闭浏览器窗口</span></span><br><span class="line">wd.quit()</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 要获取整个元素对应的HTML文本内容,可以使用 </span></span><br><span class="line">element.get_attribute(<span class="string">'outerHTML'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果,只是想获取某个元素 内部 的HTML文本内容,可以使用 </span></span><br><span class="line">element.get_attribute(<span class="string">'innerHTML'</span>)</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 获取输入框里面的文字</span></span><br><span class="line">element = wd.find_element(By.ID, <span class="string">'ID值'</span>)</span><br><span class="line"><span class="built_in">print</span>(element.get_attribute(<span class="string">'value'</span>)) </span><br></pre></td></tr></table></figure><p><strong>实验内容及步骤:</strong></p><p>在执行WEB界面自动化测试开发中,元素的定位与操作是基础,也可以说学习元素选择方法的目的就是来操控网页上的元素,最基本的操控从鼠标点击操作开始。</p><p>一、对元素执行鼠标左键点击操作: <code>click()</code>方法。</p><p>1.在 <a href="https://www.51job.com/">https://www.51job.com/</a> 网站中,点击搜索栏中的添加工作地点的元素,将会弹出选择地区的新界面,在此界面中,检查是否有已被选中的地区,如果有,依次进行删除操作,然后点击“上海”,确定选择.使用<code>qiut()</code>关闭浏览器。</p><p>二、对元素执行获取在界面上显示的文本内容: text属性。</p><p>text属性只能获取元素展示在界面上的文本内容,即处于一对标签之间的文本。</p><p>2.在上个操作步骤的关闭浏览器之前,添加: 获取网站显示语言的元素的文本内容,如果为“简”则输出:“本网站现以简体中文版显示”,如果为“EN”需输出:“ 本网站现以英文版显示 ”。</p><p>三、对元素执行输入框中传入文本、清空文本操作: <code>sent _keys(str)</code>方法和 <code>clear()</code>方法。</p><p>调用元素WebElement对象的 <code>send_keys(str)</code>方法完成字符串的输入,输入前可以使用WebElement对象的<code>clear()</code>方法清除输入框中己经有的内容,其中 str为String类型的文本。</p><p>3.在上个操作步骤的关闭浏览器之前,添加: 清空搜索输入框,再向输入框中传入“python”文本。</p><p>四、对元紊执行获取元素的多种属性值: <code>get_attribute()</code>方法。</p><ul><li><p>获取元素的某种属性值时,设置<code>get _attribute()</code>方法的参数为属性名称,注意以字符串方式传参(如:’ style’) 。</p></li><li><p>获取输入框元素的文本时,没置<code>get_attribute()</code>方法的参数为’value’。</p></li><li><p>获取整个元素对应的HTML文本内容,设置get_attribute()方法的参数为’<code>outerHTML</code>‘,获取某个元素内部的HTML文本内容,设置<code>get _ attribute()</code>方法的参数为’<code>innerHTML</code>‘。</p></li><li><p>对于没有展示在界面上,或者没有完全展示在界面上的元素文本内容,用WebElement对象的 text属性,获取文本内容,就会有问题。这时,需要尝试使用<code>element.get_ attribute('innerText')</code>,或者 <code>element.get_attribute('textContent')</code>来完成元素文本内容的获取。其中,<code>innerText</code>只显示元可见文本内容,<code>textContent</code>显示元素中所有文本内容(包括display属性为none的部分)。</p></li></ul><p>4、在上个操作步骤的关闭浏览器之前,添加:</p><ul><li>获取并输出“扫码登录”的二维码资源(src)信息;</li><li>获取输入框中的文本信息,判定是否为“python”,并输出检查结果;</li><li>点击搜索按钮,将搜索到的第一条信息对应的全部HTML文本内容抓取并输出;</li><li>点击“最新优先”选项卡,将此时搜索到的第一条信息对应的可见文本内容抓取并输出;</li><li>点击“薪资优先”选项卡,将此时搜索到的第一条信息对应的所有文本内容抓取并输出;</li></ul><p>本课要求:编写以下测试用例</p><p>1.完成上述四个实验步骤(1,2,3,4)</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.common.by <span class="keyword">import</span> By</span><br><span class="line"></span><br><span class="line">driver = webdriver.Chrome()</span><br><span class="line">driver.get(<span class="string">'https://www.51job.com'</span>)</span><br><span class="line">driver.find_element(By.CLASS_NAME, <span class="string">'fltr.radius_5'</span>).find_element(By.ID, <span class="string">'work_position_click'</span>).click()</span><br><span class="line"></span><br><span class="line">address_element = driver.find_element(By.ID, <span class="string">'work_position_click_multiple'</span>)</span><br><span class="line">spans = address_element.find_elements(By.TAG_NAME, <span class="string">'span'</span>)</span><br><span class="line"><span class="keyword">for</span> span <span class="keyword">in</span> spans:</span><br><span class="line"> span.click()</span><br><span class="line"></span><br><span class="line">driver.find_element(By.ID, <span class="string">'work_position_click_center_right_list_category_000000_020000'</span>).click()</span><br><span class="line"></span><br><span class="line">driver.find_element(By.ID, <span class="string">'work_position_click_bottom_save'</span>).click()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">showtext = driver.find_element(By.ID, <span class="string">'languagelist'</span>).find_element(By.CLASS_NAME, <span class="string">'tle'</span>).text</span><br><span class="line"><span class="keyword">if</span> showtext == <span class="string">"简"</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"本网站现以简体中文版显示\n"</span>)</span><br><span class="line"><span class="keyword">elif</span> showtext == <span class="string">"EN"</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"本网站现以英文版显示\n"</span>)</span><br><span class="line"></span><br><span class="line">input_element = driver.find_element(By.ID, <span class="string">'kwdselectid'</span>)</span><br><span class="line">input_element.clear()</span><br><span class="line">input_element.send_keys(<span class="string">'python'</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">"二维码资源为:\n"</span>+driver.find_element(By.ID, <span class="string">'qrimg'</span>).get_attribute(<span class="string">'src'</span>)+<span class="string">'\n'</span>)</span><br><span class="line"></span><br><span class="line">input_text = driver.find_element(By.ID, <span class="string">'kwdselectid'</span>).get_attribute(<span class="string">'value'</span>)</span><br><span class="line"><span class="keyword">if</span> input_text != <span class="string">'python'</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"输入查询内容有误!\n"</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"输入查询内容正确!\n"</span>)</span><br><span class="line"></span><br><span class="line">driver.find_element(By.CLASS_NAME, <span class="string">'top_wrap'</span>).find_element(By.TAG_NAME, <span class="string">'button'</span>).click()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">time.sleep(<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(driver.find_element(By.CLASS_NAME, <span class="string">'j_result'</span>).find_element(By.TAG_NAME, <span class="string">'div'</span>).get_attribute(<span class="string">'outerHTML'</span>)+<span class="string">'\n'</span>)</span><br><span class="line">elements = driver.find_element(By.CLASS_NAME, <span class="string">'j_tlc'</span>).find_elements(By.TAG_NAME, <span class="string">'span'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> element <span class="keyword">in</span> elements:</span><br><span class="line"> <span class="keyword">if</span> element.text == <span class="string">'最新优先'</span>:</span><br><span class="line"> element.click()</span><br><span class="line"> <span class="built_in">print</span>(</span><br><span class="line"> driver.find_element(By.CLASS_NAME, <span class="string">'j_result'</span>).find_element(By.TAG_NAME, <span class="string">'div'</span>).get_attribute(<span class="string">'innerText'</span>))</span><br><span class="line"> <span class="keyword">elif</span> element.text == <span class="string">'薪资优先'</span>:</span><br><span class="line"> element.click()</span><br><span class="line"> <span class="built_in">print</span>(</span><br><span class="line"> driver.find_element(By.CLASS_NAME, <span class="string">'j_result'</span>).find_element(By.TAG_NAME, <span class="string">'div'</span>).get_attribute(<span class="string">'textContent'</span>))</span><br><span class="line">driver.quit()</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>2.以管理员身份登录http:1/127.0.0.1:8047/mgr/sign.html,用户名 :byhy 密码:88888888。点击添加客户,输入客户名为“南京中医院”的客户,填写客户基本信息。预期结果为:成功登录后,检查客户列表第一项结果中客户名、电话、地址信息, 都是正确的。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.common.by <span class="keyword">import</span> By</span><br><span class="line"></span><br><span class="line">driver = webdriver.Chrome()</span><br><span class="line"></span><br><span class="line">driver.get(<span class="string">"http://127.0.0.1:8047/mgr/sign.html"</span>)</span><br><span class="line"></span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="username"]'</span>).send_keys(<span class="string">"byhy"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="password"]'</span>).send_keys(<span class="string">"88888888"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'/html/body/div/div[2]/div[1]/div[3]/div/button'</span>).click()</span><br><span class="line">time.sleep(<span class="number">1</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[1]/button'</span>).click()</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[1]/div[1]/div[1]/input'</span>).send_keys(<span class="string">"南京中医院"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[1]/div[1]/div[2]/input'</span>).send_keys(<span class="string">"13888888888"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[1]/div[1]/div[3]/textarea'</span>).send_keys(<span class="string">"南京市"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[1]/div[2]/button[1]'</span>).click()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"通过"</span>)</span><br><span class="line">driver.quit()</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure><p>3.以管理员身份登录htp:1/127.0.0.1:8047/mgr/sign.html,用户名: byhy密码:88888888。点击添加客户,输入客户名为“南京中医院”的客户,填写客户基本信息;然后再点击编辑,修改客户名为:“南京省中医院”。预期结果为:成功登录后,检查客户列表第一项结果中客户名、电话、地址信息都是正确的(修改后的结果)。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.common.by <span class="keyword">import</span> By</span><br><span class="line"></span><br><span class="line">driver = webdriver.Chrome()</span><br><span class="line">driver.get(<span class="string">"http://127.0.0.1:8047/mgr/sign.html"</span>)</span><br><span class="line"></span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="username"]'</span>).send_keys(<span class="string">"byhy"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="password"]'</span>).send_keys(<span class="string">"88888888"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'/html/body/div/div[2]/div[1]/div[3]/div/button'</span>).click()</span><br><span class="line">time.sleep(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[1]/button'</span>).click()</span><br><span class="line">time.sleep(<span class="number">1</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[1]/div[1]/div[1]/input'</span>).send_keys(<span class="string">"南京中医院"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[1]/div[1]/div[2]/input'</span>).send_keys(<span class="string">"13888888888"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[1]/div[1]/div[3]/textarea'</span>).send_keys(<span class="string">"南京市"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[1]/div[2]/button[1]'</span>).click()</span><br><span class="line">time.sleep(<span class="number">1</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[2]/div/input'</span>).send_keys(<span class="string">"南京中医院"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[3]/div[4]/div/label[1]'</span>).click()</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[3]/div[1]/div[1]/input'</span>).send_keys(<span class="string">"南京省中医院"</span>)</span><br><span class="line">driver.find_element(By.XPATH,<span class="string">'//*[@id="root"]/div/section[2]/div[3]/div[2]/div/label[1]'</span>).click()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"通过"</span>)</span><br><span class="line">driver.quit()</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<summary type="html"><link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla</summary>
<category term="selenium" scheme="http://example.com/categories/selenium/"/>
</entry>
<entry>
<title>音乐页面</title>
<link href="http://example.com/2024/12/13/playlist/"/>
<id>http://example.com/2024/12/13/playlist/</id>
<published>2024-12-13T13:05:44.000Z</published>
<updated>2024-12-15T07:28:29.084Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><!-- 简单示例 (id, server, type) --><link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/APlayer.min.css"><!--APlayer的样式--><script src="https://unpkg.com/[email protected]/dist/APlayer.min.js"></script><!--APlayer的依赖--><script src="https://unpkg.com/meting@2/dist/Meting.min.js"></script><!--Meting的依赖--><meting-js server="netease" type="playlist" autoplay=false lrc-type=3 id="7893371839"> </meting-js> ]]></content>
<summary type="html"><link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla</summary>
<category term="音乐" scheme="http://example.com/categories/%E9%9F%B3%E4%B9%90/"/>
<category term="音乐页面" scheme="http://example.com/tags/%E9%9F%B3%E4%B9%90%E9%A1%B5%E9%9D%A2/"/>
</entry>
<entry>
<title>第一篇文章</title>
<link href="http://example.com/2024/12/09/first-title/"/>
<id>http://example.com/2024/12/09/first-title/</id>
<published>2024-12-09T13:36:46.000Z</published>
<updated>2024-12-16T03:58:00.488Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="标题一"><a href="#标题一" class="headerlink" title="标题一"></a>标题一</h1><h2 id="标题二"><a href="#标题二" class="headerlink" title="标题二"></a>标题二</h2><h3 id="标题三"><a href="#标题三" class="headerlink" title="标题三"></a>标题三</h3><h4 id="标题四"><a href="#标题四" class="headerlink" title="标题四"></a>标题四</h4><h5 id="标题五"><a href="#标题五" class="headerlink" title="标题五"></a>标题五</h5><p><strong>这是加粗</strong></p><p><em>这是斜体</em></p><ul><li>无序列表</li></ul><ol><li>有序列表</li></ol><p><code>内嵌代码</code></p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">代码块</span><br></pre></td></tr></table></figure><blockquote><p>引用</p></blockquote><style>body, div, h1,h2, form, fieldset, footer,p { margin: 0; padding: 0; border: 0; outline: none;}body { color: #7c7873; font-family: 'YanoneKaffeesatzRegular'; background-color: #D7D7D7;}p {text-shadow:0 1px 0 #fff;}#wrap {width:530px; margin:20px auto 0; }h1 {margin-bottom:20px; text-align:center;font-size:48px; text-shadow:0 1px 0 #ede8d9; } #form_wrap { overflow:hidden; height:446px; position:relative; top:0px; -webkit-transition: all 1s ease-in-out .3s; -moz-transition: all 1s ease-in-out .3s; -o-transition: all 1s ease-in-out .3s; transition: all 1s ease-in-out .3s;} #form_wrap:before {content:""; position:absolute; bottom:128px;left:0px; background:url('https://uipv4.zywvvd.com:33030/HexoFiles/images/before.png'); width:530px;height: 316px;} #form_wrap:after {content:"";position:absolute; bottom:0px;left:0; background:url('https://uipv4.zywvvd.com:33030/HexoFiles/images/after.png'); width:530px;height: 260px; } #form_wrap.hide:after, #form_wrap.hide:before {display:none; } #form_wrap:hover {height:806px;top:-30px;} form {background:#f7f2ec url('https://uipv4.zywvvd.com:33030/HexoFiles/images/letter_bg.png'); position:relative;top:200px;overflow:hidden; height:200px;width:400px;margin:0px auto;padding:20px; border: 1px solid #fff; border-radius: 3px; -moz-border-radius: 3px; -webkit-border-radius: 3px; box-shadow: 0px 0px 3px #9d9d9d, inset 0px 0px 27px #fff; -moz-box-shadow: 0px 0px 3px #9d9d9d, inset 0px 0px 14px #fff; -webkit-box-shadow: 0px 0px 3px #9d9d9d, inset 0px 0px 27px #fff; -webkit-transition: all 1s ease-in-out .3s; -moz-transition: all 1s ease-in-out .3s; -o-transition: all 1s ease-in-out .3s; transition: all 1s ease-in-out .3s;} #form_wrap:hover form {height:530px;} label { margin: 11px 20px 0 0; font-size: 16px; color: #b3aba1; text-transform: uppercase; text-shadow: 0px 1px 0px #fff; } #form_wrap input[type=submit] { position:relative;font-family: 'YanoneKaffeesatzRegular'; font-size:24px; color: #7c7873;text-shadow:0 1px 0 #fff; width:100%; text-align:center;opacity:0; background:none; cursor: pointer; -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; -webkit-transition: opacity .6s ease-in-out 0s; -moz-transition: opacity .6s ease-in-out 0s; -o-transition: opacity .6s ease-in-out 0s; transition: opacity .6s ease-in-out 0s; } #form_wrap:hover input[type=submit] {z-index:1;opacity:1; -webkit-transition: opacity .5s ease-in-out 1.3s; -moz-transition: opacity .5s ease-in-out 1.3s; -o-transition: opacity .5s ease-in-out 1.3s; transition: opacity .5s ease-in-out 1.3s;} #form_wrap:hover input:hover[type=submit] {color:#435c70;} .top-banner { background-color: #555; }</style><div style="margin-top: -10px"> <div id="wrap"> <div id='form_wrap'> <form> <img src='https://uipv4.zywvvd.com:33030/HexoFiles/images/violet.jpg' height=245px/> <p >Hello ,</p> <label for="email"> </label> 一起见证奇迹吧!!! </div> </div></div><script src="http://www.jq22.com/jquery/1.7.2/jquery.min.js"></script>]]></content>
<summary type="html"><link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla</summary>
<category term="test" scheme="http://example.com/categories/test/"/>
<category term="第一篇文章" scheme="http://example.com/tags/%E7%AC%AC%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0/"/>
</entry>
<entry>
<title>Hexo 使用指南</title>
<link href="http://example.com/2024/12/09/hello-world/"/>
<id>http://example.com/2024/12/09/hello-world/</id>
<published>2024-12-09T13:28:54.024Z</published>
<updated>2024-12-12T01:35:46.029Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p><h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p><h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p><h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p><h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>]]></content>
<summary type="html"><link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla</summary>
</entry>
</feed>