如何在 splinter 中选择 class 、 div 、 tag ?

2024-02-08

我想了解 splinter 的功能,我试图在网上找到,但我找不到关于 splinter 的实用示例的良好文档,所以我在这里问了几个问题,以便它将帮助尝试学习 splinter 的初学者:

首先我很困惑splinter中实际的CSS选择器是什么,我到处都看到两种方法:

browser.find_by_css() 

or

browser.find_by_css_selector()

它们之间有什么区别?为什么第二个在当前的碎片中不起作用?

现在我原来的问题是如何选择任何类下的任何标签,如何选择任何id下的任何标签?

我试图找到,但我发现大多数 stackoverflow 问题都关于“如何在下拉列表中选择选项值”,并且 splinter 文档非常好,但问题是他们的方法没有足够实用的示例。

所以如果我有这个 html 代码:

<div class="medium-widget success-story-category">
                        <h2 class="widget-title"><span aria-hidden="true" class="icon-get-started"></span>Getting Started</h2>
<p>Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages. The following pages are a useful first step to get on your way writing programs with Python!</p>
<ul>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Programmers">Beginner's Guide, Programmers</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/NonProgrammers">Beginner's Guide, Non-Programmers</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Download">Beginner's Guide, Download &amp; Installation</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Examples">Code sample and snippets for Beginners</a></li>
</ul>

                    </div>

Then :

  • 如何选择<p>通过选择来标记数据class="medium-widget success-story-category"

  • 第二:如何选择第一个的“href”<li> tag

  • 第三:如何在第一个之间获取文本<li></li>

现在如果有<class_name id="something"> like :

<nav id="mainnav" class="python-navigation main-navigation do-not-print" role="navigation">


<ul class="navigation menu" role="menubar" aria-label="Main Navigation">



    <li id="about" class="tier-1 element-1   with-supernav" aria-haspopup="true">
        <a href="/about/" title="" class=" current_item selected selected">About</a>
  • 现在如何选择:<nav id="mainnav" class="python-navigation main-navigation do-not-print" role="navigation">使用 find_by_css 方法使用 id (不使用 find_by_id)

  • How get <a>使用 find_by_css 链接


我在这里找到了答案,我将对此进行解释,以便对其他程序员有所帮助:

第一件事browser.find_by_css_selector()不起作用,我使用了 find_by_css 方法,效果很好,所以我更喜欢find_by_css method.

如何选择<p>通过选择来标记数据class="medium-widget success-story-category"

我们可以选择格式为的任何类:

div[class="class_name"] or div[any_style_element="value"]

我们可以选择班级class="medium-widget success-story-category" by div[class="medium-widget success-story-category"]

我们可以选择

tag by ('div[class="medium-widget success-story-category"] p')

我们还可以找到:

find_h=browser.find_by_css('div[class="medium-widget success-story-category last"]:nth-child(2)')

or

当 html 是

`<div class="row">

                    <div class="medium-widget success-story-category">
                        <h2 class="widget-title"><span aria-hidden="true" class="icon-get-started"></span>Getting Started</h2>
<p>Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages. The following pages are a useful first step to get on your way writing programs with Python!</p>
<ul>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Programmers">Beginner's Guide, Programmers</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/NonProgrammers">Beginner's Guide, Non-Programmers</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Download">Beginner's Guide, Download &amp; Installation</a></li>
    <li><a href="https://wiki.python.org/moin/BeginnersGuide/Examples">Code sample and snippets for Beginners</a></li>
</ul>

                    </div>

                    <div class="medium-widget success-story-category last">
                        <h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Friendly &amp; Easy to Learn</h2>
<p>The community hosts conferences and meetups, collaborates on code, and much more. Python's documentation will help you along the way, and the mailing lists will keep you in touch.</p>
<ul>
    <li><a href="/community/workshops/">Conferences and Workshops</a></li>
    <li><a href="http://docs.python.org">Python Documentation</a></li>
    <li><a href="/community/lists">Mailing Lists</a> and <a href="/community/irc/">IRC channels</a></li>
</ul>

                    </div>

                </div>`

