python selenium - 获取链接的文本(a)

时间:2016-12-30 17:38:34

标签: python selenium phantomjs

我如何获得文本:"本文"

以下是我的尝试:

elem = browser.find_element_by_css_selector('td.mainContent > a')
print(elem.text)

这让我很困惑,因为我确信这会找到那个文本,但它并没有。 (我没有犯任何错误......这很奇怪)

html代码:(搜索"此文本"在html中找到它。)

<tbody>


<tr class="dbaListing listing hasInsertionFee">

    <td class="pictureColumn" colspan="1">
        <div class="thumbnailContainer">
            <a class="thumbnailContainerInner" href="http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/">

            <img class="thumbnail lazy image-placeholder" alt="Tastatur, Mad Catz, Strike 7, Perfekt, Den er helt ny, stadig i fuld indpakning. 

Hvorfor sælger jeg?
Jeg opdagede at jeg havde skaffet det forkerte tastetur, der var ment som gave. Og siden at jeg havde ventet for længe med at få den byttet, var det altså for sent. Derfor sælger jeg den så for 1900 kr. (300 mindre en nypris)

Den er virkelig alle pengene værd, og har så mange funktioner, at jeg aldrig kan komme tæt på at beskrive alt her. Derfor hvis i er interesserede i den kan i..." data-original="http://dbastatic.dk/pictures/pictures/3d/05/e060-37f5-4465-af3e-d20e730a9853.jpg?preset=srpgallery" src="http://dbastatic.dk/pictures/pictures/3d/05/e060-37f5-4465-af3e-d20e730a9853.jpg?preset=srpgallery" style="display: block;">
            </a>
        </div>
    </td>

<td class="mainContent" colspan="2">
    <script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "Product",
        "name": "Tastatur, Mad Catz, Strike 7, Perfekt, Den er...",
        "image": "http://dbastatic.dk/pictures/pictures/3d/05/e060-37f5-4465-af3e-d20e730a9853.jpg?preset=medium",
        "url": "http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/",
        "offers": {
            "@type": "Offer",
            "priceCurrency": "DKK",
            "price": "1900"
        }
    }
    </script>

    <a class="listingLink" href="http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/">
            <span class="headline-block">
                            </span>

    </a>

    <div class="expandable-box expandable-box-collapsed">
        <a class="listingLink" href="http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/">THIS TEXT</a>
        <a class="link-to-listing" href="http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/">Se hele annoncen</a>
    </div>


<ul class="details">

    <li>


<span>1.900 kr.</span>
    </li>

</ul>

</td>




                <td>

<span>129</span></td>
                <td>

<span>9 dage</span></td>
                <td>0</td>
                <td>

        <div class="listingUpselling bump-up-upsell">
            <p>
                Er nu på side <b>57</b>.
            </p>
            <p><a href="/produkt-tilvalg/?id=1029751995&amp;produktId=BumpUp&amp;returnUrl=http%3A%2F%2Fwww.dba.dk%2Fmin-dbadk%2F" rel="nofollow" data-ga-act="bumpup-upsell-page-number-click" data-ga-lbl="tenzing-project" class="upsellingButton btn btn-small trackClicks">Køb oprykning</a></p>
        </div>

</td>
<td class="noWrap myDbaActions">
    <ul class="unstyled">
            <li>
                <a href="/rediger-annonce/?id=1029751995" data-ga-act="EditAdBegin" data-ga-lbl="" class="trackClicks">Rediger</a>
            </li>


            <li>
                <a href="#" onclick="Dba.MyDba.MyListings.inactivateListing('/ajax/my-dba/MyDba/InActivateListing', 1029751995); return false; ">Deaktiver</a>
            </li>

        <li>
            <a href="#" data-ga-act="DeleteAdBegin" data-ga-lbl="" onclick="Dba.MyDba.MyListings.deleteListing('/ajax/my-dba/MyDba/DeleteListing', 1029751995); return false;" class="trackClicks">Slet</a>
        </li>


            <li>
                <a href="/paypal/anmodning/?externalid=1029751995" data-ga-act="click" data-ga-lbl="paypal-request-payment" class="trackClicks">Start PayPal</a>
            </li>

            <li>
                <a href="/fragt/1029751995/" data-ga-act="shipping-buy-label" data-ga-lbl="shipping" class="trackClicks">Køb pakkelabel</a>
            </li>
    </ul>
