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

Aad*_*Ura 1 python selenium automation beautifulsoup splinter

我试图理解分裂功能,我试图在网上找到,但我找不到关于分裂的实用例子的好文档,所以我在这里问了几个问题,这样它会帮助初学者试图学习分裂:

首先,我混淆了splinter中的实际css选择器,我看到了两种方法:

browser.find_by_css() 
Run Code Online (Sandbox Code Playgroud)

要么

browser.find_by_css_selector()
Run Code Online (Sandbox Code Playgroud)

它们之间有什么区别,为什么第二个不能在当前的分裂中工作?

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

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

所以,如果我有这个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>
Run Code Online (Sandbox Code Playgroud)

然后 :

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

  • 第二:如何选择第一个<li>标签的 "href"

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

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

<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>
Run Code Online (Sandbox Code Playgroud)
  • 现在如何选择:<nav id="mainnav" class="python-navigation main-navigation do-not-print" role="navigation">id使用find_by_css方法(不使用find_by_id)

  • 如何<a>使用find_by_css 获取链接

Aad*_*Ura 6

我在这里找到了我的答案,我将解释这对其他程序员有帮助:

第一件事browser.find_by_css_selector()是不工作,我使用find_by_css方法完美的工作,所以我更喜欢find_by_css方法.

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

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

div[class="class_name"] 要么 div[any_style_element="value"]

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

我们可以选择

标记为 ('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)')

要么

当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>`
Run Code Online (Sandbox Code Playgroud)

通过使用 :

`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)`
Run Code Online (Sandbox Code Playgroud)

我们可以捕捉到一个类中的图像

('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"])
Run Code Online (Sandbox Code Playgroud)

接下来的问题是如何选择

  • tag:我们可以选择
  • tag usng 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>
    
    Run Code Online (Sandbox Code Playgroud)

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

  • 通过使用

    div[class="medium-widget success-story-category last"]:nth-child(1) > ul > li:nth-child(2) > a
    
    Run Code Online (Sandbox Code Playgroud)

    请记住nth-child(2)in 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"]
    
    Run Code Online (Sandbox Code Playgroud)