通过使用 :

`find_h=browser.find_by_css('div[class="row"]:nth-child(1) > div:nth-child(1) > p')
for i in find_h:
    print(i.text)`

我们可以通过以下方式捕获类中的图像

('div[class="image_class_name"] img')进而result["href" or "src"]

例子 :

假设我必须选择该图像,然后我可以通过以下代码获取它:

find_h=browser.find_by_css('h1[class="site-headline"] img')
for i in find_h:
    print(i["src"])

接下来的问题是如何选择

  • 标签:我们可以选择
  • 使用 nth-child(n) 进行标记:
  • 所以如果我有这个 html 代码:

    <div class="medium-widget success-story-category last">
                            <h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Friendly &amp; Easy to Learn</h2>
    <p>The community hosts conferences and meetups, collaborates on code, and much more. Python's documentation will help you along the way, and the mailing lists will keep you in touch.</p>
    <ul>
        <li><a href="/community/workshops/">Conferences and Workshops</a></li>
        <li><a href="http://docs.python.org">Python Documentation</a></li>
        <li><a href="/community/lists">Mailing Lists</a> and <a href="/community/irc/">IRC channels</a></li>
    </ul>
                        </div>
    
    
    
    <div class="medium-widget success-story-category last">
                            <h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Friendly &amp; Easy to Learn</h2>
    <p>The community hosts conferences and meetups, collaborates on code, and much more. Python's documentation will help you along the way, and the mailing lists will keep you in touch.</p>
    <ul>
        <li><a href="/community/workshops/">Conferences and Workshops</a></li>
        <li><a href="http://docs.python.org">Python Documentation</a></li>
        <li><a href="/community/lists">Mailing Lists</a> and <a href="/community/irc/">IRC channels</a></li>
    </ul>
                        </div>
    

    然后我们可以选择任何的href链接

  • 通过使用
  • div[class="medium-widget success-story-category last"]:nth-child(1) > ul > li:nth-child(2) > a
    

    请记住 nth-child(2) 在div[class="medium-widget success-story-category last"]:nth-child(1)不选择此类的第二个嵌套 div 而是选择 nth-child(2) 选择第二个medium-widget success-story-category last类(如您所见,有两个具有相同名称的类medium-widget success-story-category last) .

    最后一个问题的最后答案:

    如果有<class_name id="something"> :

    然后选择喜欢

    class_name[id="something"]
    
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

    如何在 splinter 中选择 class 、 div 、 tag ? 的相关文章

    随机推荐

    • 如何在Python3中扩展curses窗口类?

      我想延长curses通过调用创建的内置窗口类curses newwin 但是 我很难找到应该替换的类的实际名称 newwin 下面的占位符 usr bin python3 import curses class Window curses
    • AngularJS。将标签值(Unix 时间转换为人类可读时间)

      我正在从数据库获取数据并显示它 ul li li ul
    • Java 中的 for-each 表达式被翻译成什么? [复制]

      这个问题在这里已经有答案了 for SomeListElement element objectWithList getList 上面的片段翻译成什么 我最感兴趣的是如果getList 方法调用一次 还是每次迭代 元素调用 它相当于 for
    • 如何将静态或共享库链接到内核模块?

      aaa c中有一个函数 int myadd int a int b return a b 并使用 aaa c 构建到静态库中 gcc c aaa c o aaa o ar cr libaaa a aaa o 和一个共享库使用 gcc c a
    • 同步适配器中多个 RecyclerView 的滚动

      我想实现一个水平回收视图垂直内部回收视图 最终结果应该是这样的 因此 对于垂直方向上的每个元素回收视图 我还需要一个横向的 有点像学校的时间表 日期在左边 实际时间表在右边 可以水平滚动 我设法实现了这一点 通过放置回收视图第一个里面回收视
    • Sleuth 未向 Zipkin 发送跟踪信息

      即使 Zipkin 运行良好 Sleuth 也不会向 Zipkin 发送跟踪信息 我正在使用 Spring 1 5 8 RELEASE spring cloud Dalston SR4 并且我在微服务中添加了以下依赖项
    • NodePort 服务并非在所有节点上均可用

      我正在尝试运行 3 节点 Kubernetes 集群 我已经充分启动并运行了集群 以便在不同的节点上运行服务 不幸的是 我似乎无法让基于 NodePort 的服务正常工作 因为我无论如何都理解正确性 我的问题是 我定义的任何 NodePor
    • 是否可以在监视任务之后运行任务?

      我有一个基于 PHP 的项目无法运行grunt php https github com sindresorhus grunt php 相反 我使用grunt 执行 https github com jharding grunt exec运
    • 如何获取 RecyclerView ItemDecoration 中的偏移量

      我写了两个ItemDecorator s for RecyclerView 每个都添加了一些顶部偏移量getItemOffsets 比方说 第一个装饰器添加 20dp 顶部偏移 第二个 dector 添加 30dp 顶部偏移 现在 当我将它
    • 语法错误,意外的 T_PRINT,期望 T_STRING [关闭]

      这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 我收到此错
    • w7 上的 Vagrant:找不到任何盒子

      我是 stackoverflow 的新手 这是我的第一个问题 我有一个问题Vagrant当尝试在私人存储库上获取一个盒子时 我尝试在 Atlas 上获取 hashicorp precise64 但我遇到了同样的问题 流浪者找不到盒子 我在用
    • 错误:“schemaLocation 值 *** 必须具有偶数个 URI。”关于 Spring 调度程序中的命名空间

      我收到以下错误
    • EF Code First 强制预加载

      我将 EF 5 与 Code First 结合使用 我有一个类 我想总是急切地加载一些属性 我删除了virtual关键字但它不是急切加载 public class Person public ICollection
    • Xcode 中这些黄色间隔符是什么?

      刚回到我的办公桌前 Xcode 不断突出显示 下划线文件中的空白区域 如果我输入一些内容 它就会消失 但如果我切换选项卡并返回 它们就会重新出现 更新 这不是由 显示不可见 选项引起的 这看起来不同 尝试一下 您似乎正在进行搜索 查看顶部的
    • iOS - 如何预加载键盘?

      问题 在大多数 iPhone 应用程序中 第一次出现键盘时会有相当多的延迟 可能创建键盘需要相当多的精力 即使在 iPhone 4 上也是如此 大多数人似乎对此表示同意 我不是 这真的让我烦恼 而且我的应用程序的呈现方式 用户会感到非常困惑
    • 检查数据框是在 Pandas 中复制还是查看

      有没有一种简单的方法来检查两个数据帧是否是不涉及操作的相同基础数据的不同副本或视图 我试图掌握每个规则的生成时间 并且考虑到规则看起来有多么特殊 我想要一种简单的测试方法 例如 我认为 id df values 在不同视图中是稳定的 但它们
    • Rails own_to 关联,作为集合的一部分时无法访问所有者的属性?

      我有一个物体 球 它属于一个女孩 它可以有很多球 大多数情况下一切正常 但如果我尝试通过以下方式打印出女孩的名字 balls each do b b girl name end 我收到以下错误 undefined method name f
    • 监听 Flutter SQFLite 数据库中的实时变化

      我创建了一个功能齐全的应用程序Flutter 它用sqflite https pub dev packages sqflite用于存储数据的插件 我想要一个数据更改监听器 当添加或更新新数据时 它将更新或刷新数据 我怎样才能做到这一点 我遇
    • 具有外部 IP 的 Google 容器引擎,无负载均衡器

      我能够连接到 Kubernetes 中 pod 中运行的应用程序 但这是使用负载均衡器网关 根据此处的文档 https cloud google com container engine docs tutorials http balanc
    • 如何在 splinter 中选择 class 、 div 、 tag ?

      我想了解 splinter 的功能 我试图在网上找到 但我找不到关于 splinter 的实用示例的良好文档 所以我在这里问了几个问题 以便它将帮助尝试学习 splinter 的初学者 首先我很困惑splinter中实际的CSS选择器是什么