</td>
                </tr>  


<tr class="dbaListing listing hasInsertionFee">

    <td class="pictureColumn" colspan="1">
        <div class="thumbnailContainer">
            <a class="thumbnailContainerInner" href="http://www.dba.dk/dykkerur-akribos-xxiv-test/id-1030155633/">

            <img class="thumbnail lazy image-placeholder" alt="Dykkerur, Akribos XXIV, test" data-original="http://dbastatic.dk/pictures/pictures/41/0d/7053-631b-4ba6-b520-cb0cbdd67b29.jpg?preset=srpgallery" src="http://dbastatic.dk/pictures/pictures/41/0d/7053-631b-4ba6-b520-cb0cbdd67b29.jpg?preset=srpgallery" style="display: block;">
            </a>
        </div>
    </td>

<td class="mainContent" colspan="2">
    <script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "Product",
        "name": "Dykkerur, Akribos XXIV, test",
        "image": "http://dbastatic.dk/pictures/pictures/41/0d/7053-631b-4ba6-b520-cb0cbdd67b29.jpg?preset=medium",
        "url": "http://www.dba.dk/dykkerur-akribos-xxiv-test/id-1030155633/",
        "offers": {
            "@type": "Offer",
            "priceCurrency": "DKK",
            "price": "9999"
        }
    }
    </script>

    <a class="listingLink" href="http://www.dba.dk/dykkerur-akribos-xxiv-test/id-1030155633/">
            <span class="headline-block">
                            </span>

    </a>

    <div class="expandable-box expandable-box-collapsed">
        <a class="listingLink" href="http://www.dba.dk/dykkerur-akribos-xxiv-test/id-1030155633/">Dykkerur, Akribos XXIV, test</a>
        <a class="link-to-listing" href="http://www.dba.dk/dykkerur-akribos-xxiv-test/id-1030155633/">Se hele annoncen</a>
    </div>


<ul class="details">

    <li>


<span>9.999 kr.</span>
    </li>

</ul>

</td>




                <td>

<span>47</span></td>
                <td>

<span>29 dage</span></td>
                <td>0</td>
                <td>

        <div class="listingUpselling bump-up-upsell">
            <p>
                Er nu på side <b>8</b>.
            </p>
            <p><a href="/produkt-tilvalg/?id=1030155633&amp;produktId=BumpUp&amp;returnUrl=http%3A%2F%2Fwww.dba.dk%2Fmin-dbadk%2F" rel="nofollow" data-ga-act="bumpup-upsell-page-number-click" data-ga-lbl="tenzing-project" class="upsellingButton btn btn-small trackClicks">Køb oprykning</a></p>
        </div>

</td>
<td class="noWrap myDbaActions">
    <ul class="unstyled">
            <li>
                <a href="/rediger-annonce/?id=1030155633" data-ga-act="EditAdBegin" data-ga-lbl="" class="trackClicks">Rediger</a>
            </li>


            <li>
                <a href="#" onclick="Dba.MyDba.MyListings.inactivateListing('/ajax/my-dba/MyDba/InActivateListing', 1030155633); return false; ">Deaktiver</a>
            </li>

        <li>
            <a href="#" data-ga-act="DeleteAdBegin" data-ga-lbl="" onclick="Dba.MyDba.MyListings.deleteListing('/ajax/my-dba/MyDba/DeleteListing', 1030155633); return false;" class="trackClicks">Slet</a>
        </li>


            <li>
                <a href="/paypal/anmodning/?externalid=1030155633" data-ga-act="click" data-ga-lbl="paypal-request-payment" class="trackClicks">Start PayPal</a>
            </li>

            <li>
                <a href="/fragt/1030155633/" data-ga-act="shipping-buy-label" data-ga-lbl="shipping" class="trackClicks">Køb pakkelabel</a>
            </li>
    </ul>
