如何使用 Selenium 和 Python 通过爬虫测试非标准下拉列表 [英] How to test non-standard drop down lists through a crawler using Selenium and Python

查看:32
本文介绍了如何使用 Selenium 和 Python 通过爬虫测试非标准下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我参与了一个为网页构建爬虫程序的大学项目.现在我在网页中遇到了测试下拉列表.具体来说,以下页面不使用标准的下拉"类.

I am in a university project building a cralwer for webpages. Now I encountered testing dropdown lists in webpage. Specifically, the following page does not use the standard "dropdown" class.

https://www.mirrorfiction.com/zh-Hant/book/406

我在执行决策程序以判断网页中是否有下拉菜单以及下拉菜单是否已被下拉时遇到困难.

I have difficulties implementing a decision procedure to tell whether there are dropdowns in the webpage and whether the dropdowns have been dropped down.

程序员似乎使用了一个名为 btnGa 或类似的类.我不能用谷歌搜索.
我不知道浏览器如何知道下拉标签.
html 源文件太大.所以下面只复制了一小部分.
html 源包含一个 ul 的 navlist 类.然后这个 ul 中的 li 标签包含另一个 ul,它实际上是悬停时的下拉列表.
但我真的看不出浏览器如何知道这是一个下拉列表.因此我不知道如何编写测试用例来单击此下拉列表中的按钮.

The programmers seem to use a class called btnGa or something alike. I cannot google that.
I have no clue how a browser would know the dropdown tags.
The html source is too big. So I only copied a small part in the following.
The html source contains a ul of navlist class. Then an li tag in this ul contains another ul which is actually a dropdown list at hovering.
But I really cannot see how the browser would know that this is a dropdown list. Thus I don't know how to write a test case to click the buttons in this dropdown list.

任何人都可以帮助我识别下拉列表并单击下拉列表项吗?

Can any one help me out in recognizing the dropdown lists and click the dropdown list items ?

HTML:

<ul class="navList">
    <li class="mobileDisable">
        <a data-ga-label="小說" data-ga-action="click" data-ga-category="header" onclick="app.navLiClick(this)" class="hasSub btnGa open" href="https://www.mirrorfiction.com/zh-Hant/list/novel">
            <span class="text novel">小說</span>
        </a>
        <ul>
            <li>
                <a data-ga-label="小說-仙俠" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/11">
                        <span class="text">仙俠</span>
                        <span class="num">/ 16</span>
                    </a>
                </li>
                <li>
                    <a data-ga-label="小說-玄幻" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/9">
                        <span class="text">玄幻</span>
                        <span class="num">/ 24</span>
                    </a>
                </li>
                <li>
                    <a data-ga-label="小說-奇幻" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/29">
                        <span class="text">奇幻</span>
                        <span class="num">/ 56</span>
                     </a>
                                </li>
                                                            <li>
                     <a data-ga-label="小說-武俠" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/10">
                         <span class="text">武俠</span>
                         <span class="num">/ 11</span>
                      </a>
                  </li>
                                                            <li>
                  <a data-ga-label="小說-科幻" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/8">
                       <span class="text">科幻</span>
                       <span class="num">/ 17</span>
                  </a>
              </li>
                                                            <li>
              <a data-ga-label="小說-恐怖" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/30">
                       <span class="text">恐怖</span>
                       <span class="num">/ 11</span>
              </a>
            </li>
                                                            <li>
           <a data-ga-label="小說-校園" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/7">
                  <span class="text">校園</span>
                  <span class="num">/ 16</span>
                </a>
            </li>
                                                            <li>
            <a data-ga-label="小說-愛情" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/1">
                  <span class="text">愛情</span>
                  <span class="num">/ 78</span>
               </a>
            </li>
                                                            <li>
            <a data-ga-label="小說-寫實" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/5">
                  <span class="text">寫實</span>
                  <span class="num">/ 48</span>
                </a>
            </li>
                                                            <li>
                                    <a data-ga-label="小說-歷史" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/6">
                                        <span class="text">歷史</span>
                                        <span class="num">/ 13</span>
                                    </a>
                                </li>
                                                            <li>
                                    <a data-ga-label="小說-驚悚" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/31">
                                        <span class="text">驚悚</span>
                                        <span class="num">/ 24</span>
                                    </a>
                                </li>
                                                            <li>
                                    <a data-ga-label="小說-其它" data-ga-action="click" data-ga-category="header" class="btnGa" href="https://www.mirrorfiction.com/zh-Hant/list/novel/classification/12">
                                        <span class="text">其它</span>
                                        <span class="num">/ 40</span>
                                    </a>
                                </li>
                                                    </ul>
                    </li>

推荐答案

考虑按照步骤操作代码行打开url &点击菜单:

Consider following the steps & lines of code to open the url & click on a menu:

  • 通过 pip 安装当前版本的 selenium
  • 下载最新的 chromedriver.exe 并在脚本中提供绝对路径
  • 代码块:

  • Install current version of selenium through pip
  • Download the latest chromedriver.exe and provide the absolute path in your script
  • Code Block:

from selenium import webdriver
driver=webdriver.Chrome("C:\Utility\your_directory\chromedriver.exe")
#maximize the browser window
driver.maximize_window()
#open the url in the browser
driver.get("https://www.mirrorfiction.com/zh-Hant/book/406")
#click on the first menu item 小說
driver.find_element_by_xpath("//nav[@id='nav']/div/ul/li/a/span[@class='text novel']").click()

这篇关于如何使用 Selenium 和 Python 通过爬虫测试非标准下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