<pre class="highlight python" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"><span class="c" style="color: rgb(153, 153, 136); font-style: italic;"># coding:utf8</span>
<span class="kn" style="font-weight: bold;">import</span> <span class="nn" style="color: rgb(85, 85, 85);">logging</span>
<span class="kn" style="font-weight: bold;">import</span> <span class="nn" style="color: rgb(85, 85, 85);">histsimilar</span>
<span class="kn" style="font-weight: bold;">import</span> <span class="nn" style="color: rgb(85, 85, 85);">traceback</span>
<span class="kn" style="font-weight: bold;">import</span> <span class="nn" style="color: rgb(85, 85, 85);">time</span>
<span class="kn" style="font-weight: bold;">import</span> <span class="nn" style="color: rgb(85, 85, 85);">os</span>
<span class="kn" style="font-weight: bold;">from</span> <span class="nn" style="color: rgb(85, 85, 85);">selenium</span> <span class="kn" style="font-weight: bold;">import</span> <span class="n">webdriver</span>
<span class="kn" style="font-weight: bold;">from</span> <span class="nn" style="color: rgb(85, 85, 85);">selenium.common.exceptions</span> <span class="kn" style="font-weight: bold;">import</span> <span class="n">NoSuchElementException</span><span class="p">,</span> <span class="n">WebDriverException</span>
<span class="n">logging</span><span class="o" style="font-weight: bold;">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb" style="color: rgb(0, 134, 179);">file</span><span class="n">name</span><span class="o" style="font-weight: bold;">=</span><span class="s" style="color: rgb(221, 17, 68);">'log.txt'</span><span class="p">,</span><span class="n">level</span><span class="o" style="font-weight: bold;">=</span><span class="n">logging</span><span class="o" style="font-weight: bold;">.</span><span class="n">INFO</span><span class="p">)</span>
<span class="n">success</span> <span class="o" style="font-weight: bold;">=</span> <span class="bp" style="color: rgb(153, 153, 153);">True</span>
<span class="n">desired_caps</span> <span class="o" style="font-weight: bold;">=</span> <span class="nb" style="color: rgb(0, 134, 179);">dict</span><span class="p">()</span>
<span class="n">desired_caps</span><span class="p">[</span><span class="s" style="color: rgb(221, 17, 68);">'browserName'</span><span class="p">]</span> <span class="o" style="font-weight: bold;">=</span> <span class="s" style="color: rgb(221, 17, 68);">'iOS'</span>
<span class="n">desired_caps</span><span class="p">[</span><span class="s" style="color: rgb(221, 17, 68);">'platform'</span><span class="p">]</span> <span class="o" style="font-weight: bold;">=</span> <span class="s" style="color: rgb(221, 17, 68);">'Mac'</span>
<span class="n">desired_caps</span><span class="p">[</span><span class="s" style="color: rgb(221, 17, 68);">'version'</span><span class="p">]</span> <span class="o" style="font-weight: bold;">=</span> <span class="s" style="color: rgb(221, 17, 68);">'6.1'</span>
<span class="n">desired_caps</span><span class="p">[</span><span class="s" style="color: rgb(221, 17, 68);">'app'</span><span class="p">]</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="n">path</span><span class="o" style="font-weight: bold;">.</span><span class="nb" style="color: rgb(0, 134, 179);">abs</span><span class="n">path</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">'测试app的位置'</span><span class="p">)</span>
<span class="c" style="color: rgb(153, 153, 136); font-style: italic;">#def is_alert_present(wd):</span>
<span class="c" style="color: rgb(153, 153, 136); font-style: italic;"># try:</span>
<span class="c" style="color: rgb(153, 153, 136); font-style: italic;"># wd.switch_to_alert().text</span>
<span class="c" style="color: rgb(153, 153, 136); font-style: italic;"># return True</span>
<span class="c" style="color: rgb(153, 153, 136); font-style: italic;"># except:</span>
<span class="c" style="color: rgb(153, 153, 136); font-style: italic;"># return False</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">click</span><span class="p">(</span><span class="n">element</span><span class="p">):</span>
<span class="n">element</span><span class="o" style="font-weight: bold;">.</span><span class="n">click</span><span class="p">()</span>
<span class="n">time</span><span class="o" style="font-weight: bold;">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">3</span><span class="p">)</span>
<span class="n">logging</span><span class="o" style="font-weight: bold;">.</span><span class="n">info</span><span class="p">(</span><span class="nb" style="color: rgb(0, 134, 179);">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">' time click '</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">page_source</span><span class="p">)</span>
<span class="n">screenshot</span><span class="p">()</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">send_keys</span><span class="p">(</span><span class="n">element</span><span class="p">,</span> <span class="n">keys</span><span class="p">):</span>
<span class="n">element</span><span class="o" style="font-weight: bold;">.</span><span class="n">send_keys</span><span class="p">(</span><span class="n">keys</span><span class="p">)</span>
<span class="n">time</span><span class="o" style="font-weight: bold;">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">3</span><span class="p">)</span>
<span class="n">logging</span><span class="o" style="font-weight: bold;">.</span><span class="n">info</span><span class="p">(</span><span class="nb" style="color: rgb(0, 134, 179);">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">' time click '</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">page_source</span><span class="p">)</span>
<span class="n">screenshot</span><span class="p">()</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">execute_script</span><span class="p">(</span><span class="n">script</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
<span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">execute_script</span><span class="p">(</span><span class="n">script</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
<span class="n">time</span><span class="o" style="font-weight: bold;">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">3</span><span class="p">)</span>
<span class="n">screenshot</span><span class="p">()</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">mk_new_dir</span><span class="p">():</span>
<span class="k" style="font-weight: bold;">global</span> <span class="n">path</span><span class="p">,</span> <span class="n">base</span>
<span class="n">base</span> <span class="o" style="font-weight: bold;">=</span> <span class="s" style="color: rgb(221, 17, 68);">'截图存放的位置'</span>
<span class="k" style="font-weight: bold;">if</span> <span class="ow" style="font-weight: bold;">not</span> <span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="n">path</span><span class="o" style="font-weight: bold;">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">base</span><span class="p">):</span>
<span class="n">path</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">base</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">'0/'</span>
<span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="k" style="font-weight: bold;">return</span>
<span class="n">path</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">base</span> <span class="o" style="font-weight: bold;">+</span> <span class="nb" style="color: rgb(0, 134, 179);">str</span><span class="p">(</span><span class="nb" style="color: rgb(0, 134, 179);">len</span><span class="p">(</span><span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="nb" style="color: rgb(0, 134, 179);">listdir</span><span class="p">(</span><span class="n">base</span><span class="p">)))</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">'/'</span>
<span class="n">c</span> <span class="o" style="font-weight: bold;">=</span> <span class="mi" style="color: rgb(0, 153, 153);">0</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">screenshot</span><span class="p">():</span>
<span class="k" style="font-weight: bold;">global</span> <span class="n">c</span><span class="p">,</span> <span class="n">path</span>
<span class="k" style="font-weight: bold;">if</span> <span class="ow" style="font-weight: bold;">not</span> <span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="n">path</span><span class="o" style="font-weight: bold;">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
<span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">get_screenshot_as_file</span><span class="p">(</span><span class="n">path</span> <span class="o" style="font-weight: bold;">+</span> <span class="nb" style="color: rgb(0, 134, 179);">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">'.png'</span><span class="p">)</span>
<span class="n">c</span> <span class="o" style="font-weight: bold;">+=</span> <span class="mi" style="color: rgb(0, 153, 153);">1</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">compare</span><span class="p">():</span>
<span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">s</span> <span class="o" style="font-weight: bold;">=</span> <span class="nb" style="color: rgb(0, 134, 179);">list</span><span class="p">()</span>
<span class="k" style="font-weight: bold;">for</span> <span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">name</span> <span class="ow" style="font-weight: bold;">in</span> <span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="nb" style="color: rgb(0, 134, 179);">listdir</span><span class="p">(</span><span class="n">base</span><span class="p">):</span>
<span class="k" style="font-weight: bold;">if</span> <span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="n">path</span><span class="o" style="font-weight: bold;">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">base</span> <span class="o" style="font-weight: bold;">+</span> <span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">name</span><span class="p">):</span>
<span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">s</span><span class="o" style="font-weight: bold;">.</span><span class="n">append</span><span class="p">((</span><span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="n">path</span><span class="o" style="font-weight: bold;">.</span><span class="n">getmtime</span><span class="p">(</span><span class="n">base</span> <span class="o" style="font-weight: bold;">+</span> <span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">name</span><span class="p">),</span> <span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">name</span><span class="p">))</span>
<span class="k" style="font-weight: bold;">if</span> <span class="nb" style="color: rgb(0, 134, 179);">len</span><span class="p">(</span><span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">s</span><span class="p">)</span> <span class="o" style="font-weight: bold;"><</span> <span class="mi" style="color: rgb(0, 153, 153);">3</span><span class="p">:</span>
<span class="k" style="font-weight: bold;">return</span>
<span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">s</span><span class="o" style="font-weight: bold;">.</span><span class="n">sort</span><span class="p">()</span>
<span class="n">new</span> <span class="o" style="font-weight: bold;">=</span> <span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">s</span><span class="p">[</span><span class="nb" style="color: rgb(0, 134, 179);">len</span><span class="p">(</span><span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">s</span><span class="p">)</span> <span class="o" style="font-weight: bold;">-</span> <span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">][</span><span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">]</span>
<span class="n">previous</span> <span class="o" style="font-weight: bold;">=</span> <span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">s</span><span class="p">[</span><span class="nb" style="color: rgb(0, 134, 179);">len</span><span class="p">(</span><span class="nb" style="color: rgb(0, 134, 179);">dir</span><span class="n">s</span><span class="p">)</span> <span class="o" style="font-weight: bold;">-</span> <span class="mi" style="color: rgb(0, 153, 153);">2</span><span class="p">][</span><span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">]</span>
<span class="n">names</span> <span class="o" style="font-weight: bold;">=</span> <span class="p">[(</span><span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="n">path</span><span class="o" style="font-weight: bold;">.</span><span class="n">getmtime</span><span class="p">(</span><span class="n">base</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">new</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">'/'</span> <span class="o" style="font-weight: bold;">+</span> <span class="nb" style="color: rgb(0, 134, 179);">file</span><span class="n">name</span><span class="p">),</span> <span class="nb" style="color: rgb(0, 134, 179);">file</span><span class="n">name</span><span class="p">)</span> <span class="k" style="font-weight: bold;">for</span> <span class="nb" style="color: rgb(0, 134, 179);">file</span><span class="n">name</span> <span class="ow" style="font-weight: bold;">in</span>
<span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="nb" style="color: rgb(0, 134, 179);">listdir</span><span class="p">(</span><span class="n">base</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">new</span><span class="p">)]</span>
<span class="n">names</span><span class="o" style="font-weight: bold;">.</span><span class="n">sort</span><span class="p">()</span>
<span class="k" style="font-weight: bold;">for</span> <span class="n">n</span> <span class="ow" style="font-weight: bold;">in</span> <span class="n">names</span><span class="p">:</span>
<span class="k" style="font-weight: bold;">if</span> <span class="n">n</span><span class="p">[</span><span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">]</span><span class="o" style="font-weight: bold;">.</span><span class="n">startswith</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">'.'</span><span class="p">):</span>
<span class="k" style="font-weight: bold;">continue</span>
<span class="k" style="font-weight: bold;">if</span> <span class="n">os</span><span class="o" style="font-weight: bold;">.</span><span class="n">path</span><span class="o" style="font-weight: bold;">.</span><span class="n">exists</span><span class="p">(</span><span class="n">base</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">previous</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">'/'</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">n</span><span class="p">[</span><span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">]):</span>
<span class="k" style="font-weight: bold;">print</span> <span class="n">n</span><span class="p">[</span><span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">]</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">' similarity : '</span> <span class="o" style="font-weight: bold;">+</span> <span class="nb" style="color: rgb(0, 134, 179);">str</span><span class="p">(</span>
<span class="nb" style="color: rgb(0, 134, 179);">int</span><span class="p">(</span><span class="n">histsimilar</span><span class="o" style="font-weight: bold;">.</span><span class="n">calc_similar_by_path</span><span class="p">(</span><span class="n">base</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">new</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">'/'</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">n</span><span class="p">[</span><span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">],</span>
<span class="n">base</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">previous</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">'/'</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">n</span><span class="p">[</span><span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">])</span> <span class="o" style="font-weight: bold;">*</span> <span class="mi" style="color: rgb(0, 153, 153);">100</span><span class="p">))</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">'</span><span class="si" style="color: rgb(221, 17, 68);">%</span><span class="s" style="color: rgb(221, 17, 68);">'</span>
<span class="k" style="font-weight: bold;">try</span><span class="p">:</span>
<span class="n">mk_new_dir</span><span class="p">()</span>
<span class="n">wd</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">webdriver</span><span class="o" style="font-weight: bold;">.</span><span class="n">Remote</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">'http://0.0.0.0:4723/wd/hub'</span><span class="p">,</span> <span class="n">desired_caps</span><span class="p">)</span>
<span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">implicitly_wait</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">5</span><span class="p">)</span>
<span class="k" style="font-weight: bold;">try</span><span class="p">:</span>
<span class="n">button</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">'点击开启'</span><span class="p">)</span>
<span class="k" style="font-weight: bold;">while</span> <span class="ow" style="font-weight: bold;">not</span> <span class="n">button</span><span class="o" style="font-weight: bold;">.</span><span class="n">is_displayed</span><span class="p">():</span>
<span class="c" style="color: rgb(153, 153, 136); font-style: italic;">#screenshot()</span>
<span class="n">time</span><span class="o" style="font-weight: bold;">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">)</span>
<span class="n">time</span><span class="o" style="font-weight: bold;">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">3</span><span class="p">)</span>
<span class="n">click</span><span class="p">(</span><span class="n">button</span><span class="p">)</span>
<span class="k" style="font-weight: bold;">except</span> <span class="n">WebDriverException</span><span class="p">,</span> <span class="n">NoSuchElementException</span><span class="p">:</span>
<span class="n">logging</span><span class="o" style="font-weight: bold;">.</span><span class="n">info</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">'没有开机动画'</span><span class="p">)</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_xpath</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"//window[1]/tableview[1]/cell[1]/button[1]"</span><span class="p">))</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">" 返回"</span><span class="p">))</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_xpath</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"//window[1]/tableview[1]/cell[1]/button[2]"</span><span class="p">))</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">" 返回"</span><span class="p">))</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"游记"</span><span class="p">))</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"qunar login"</span><span class="p">))</span>
<span class="n">send_keys</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_xpath</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"//window[1]/image[2]/textfield[1]"</span><span class="p">),</span> <span class="s" style="color: rgb(221, 17, 68);">"lvtu@yopmail.com"</span><span class="p">)</span>
<span class="n">send_keys</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_xpath</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"//window[1]/image[3]/secure[1]"</span><span class="p">),</span> <span class="s" style="color: rgb(221, 17, 68);">"lvtu@yopmail.com"</span><span class="p">)</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"登录"</span><span class="p">))</span>
<span class="k" style="font-weight: bold;">try</span><span class="p">:</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_xpath</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"//window[1]/button[3]"</span><span class="p">))</span>
<span class="k" style="font-weight: bold;">except</span><span class="p">:</span>
<span class="n">logging</span><span class="o" style="font-weight: bold;">.</span><span class="n">info</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">'没上传提示'</span><span class="p">)</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_xpath</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"//window[1]/tableview[1]/cell[1]/text[1]"</span><span class="p">))</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_xpath</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"//window[1]/tableview[1]/cell[1]"</span><span class="p">))</span>
<span class="n">execute_script</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"mobile: swipe"</span><span class="p">,</span>
<span class="p">{</span><span class="s" style="color: rgb(221, 17, 68);">"touchCount"</span><span class="p">:</span> <span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">,</span> <span class="s" style="color: rgb(221, 17, 68);">"startX"</span><span class="p">:</span> <span class="mi" style="color: rgb(0, 153, 153);">157</span><span class="p">,</span> <span class="s" style="color: rgb(221, 17, 68);">"startY"</span><span class="p">:</span> <span class="mi" style="color: rgb(0, 153, 153);">529</span><span class="p">,</span> <span class="s" style="color: rgb(221, 17, 68);">"endX"</span><span class="p">:</span> <span class="mi" style="color: rgb(0, 153, 153);">156</span><span class="p">,</span> <span class="s" style="color: rgb(221, 17, 68);">"endY"</span><span class="p">:</span> <span class="mi" style="color: rgb(0, 153, 153);">102</span><span class="p">,</span> <span class="s" style="color: rgb(221, 17, 68);">"duration"</span><span class="p">:</span> <span class="mf" style="color: rgb(0, 153, 153);">0.5</span><span class="p">})</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">" 返回"</span><span class="p">))</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">" 返回"</span><span class="p">))</span>
<span class="n">click</span><span class="p">(</span><span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">find_element_by_xpath</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"//window[1]/button[1]"</span><span class="p">))</span>
<span class="k" style="font-weight: bold;">except</span><span class="p">:</span>
<span class="n">traceback</span><span class="o" style="font-weight: bold;">.</span><span class="n">print_exc</span><span class="p">()</span>
<span class="n">success</span> <span class="o" style="font-weight: bold;">=</span> <span class="bp" style="color: rgb(153, 153, 153);">False</span>
<span class="k" style="font-weight: bold;">finally</span><span class="p">:</span>
<span class="k" style="font-weight: bold;">if</span> <span class="n">success</span><span class="p">:</span>
<span class="c" style="color: rgb(153, 153, 136); font-style: italic;">#raise Exception("Test failed.")</span>
<span class="n">compare</span><span class="p">()</span>
<span class="n">wd</span><span class="o" style="font-weight: bold;">.</span><span class="n">quit</span><span class="p">()</span>
<span class="p"><span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 22.390625px;">计算图片相似度部分</span>
</span>
<span class="p"><span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 22.390625px;"></span></span><pre class="highlight python" name="code" style="white-space: pre-wrap; word-wrap: break-word;"><span class="c" style="color: rgb(153, 153, 136); font-style: italic;"># -*- coding: utf-8 -*-</span>
<span class="kn" style="font-weight: bold;">import</span> <span class="nn" style="color: rgb(85, 85, 85);">Image</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">make_regalur_image</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">size</span><span class="o" style="font-weight: bold;">=</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">256</span><span class="p">,</span> <span class="mi" style="color: rgb(0, 153, 153);">256</span><span class="p">)):</span>
<span class="k" style="font-weight: bold;">return</span> <span class="n">img</span><span class="o" style="font-weight: bold;">.</span><span class="n">resize</span><span class="p">(</span><span class="n">size</span><span class="p">)</span><span class="o" style="font-weight: bold;">.</span><span class="n">convert</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">'RGB'</span><span class="p">)</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">split_image</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">part_size</span><span class="o" style="font-weight: bold;">=</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">64</span><span class="p">,</span> <span class="mi" style="color: rgb(0, 153, 153);">64</span><span class="p">)):</span>
<span class="n">w</span><span class="p">,</span> <span class="n">h</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">img</span><span class="o" style="font-weight: bold;">.</span><span class="n">size</span>
<span class="n">pw</span><span class="p">,</span> <span class="n">ph</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">part_size</span>
<span class="k" style="font-weight: bold;">assert</span> <span class="n">w</span> <span class="o" style="font-weight: bold;">%</span> <span class="n">pw</span> <span class="o" style="font-weight: bold;">==</span> <span class="n">h</span> <span class="o" style="font-weight: bold;">%</span> <span class="n">ph</span> <span class="o" style="font-weight: bold;">==</span> <span class="mi" style="color: rgb(0, 153, 153);">0</span>
<span class="k" style="font-weight: bold;">return</span> <span class="p">[</span><span class="n">img</span><span class="o" style="font-weight: bold;">.</span><span class="n">crop</span><span class="p">((</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">,</span> <span class="n">i</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">pw</span><span class="p">,</span> <span class="n">j</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">ph</span><span class="p">))</span><span class="o" style="font-weight: bold;">.</span><span class="n">copy</span><span class="p">()</span> \
<span class="k" style="font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="font-weight: bold;">in</span> <span class="nb" style="color: rgb(0, 134, 179);">xrange</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">,</span> <span class="n">w</span><span class="p">,</span> <span class="n">pw</span><span class="p">)</span> \
<span class="k" style="font-weight: bold;">for</span> <span class="n">j</span> <span class="ow" style="font-weight: bold;">in</span> <span class="nb" style="color: rgb(0, 134, 179);">xrange</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">,</span> <span class="n">h</span><span class="p">,</span> <span class="n">ph</span><span class="p">)]</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">hist_similar</span><span class="p">(</span><span class="n">lh</span><span class="p">,</span> <span class="n">rh</span><span class="p">):</span>
<span class="k" style="font-weight: bold;">assert</span> <span class="nb" style="color: rgb(0, 134, 179);">len</span><span class="p">(</span><span class="n">lh</span><span class="p">)</span> <span class="o" style="font-weight: bold;">==</span> <span class="nb" style="color: rgb(0, 134, 179);">len</span><span class="p">(</span><span class="n">rh</span><span class="p">)</span>
<span class="k" style="font-weight: bold;">return</span> <span class="nb" style="color: rgb(0, 134, 179);">sum</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">1</span> <span class="o" style="font-weight: bold;">-</span> <span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">0</span> <span class="k" style="font-weight: bold;">if</span> <span class="n">l</span> <span class="o" style="font-weight: bold;">==</span> <span class="n">r</span> <span class="k" style="font-weight: bold;">else</span> <span class="nb" style="color: rgb(0, 134, 179);">float</span><span class="p">(</span><span class="nb" style="color: rgb(0, 134, 179);">abs</span><span class="p">(</span><span class="n">l</span> <span class="o" style="font-weight: bold;">-</span> <span class="n">r</span><span class="p">))</span> <span class="o" style="font-weight: bold;">/</span> <span class="nb" style="color: rgb(0, 134, 179);">max</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">r</span><span class="p">))</span> <span class="k" style="font-weight: bold;">for</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="ow" style="font-weight: bold;">in</span> <span class="nb" style="color: rgb(0, 134, 179);">zip</span><span class="p">(</span><span class="n">lh</span><span class="p">,</span> <span class="n">rh</span><span class="p">))</span> <span class="o" style="font-weight: bold;">/</span> <span class="nb" style="color: rgb(0, 134, 179);">len</span><span class="p">(</span><span class="n">lh</span><span class="p">)</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">calc_similar</span><span class="p">(</span><span class="n">li</span><span class="p">,</span> <span class="n">ri</span><span class="p">):</span>
<span class="c" style="color: rgb(153, 153, 136); font-style: italic;"># return hist_similar(li.histogram(), ri.histogram())</span>
<span class="k" style="font-weight: bold;">return</span> <span class="nb" style="color: rgb(0, 134, 179);">sum</span><span class="p">(</span><span class="n">hist_similar</span><span class="p">(</span><span class="n">l</span><span class="o" style="font-weight: bold;">.</span><span class="n">histogram</span><span class="p">(),</span> <span class="n">r</span><span class="o" style="font-weight: bold;">.</span><span class="n">histogram</span><span class="p">())</span> <span class="k" style="font-weight: bold;">for</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="ow" style="font-weight: bold;">in</span> <span class="nb" style="color: rgb(0, 134, 179);">zip</span><span class="p">(</span><span class="n">split_image</span><span class="p">(</span><span class="n">li</span><span class="p">),</span> <span class="n">split_image</span><span class="p">(</span><span class="n">ri</span><span class="p">)))</span> <span class="o" style="font-weight: bold;">/</span> <span class="mf" style="color: rgb(0, 153, 153);">16.0</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">calc_similar_by_path</span><span class="p">(</span><span class="n">lf</span><span class="p">,</span> <span class="n">rf</span><span class="p">):</span>
<span class="n">li</span><span class="p">,</span> <span class="n">ri</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">make_regalur_image</span><span class="p">(</span><span class="n">Image</span><span class="o" style="font-weight: bold;">.</span><span class="nb" style="color: rgb(0, 134, 179);">open</span><span class="p">(</span><span class="n">lf</span><span class="p">)),</span> <span class="n">make_regalur_image</span><span class="p">(</span><span class="n">Image</span><span class="o" style="font-weight: bold;">.</span><span class="nb" style="color: rgb(0, 134, 179);">open</span><span class="p">(</span><span class="n">rf</span><span class="p">))</span>
<span class="k" style="font-weight: bold;">return</span> <span class="n">calc_similar</span><span class="p">(</span><span class="n">li</span><span class="p">,</span> <span class="n">ri</span><span class="p">)</span>
<span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">make_doc_data</span><span class="p">(</span><span class="n">lf</span><span class="p">,</span> <span class="n">rf</span><span class="p">):</span>
<span class="n">li</span><span class="p">,</span> <span class="n">ri</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">make_regalur_image</span><span class="p">(</span><span class="n">Image</span><span class="o" style="font-weight: bold;">.</span><span class="nb" style="color: rgb(0, 134, 179);">open</span><span class="p">(</span><span class="n">lf</span><span class="p">)),</span> <span class="n">make_regalur_image</span><span class="p">(</span><span class="n">Image</span><span class="o" style="font-weight: bold;">.</span><span class="nb" style="color: rgb(0, 134, 179);">open</span><span class="p">(</span><span class="n">rf</span><span class="p">))</span>
<span class="n">li</span><span class="o" style="font-weight: bold;">.</span><span class="n">save</span><span class="p">(</span><span class="n">lf</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">'_regalur.png'</span><span class="p">)</span>
<span class="n">ri</span><span class="o" style="font-weight: bold;">.</span><span class="n">save</span><span class="p">(</span><span class="n">rf</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">'_regalur.png'</span><span class="p">)</span>
<span class="n">fd</span> <span class="o" style="font-weight: bold;">=</span> <span class="nb" style="color: rgb(0, 134, 179);">open</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">'stat.csv'</span><span class="p">,</span> <span class="s" style="color: rgb(221, 17, 68);">'w'</span><span class="p">)</span>
<span class="n">fd</span><span class="o" style="font-weight: bold;">.</span><span class="n">write</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">'</span><span class="se" style="color: rgb(221, 17, 68);">\n</span><span class="s" style="color: rgb(221, 17, 68);">'</span><span class="o" style="font-weight: bold;">.</span><span class="n">join</span><span class="p">(</span><span class="n">l</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">','</span> <span class="o" style="font-weight: bold;">+</span> <span class="n">r</span> <span class="k" style="font-weight: bold;">for</span> <span class="n">l</span><span class="p">,</span> <span class="n">r</span> <span class="ow" style="font-weight: bold;">in</span> <span class="nb" style="color: rgb(0, 134, 179);">zip</span><span class="p">(</span><span class="nb" style="color: rgb(0, 134, 179);">map</span><span class="p">(</span><span class="nb" style="color: rgb(0, 134, 179);">str</span><span class="p">,</span> <span class="n">li</span><span class="o" style="font-weight: bold;">.</span><span class="n">histogram</span><span class="p">()),</span> <span class="nb" style="color: rgb(0, 134, 179);">map</span><span class="p">(</span><span class="nb" style="color: rgb(0, 134, 179);">str</span><span class="p">,</span> <span class="n">ri</span><span class="o" style="font-weight: bold;">.</span><span class="n">histogram</span><span class="p">()))))</span>
<span class="c" style="color: rgb(153, 153, 136); font-style: italic;"># print >>fd, '\n'</span>
<span class="c" style="color: rgb(153, 153, 136); font-style: italic;"># fd.write(','.join(map(str, ri.histogram())))</span>
<span class="n">fd</span><span class="o" style="font-weight: bold;">.</span><span class="n">close</span><span class="p">()</span>
<span class="kn" style="font-weight: bold;">import</span> <span class="nn" style="color: rgb(85, 85, 85);">ImageDraw</span>
<span class="n">li</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">li</span><span class="o" style="font-weight: bold;">.</span><span class="n">convert</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">'RGB'</span><span class="p">)</span>
<span class="n">draw</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">ImageDraw</span><span class="o" style="font-weight: bold;">.</span><span class="n">Draw</span><span class="p">(</span><span class="n">li</span><span class="p">)</span>
<span class="k" style="font-weight: bold;">for</span> <span class="n">i</span> <span class="ow" style="font-weight: bold;">in</span> <span class="nb" style="color: rgb(0, 134, 179);">xrange</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">,</span> <span class="mi" style="color: rgb(0, 153, 153);">256</span><span class="p">,</span> <span class="mi" style="color: rgb(0, 153, 153);">64</span><span class="p">):</span>
<span class="n">draw</span><span class="o" style="font-weight: bold;">.</span><span class="n">line</span><span class="p">((</span><span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="mi" style="color: rgb(0, 153, 153);">256</span><span class="p">,</span> <span class="n">i</span><span class="p">),</span> <span class="n">fill</span><span class="o" style="font-weight: bold;">=</span><span class="s" style="color: rgb(221, 17, 68);">'#ff0000'</span><span class="p">)</span>
<span class="n">draw</span><span class="o" style="font-weight: bold;">.</span><span class="n">line</span><span class="p">((</span><span class="n">i</span><span class="p">,</span> <span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="mi" style="color: rgb(0, 153, 153);">256</span><span class="p">),</span> <span class="n">fill</span><span class="o" style="font-weight: bold;">=</span><span class="s" style="color: rgb(221, 17, 68);">'#ff0000'</span><span class="p">)</span>
<span class="n">li</span><span class="o" style="font-weight: bold;">.</span><span class="n">save</span><span class="p">(</span><span class="n">lf</span> <span class="o" style="font-weight: bold;">+</span> <span class="s" style="color: rgb(221, 17, 68);">'_lines.png'</span><span class="p">)</span>