</td>
                </tr>  


<tr class="dbaListing listing hasInsertionFee">

    <td class="pictureColumn" colspan="1">
        <div class="thumbnailContainer">
            <a class="thumbnailContainerInner" href="http://www.dba.dk/dykkerur-adidas-test/id-1030155766/">

            <img class="thumbnail lazy image-placeholder" alt="Dykkerur, Adidas, test" data-original="http://dbastatic.dk/pictures/pictures/6d/45/bee3-8bb7-4e0b-9cb4-4c75ebea7ac2.jpg?preset=srpgallery" src="http://dbastatic.dk/pictures/pictures/6d/45/bee3-8bb7-4e0b-9cb4-4c75ebea7ac2.jpg?preset=srpgallery" style="display: block;">
            </a>
        </div>
    </td>

<td class="mainContent" colspan="2">
    <script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "Product",
        "name": "Dykkerur, Adidas, test",
        "image": "http://dbastatic.dk/pictures/pictures/6d/45/bee3-8bb7-4e0b-9cb4-4c75ebea7ac2.jpg?preset=medium",
        "url": "http://www.dba.dk/dykkerur-adidas-test/id-1030155766/",
        "offers": {
            "@type": "Offer",
            "priceCurrency": "DKK",
            "price": "9999"
        }
    }
    </script>

    <a class="listingLink" href="http://www.dba.dk/dykkerur-adidas-test/id-1030155766/">
            <span class="headline-block">
                            </span>

    </a>

    <div class="expandable-box expandable-box-collapsed">
        <a class="listingLink" href="http://www.dba.dk/dykkerur-adidas-test/id-1030155766/">Dykkerur, Adidas, test</a>
        <a class="link-to-listing" href="http://www.dba.dk/dykkerur-adidas-test/id-1030155766/">Se hele annoncen</a>
    </div>


<ul class="details">

    <li>


<span>9.999 kr.</span>
    </li>

</ul>

</td>




                <td>

<span>51</span></td>
                <td>

<span>29 dage</span></td>
                <td>0</td>
                <td>

        <div class="listingUpselling bump-up-upsell">
            <p>
                Er nu på side <b>8</b>.
            </p>
            <p><a href="/produkt-tilvalg/?id=1030155766&amp;produktId=BumpUp&amp;returnUrl=http%3A%2F%2Fwww.dba.dk%2Fmin-dbadk%2F" rel="nofollow" data-ga-act="bumpup-upsell-page-number-click" data-ga-lbl="tenzing-project" class="upsellingButton btn btn-small trackClicks">Køb oprykning</a></p>
        </div>

</td>
<td class="noWrap myDbaActions">
    <ul class="unstyled">
            <li>
                <a href="/rediger-annonce/?id=1030155766" data-ga-act="EditAdBegin" data-ga-lbl="" class="trackClicks">Rediger</a>
            </li>


            <li>
                <a href="#" onclick="Dba.MyDba.MyListings.inactivateListing('/ajax/my-dba/MyDba/InActivateListing', 1030155766); return false; ">Deaktiver</a>
            </li>

        <li>
            <a href="#" data-ga-act="DeleteAdBegin" data-ga-lbl="" onclick="Dba.MyDba.MyListings.deleteListing('/ajax/my-dba/MyDba/DeleteListing', 1030155766); return false;" class="trackClicks">Slet</a>
        </li>


            <li>
                <a href="/paypal/anmodning/?externalid=1030155766" data-ga-act="click" data-ga-lbl="paypal-request-payment" class="trackClicks">Start PayPal</a>
            </li>

            <li>
                <a href="/fragt/1030155766/" data-ga-act="shipping-buy-label" data-ga-lbl="shipping" class="trackClicks">Køb pakkelabel</a>
            </li>
    </ul>
</td>
                </tr>  


<tr class="dbaListing listing lastListing hasInsertionFee">

    <td class="pictureColumn" colspan="1">
        <div class="thumbnailContainer">
            <a class="thumbnailContainerInner" href="http://www.dba.dk/joystick-l-h-defekt-test/id-1030156328/">

            <img class="thumbnail lazy image-placeholder" alt="Joystick, l, h, Defekt, test" data-original="http://dbastatic.dk/pictures/pictures/25/48/8637-94be-42b8-a809-ed69822a40e4.jpg?preset=srpgallery" src="http://dbastatic.dk/pictures/pictures/25/48/8637-94be-42b8-a809-ed69822a40e4.jpg?preset=srpgallery" style="display: block;">
            </a>
        </div>
    </td>

<td class="mainContent" colspan="2">
    <script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "Product",
        "name": "Joystick, l, h, Defekt, test",
        "image": "http://dbastatic.dk/pictures/pictures/25/48/8637-94be-42b8-a809-ed69822a40e4.jpg?preset=medium",
        "url": "http://www.dba.dk/joystick-l-h-defekt-test/id-1030156328/",
        "offers": {
            "@type": "Offer",
            "priceCurrency": "DKK",
            "price": "900"
        }
    }
    </script>

    <a class="listingLink" href="http://www.dba.dk/joystick-l-h-defekt-test/id-1030156328/">
            <span class="headline-block">
                            </span>

    </a>

    <div class="expandable-box expandable-box-collapsed">
        <a class="listingLink" href="http://www.dba.dk/joystick-l-h-defekt-test/id-1030156328/">Joystick, l, h, Defekt, test</a>
        <a class="link-to-listing" href="http://www.dba.dk/joystick-l-h-defekt-test/id-1030156328/">Se hele annoncen</a>
    </div>


<ul class="details">

    <li>


<span>900 kr.</span>
    </li>

</ul>

</td>




                <td>

<span>44</span></td>
                <td>

<span>29 dage</span></td>
                <td>0</td>
                <td>

        <div class="listingUpselling bump-up-upsell">
            <p>
                Er nu på side <b>5</b>.
            </p>
            <p><a href="/produkt-tilvalg/?id=1030156328&amp;produktId=BumpUp&amp;returnUrl=http%3A%2F%2Fwww.dba.dk%2Fmin-dbadk%2F" rel="nofollow" data-ga-act="bumpup-upsell-page-number-click" data-ga-lbl="tenzing-project" class="upsellingButton btn btn-small trackClicks">Køb oprykning</a></p>
        </div>

</td>
<td class="noWrap myDbaActions">
    <ul class="unstyled">
            <li>
                <a href="/rediger-annonce/?id=1030156328" data-ga-act="EditAdBegin" data-ga-lbl="" class="trackClicks">Rediger</a>
            </li>


            <li>
                <a href="#" onclick="Dba.MyDba.MyListings.inactivateListing('/ajax/my-dba/MyDba/InActivateListing', 1030156328); return false; ">Deaktiver</a>
            </li>

        <li>
            <a href="#" data-ga-act="DeleteAdBegin" data-ga-lbl="" onclick="Dba.MyDba.MyListings.deleteListing('/ajax/my-dba/MyDba/DeleteListing', 1030156328); return false;" class="trackClicks">Slet</a>
        </li>


            <li>
                <a href="/paypal/anmodning/?externalid=1030156328" data-ga-act="click" data-ga-lbl="paypal-request-payment" class="trackClicks">Start PayPal</a>
            </li>

            <li>
                <a href="/fragt/1030156328/" data-ga-act="shipping-buy-label" data-ga-lbl="shipping" class="trackClicks">Køb pakkelabel</a>
            </li>
    </ul>
</td>
                </tr>  

        </tbody>

2 个答案:

答案 0 :(得分:0)

试试这个

string.Format

答案 1 :(得分:0)

您似乎使用了错误的选择器。请尝试以下方法:

elem = browser.find_element_by_css_selector('div.expandable-box.expandable-box-collapsed > a.listingLink')
print(elem.text)

<强>更新

您的选择器td.mainContent > a指向<td class="mainContent">的直接孩子,这是

<a class="listingLink" href="http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/">
        <span class="headline-block"></span>
</a>

如您所见,它不包含文本节点