InvalidSelectorException:消息:无效的选择器:指定了无效或非法的选择器,使用 CSSSelector 和 Selenium Python 查找 href
InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified finding a href using CSSSelector and Selenium Python
我正在尝试从 中获取 属性“href”,即 "/kkanagas/status/1491996075573587994"。我知道它很长,但我找不到另一种方法来缩短它。只需搜索 "href",您就会看到 "/kkanagas/status/1491996075573587994" - 这正是我要找的。
<article aria-labelledby="id__fhryquvd10b id__xyp79hh56xk id__ufvrjjfijmj id__t3x5a40wpik id__bar6zadu3v9 id__bskm5hb18ak id__k838i141bg id__sxzis5aczdj id__ymircvocx1 id__ebx7cry08s9 id__qql66qtrpr id__l377gbo5gll id__rz84acqn28" role="article" tabindex="0" class="css-1dbjc4n r-1loqt21 r-18u37iz r-1ny4l3l r-1udh08x r-1qhn6m8 r-i023vh r-o7ynqc r-6416eg" data-testid="tweet">
<div class="css-1dbjc4n r-eqz5dr r-16y2uox r-1wbh5a2">
<div class="css-1dbjc4n r-16y2uox r-1wbh5a2 r-1ny4l3l">
<div class="css-1dbjc4n">
<div class="css-1dbjc4n">
<div class="css-1dbjc4n r-18u37iz">
<div class="css-1dbjc4n r-1iusvr4 r-16y2uox r-ttdzmv"></div>
</div>
</div>
<div class="css-1dbjc4n r-18u37iz">
<div class="css-1dbjc4n r-1awozwy r-1hwvwag r-18kxxzh r-1b7u577">
<div class="css-1dbjc4n r-18kxxzh r-1wbh5a2 r-13qz1uu">
<div class="css-1dbjc4n r-1wbh5a2 r-dnmrzs">
<div class="css-1dbjc4n r-1adg3ll r-bztko3" id="id__bskm5hb18ak" style="height: 48px; width: 48px;">
<div class="r-1adg3ll r-13qz1uu" style="padding-bottom: 100%;"></div>
<div class="r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-ipm5af r-13qz1uu">
<div class="css-1dbjc4n r-1adg3ll r-1pi2tsx r-1wyvozj r-bztko3 r-u8s1d r-1v2oles r-desppf r-13qz1uu">
<div class="r-1adg3ll r-13qz1uu" style="padding-bottom: 100%;"></div>
<div class="r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-ipm5af r-13qz1uu">
<div class="css-1dbjc4n r-sdzlij r-ggadg3 r-1udh08x r-u8s1d r-8jfcpp" style="height: calc(100% - -4px); width: calc(100% - -4px);">
<a href="/kkanagas" role="link" class="css-4rbku5 css-18t94o4 css-1dbjc4n r-1niwhzg r-1loqt21 r-1pi2tsx r-1ny4l3l r-o7ynqc r-6416eg r-13qz1uu">
<div class="css-1dbjc4n r-sdzlij r-1wyvozj r-1udh08x r-633pao r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);">
<div class="css-1dbjc4n r-1niwhzg r-1pi2tsx r-13qz1uu"></div>
</div>
<div class="css-1dbjc4n r-sdzlij r-1wyvozj r-1udh08x r-633pao r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);">
<div class="css-1dbjc4n r-kemksi r-1pi2tsx r-13qz1uu"></div>
</div>
<div class="css-1dbjc4n r-kemksi r-sdzlij r-1wyvozj r-1udh08x r-633pao r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);">
<div class="css-1dbjc4n r-1adg3ll r-1udh08x" style="">
<div class="r-1adg3ll r-13qz1uu" style="padding-bottom: 100%;"></div>
<div class="r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-ipm5af r-13qz1uu">
<div aria-label="" class="css-1dbjc4n r-1p0dtai r-1mlwlqe r-1d2f490 r-1udh08x r-u8s1d r-zchlnj r-ipm5af r-417010">
<div class="css-1dbjc4n r-1niwhzg r-vvn4in r-u6sd8q r-4gszlv r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-zchlnj r-ipm5af r-13qz1uu r-1wyyakw" style="background-image: url("https://pbs.twimg.com/profile_images/3202473125/48583ddb2e2de5d9193020f2cf38694b_bigger.jpeg");"></div>
<img alt="" draggable="true" src="https://pbs.twimg.com/profile_images/3202473125/48583ddb2e2de5d9193020f2cf38694b_bigger.jpeg" class="css-9pa8cd">
</div>
</div>
</div>
</div>
<div class="css-1dbjc4n r-sdzlij r-1wyvozj r-1udh08x r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);">
<div class="css-1dbjc4n r-172uzmj r-1pi2tsx r-1ny4l3l r-o7ynqc r-6416eg r-13qz1uu"></div>
</div>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="css-1dbjc4n r-1iusvr4 r-16y2uox r-1777fci r-kzbkwu">
<div class="css-1dbjc4n">
<div class="css-1dbjc4n r-zl2h9q">
<div class="css-1dbjc4n r-k4xj1c r-18u37iz r-1wtj0ep">
<div class="css-1dbjc4n r-1d09ksm r-18u37iz r-1wbh5a2">
<div class="css-1dbjc4n r-1wbh5a2 r-dnmrzs">
<a href="/kkanagas" role="link" class="css-4rbku5 css-18t94o4 css-1dbjc4n r-1loqt21 r-1wbh5a2 r-dnmrzs r-1ny4l3l">
<div class="css-1dbjc4n r-1awozwy r-18u37iz r-1wbh5a2 r-dnmrzs r-1ny4l3l" id="id__bar6zadu3v9">
<div class="css-1dbjc4n r-1awozwy r-18u37iz r-dnmrzs">
<div dir="auto" class="css-901oao r-1awozwy r-1fmj7o5 r-6koalj r-37j5jr r-a023e6 r-b88u0q r-rjixqe r-bcqeeo r-1udh08x r-3s2u2q r-qvutc0"><span class="css-901oao css-16my406 css-bfa6kz r-poiln3 r-bcqeeo r-qvutc0"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">Karthik K</span></span></div>
<div dir="auto" class="css-901oao r-1fmj7o5 r-xoduu5 r-18u37iz r-1q142lx r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-qvutc0"></div>
</div>
<div class="css-1dbjc4n r-18u37iz r-1wbh5a2 r-13hce6t">
<div dir="ltr" class="css-901oao css-bfa6kz r-9ilb82 r-18u37iz r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-qvutc0"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">@kkanagas</span></div>
</div>
</div>
</a>
</div>
<div dir="auto" aria-hidden="true" class="css-901oao r-9ilb82 r-1q142lx r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-s1qlax r-qvutc0"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">·</span></div>
<a href="/kkanagas/status/1491996075573587994" dir="auto" aria-label="1 minute ago" role="link" class="css-4rbku5 css-18t94o4 css-901oao r-9ilb82 r-1loqt21 r-1q142lx r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-3s2u2q r-qvutc0" id="id__sxzis5aczdj"><time datetime="2022-02-11T04:42:39.000Z">1m</time></a>
</div>
<div class="css-1dbjc4n r-1joea0r">
<div class="css-1dbjc4n r-1awozwy r-6koalj r-18u37iz">
<div class="css-1dbjc4n">
<div class="css-1dbjc4n r-18u37iz r-1h0z5md">
<div aria-expanded="false" aria-haspopup="menu" aria-label="More" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-bt1l66 r-1ny4l3l r-bztko3 r-lrvibr" data-testid="caret">
<div dir="ltr" class="css-901oao r-1awozwy r-9ilb82 r-6koalj r-37j5jr r-a023e6 r-16dba41 r-1h0z5md r-rjixqe r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0">
<div class="css-1dbjc4n r-xoduu5">
<div class="css-1dbjc4n r-1niwhzg r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-1ny4l3l r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
<svg viewBox="0 0 24 24" aria-hidden="true" class="r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1hdv0qi">
<g>
<circle cx="5" cy="12" r="2"></circle>
<circle cx="12" cy="12" r="2"></circle>
<circle cx="19" cy="12" r="2"></circle>
</g>
</svg>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="css-1dbjc4n">
<div class="css-1dbjc4n">
<div lang="en" dir="auto" class="css-901oao r-1fmj7o5 r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-bnwqim r-qvutc0" id="id__ebx7cry08s9"><span class="r-18u37iz"><a dir="ltr" href="/hashtag/USA?src=hashtag_click" role="link" class="css-4rbku5 css-18t94o4 css-901oao css-16my406 r-1cvl2hr r-1loqt21 r-poiln3 r-b88u0q r-bcqeeo r-qvutc0">#USA</a></span><span class="css-901oao css-16my406 r-poiln3 r-b88u0q r-bcqeeo r-qvutc0"></span><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0"> and </span><span class="r-18u37iz"><a dir="ltr" href="/hashtag/CZE?src=hashtag_click" role="link" class="css-4rbku5 css-18t94o4 css-901oao css-16my406 r-1cvl2hr r-1loqt21 r-poiln3 r-bcqeeo r-qvutc0">#CZE</a></span><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0"> are tied 0-0 at the end of the first period of this Women's Hockey QF. </span><img alt="Eyes" draggable="false" src="https://abs-0.twimg.com/emoji/v2/svg/1f440.svg" title="Eyes" class="r-4qtqp9 r-dflpy8 r-sjv1od r-zw8f10 r-10akycc r-h9hxbl"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">
</span><span class="r-18u37iz"><a dir="ltr" href="/hashtag/Beijing2022?src=hashtag_click" role="link" class="css-4rbku5 css-18t94o4 css-901oao css-16my406 r-1cvl2hr r-1loqt21 r-poiln3 r-bcqeeo r-3s2u2q r-qvutc0">#Beijing2022<img alt="" draggable="false" src="https://abs.twimg.com/hashflags/Beijing_Winter_Olympics_Beijing2022_2022/Beijing_Winter_Olympics_Beijing2022_2022.png" class="r-4qtqp9 r-dflpy8 r-9cviqr r-zw8f10 r-10akycc r-h9hxbl"></a></span><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0"> </span><span class="r-18u37iz"><a dir="ltr" href="/hashtag/IceHockey?src=hashtag_click" role="link" class="css-4rbku5 css-18t94o4 css-901oao css-16my406 r-1cvl2hr r-1loqt21 r-poiln3 r-bcqeeo r-3s2u2q r-qvutc0">#IceHockey<img alt="" draggable="false" src="https://abs.twimg.com/hashflags/Beijing_Winter_Olympics_IceHockey_2022/Beijing_Winter_Olympics_IceHockey_2022.png" class="r-4qtqp9 r-dflpy8 r-9cviqr r-zw8f10 r-10akycc r-h9hxbl"></a></span>
</div>
</div>
<div class="css-1dbjc4n"></div>
<div class="css-1dbjc4n">
<div aria-label="" role="group" class="css-1dbjc4n r-1ta3fxp r-18u37iz r-1wtj0ep r-1s2bzr4 r-1mdbhws" id="id__eui52vqqmkn">
<div class="css-1dbjc4n r-18u37iz r-1h0z5md">
<div aria-label="0 Replies. Reply" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-bt1l66 r-1ny4l3l r-bztko3 r-lrvibr" data-testid="reply">
<div dir="ltr" class="css-901oao r-1awozwy r-9ilb82 r-6koalj r-37j5jr r-a023e6 r-16dba41 r-1h0z5md r-rjixqe r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0">
<div class="css-1dbjc4n r-xoduu5">
<div class="css-1dbjc4n r-1niwhzg r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-1ny4l3l r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
<svg viewBox="0 0 24 24" aria-hidden="true" class="r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1hdv0qi">
<g>
<path d="M14.046 2.242l-4.148-.01h-.002c-4.374 0-7.8 3.427-7.8 7.802 0 4.098 3.186 7.206 7.465 7.37v3.828c0 .108.044.286.12.403.142.225.384.347.632.347.138 0 .277-.038.402-.118.264-.168 6.473-4.14 8.088-5.506 1.902-1.61 3.04-3.97 3.043-6.312v-.017c-.006-4.367-3.43-7.787-7.8-7.788zm3.787 12.972c-1.134.96-4.862 3.405-6.772 4.643V16.67c0-.414-.335-.75-.75-.75h-.396c-3.66 0-6.318-2.476-6.318-5.886 0-3.534 2.768-6.302 6.3-6.302l4.147.01h.002c3.532 0 6.3 2.766 6.302 6.296-.003 1.91-.942 3.844-2.514 5.176z"></path>
</g>
</svg>
</div>
<div class="css-1dbjc4n r-xoduu5 r-1udh08x"><span data-testid="app-text-transition-container" style="transition-property: transform; transition-duration: 0.3s; transform: translate3d(0px, 0px, 0px);"><span class="css-901oao css-16my406 r-poiln3 r-n6v787 r-1cwl3u0 r-vc7bo5 r-1pn2ns4 r-qvutc0"></span></span></div>
</div>
</div>
</div>
<div class="css-1dbjc4n r-18u37iz r-1h0z5md">
<div aria-expanded="false" aria-haspopup="menu" aria-label="0 Retweets. Retweet" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-bt1l66 r-1ny4l3l r-bztko3 r-lrvibr" data-testid="retweet">
<div dir="ltr" class="css-901oao r-1awozwy r-9ilb82 r-6koalj r-37j5jr r-a023e6 r-16dba41 r-1h0z5md r-rjixqe r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0">
<div class="css-1dbjc4n r-xoduu5">
<div class="css-1dbjc4n r-1niwhzg r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-1ny4l3l r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
<svg viewBox="0 0 24 24" aria-hidden="true" class="r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1hdv0qi">
<g>
<path d="M23.77 15.67c-.292-.293-.767-.293-1.06 0l-2.22 2.22V7.65c0-2.068-1.683-3.75-3.75-3.75h-5.85c-.414 0-.75.336-.75.75s.336.75.75.75h5.85c1.24 0 2.25 1.01 2.25 2.25v10.24l-2.22-2.22c-.293-.293-.768-.293-1.06 0s-.294.768 0 1.06l3.5 3.5c.145.147.337.22.53.22s.383-.072.53-.22l3.5-3.5c.294-.292.294-.767 0-1.06zm-10.66 3.28H7.26c-1.24 0-2.25-1.01-2.25-2.25V6.46l2.22 2.22c.148.147.34.22.532.22s.384-.073.53-.22c.293-.293.293-.768 0-1.06l-3.5-3.5c-.293-.294-.768-.294-1.06 0l-3.5 3.5c-.294.292-.294.767 0 1.06s.767.293 1.06 0l2.22-2.22V16.7c0 2.068 1.683 3.75 3.75 3.75h5.85c.414 0 .75-.336.75-.75s-.337-.75-.75-.75z"></path>
</g>
</svg>
</div>
<div class="css-1dbjc4n r-xoduu5 r-1udh08x"><span data-testid="app-text-transition-container" style="transition-property: transform; transition-duration: 0.3s; transform: translate3d(0px, 0px, 0px);"><span class="css-901oao css-16my406 r-poiln3 r-n6v787 r-1cwl3u0 r-vc7bo5 r-1pn2ns4 r-qvutc0"></span></span></div>
</div>
</div>
</div>
<div class="css-1dbjc4n r-18u37iz r-1h0z5md">
<div aria-label="0 Likes. Like" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-bt1l66 r-1ny4l3l r-bztko3 r-lrvibr" data-testid="like">
<div dir="ltr" class="css-901oao r-1awozwy r-9ilb82 r-6koalj r-37j5jr r-a023e6 r-16dba41 r-1h0z5md r-rjixqe r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0">
<div class="css-1dbjc4n r-xoduu5">
<div class="css-1dbjc4n r-1niwhzg r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-1ny4l3l r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
<svg viewBox="0 0 24 24" aria-hidden="true" class="r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1hdv0qi">
<g>
<path d="M12 21.638h-.014C9.403 21.59 1.95 14.856 1.95 8.478c0-3.064 2.525-5.754 5.403-5.754 2.29 0 3.83 1.58 4.646 2.73.814-1.148 2.354-2.73 4.645-2.73 2.88 0 5.404 2.69 5.404 5.755 0 6.376-7.454 13.11-10.037 13.157H12zM7.354 4.225c-2.08 0-3.903 1.988-3.903 4.255 0 5.74 7.034 11.596 8.55 11.658 1.518-.062 8.55-5.917 8.55-11.658 0-2.267-1.823-4.255-3.903-4.255-2.528 0-3.94 2.936-3.952 2.965-.23.562-1.156.562-1.387 0-.014-.03-1.425-2.965-3.954-2.965z"></path>
</g>
</svg>
</div>
<div class="css-1dbjc4n r-xoduu5 r-1udh08x"><span data-testid="app-text-transition-container" style="transition-property: transform; transition-duration: 0.3s; transform: translate3d(0px, 0px, 0px);"><span class="css-901oao css-16my406 r-poiln3 r-n6v787 r-1cwl3u0 r-vc7bo5 r-1pn2ns4 r-qvutc0"></span></span></div>
</div>
</div>
</div>
<div class="css-1dbjc4n r-18u37iz r-1h0z5md">
<div aria-expanded="false" aria-haspopup="menu" aria-label="Share Tweet" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-bt1l66 r-1ny4l3l r-bztko3 r-lrvibr">
<div dir="ltr" class="css-901oao r-1awozwy r-9ilb82 r-6koalj r-37j5jr r-a023e6 r-16dba41 r-1h0z5md r-rjixqe r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0">
<div class="css-1dbjc4n r-xoduu5">
<div class="css-1dbjc4n r-1niwhzg r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-1ny4l3l r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
<svg viewBox="0 0 24 24" aria-hidden="true" class="r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1hdv0qi">
<g>
<path d="M17.53 7.47l-5-5c-.293-.293-.768-.293-1.06 0l-5 5c-.294.293-.294.768 0 1.06s.767.294 1.06 0l3.72-3.72V15c0 .414.336.75.75.75s.75-.336.75-.75V4.81l3.72 3.72c.146.147.338.22.53.22s.384-.072.53-.22c.293-.293.293-.767 0-1.06z"></path>
<path d="M19.708 21.944H4.292C3.028 21.944 2 20.916 2 19.652V14c0-.414.336-.75.75-.75s.75.336.75.75v5.652c0 .437.355.792.792.792h15.416c.437 0 .792-.355.792-.792V14c0-.414.336-.75.75-.75s.75.336.75.75v5.652c0 1.264-1.028 2.292-2.292 2.292z"></path>
</g>
</svg>
</div>
</div>
</div>
</div>
**</div>**
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</article>
我试图找到它的代码:
try:
tweets = wait.until(EC.presence_of_all_elements_located(
(By.CSS_SELECTOR, "article[data-testid='tweet']a[href]")))
links = [link.get_attribute('href')
for link in tweets]
print(links)
except TimeoutException:
print("Couldn't detect any tweets")
错误:
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
(Session info: chrome=98.0.4758.82)
Stacktrace:
Backtrace:
Ordinal0 [0x00977AC3+2587331]
Ordinal0 [0x0090ADD1+2141649]
Ordinal0 [0x00803BB8+1063864]
Ordinal0 [0x008063D7+1074135]
Ordinal0 [0x0080629E+1073822]
Ordinal0 [0x00806510+1074448]
Ordinal0 [0x0082FF42+1244994]
Ordinal0 [0x008303CB+1246155]
Ordinal0 [0x0085A64C+1418828]
Ordinal0 [0x008486D4+1345236]
Ordinal0 [0x00858A0A+1411594]
Ordinal0 [0x008484A6+1344678]
Ordinal0 [0x008253F6+1201142]
Ordinal0 [0x008262E6+1204966]
GetHandleVerifier [0x00B1DF22+1680738]
GetHandleVerifier [0x00BD0DBC+2413564]
GetHandleVerifier [0x00A0D151+563089]
GetHandleVerifier [0x00A0BF13+558419]
Ordinal0 [0x0091081E+2164766]
Ordinal0 [0x00915508+2184456]
Ordinal0 [0x00915650+2184784]
Ordinal0 [0x0091F5BC+2225596]
BaseThreadInitThunk [0x753FFA29+25]
RtlGetAppContainerNamedObjectPath [0x76FE7A9E+286]
RtlGetAppContainerNamedObjectPath [0x76FE7A6E+238]
好像哪里不对?
这个错误信息...
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
...表示您使用的 定位器策略 无效 .
您需要在 article[data-testid='tweet']
和 a[href]
之间插入一个 space 以使其成为有效的 。如此有效,您的代码行应该是:
try:
tweets = wait.until(EC.presence_of_all_elements_located(
(By.CSS_SELECTOR, "article[data-testid='tweet'] a[href]")))
links = [link.get_attribute('href')
for link in tweets]
print(links)
except TimeoutException:
print("Couldn't detect any tweets")
但是,要提取 href
属性的 value,即 /kkanagas/status/1491996075573587994
您可以使用以下定位器策略:
try:
tweets = wait.until(EC.presence_of_all_elements_located(
(By.CSS_SELECTOR, "article[data-testid='tweet'] a[href*='status']")))
links = [link.get_attribute('href')
for link in tweets]
print(links)
except TimeoutException:
print("Couldn't detect any tweets")
我正在尝试从 中获取 属性“href”,即 "/kkanagas/status/1491996075573587994"。我知道它很长,但我找不到另一种方法来缩短它。只需搜索 "href",您就会看到 "/kkanagas/status/1491996075573587994" - 这正是我要找的。
<article aria-labelledby="id__fhryquvd10b id__xyp79hh56xk id__ufvrjjfijmj id__t3x5a40wpik id__bar6zadu3v9 id__bskm5hb18ak id__k838i141bg id__sxzis5aczdj id__ymircvocx1 id__ebx7cry08s9 id__qql66qtrpr id__l377gbo5gll id__rz84acqn28" role="article" tabindex="0" class="css-1dbjc4n r-1loqt21 r-18u37iz r-1ny4l3l r-1udh08x r-1qhn6m8 r-i023vh r-o7ynqc r-6416eg" data-testid="tweet">
<div class="css-1dbjc4n r-eqz5dr r-16y2uox r-1wbh5a2">
<div class="css-1dbjc4n r-16y2uox r-1wbh5a2 r-1ny4l3l">
<div class="css-1dbjc4n">
<div class="css-1dbjc4n">
<div class="css-1dbjc4n r-18u37iz">
<div class="css-1dbjc4n r-1iusvr4 r-16y2uox r-ttdzmv"></div>
</div>
</div>
<div class="css-1dbjc4n r-18u37iz">
<div class="css-1dbjc4n r-1awozwy r-1hwvwag r-18kxxzh r-1b7u577">
<div class="css-1dbjc4n r-18kxxzh r-1wbh5a2 r-13qz1uu">
<div class="css-1dbjc4n r-1wbh5a2 r-dnmrzs">
<div class="css-1dbjc4n r-1adg3ll r-bztko3" id="id__bskm5hb18ak" style="height: 48px; width: 48px;">
<div class="r-1adg3ll r-13qz1uu" style="padding-bottom: 100%;"></div>
<div class="r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-ipm5af r-13qz1uu">
<div class="css-1dbjc4n r-1adg3ll r-1pi2tsx r-1wyvozj r-bztko3 r-u8s1d r-1v2oles r-desppf r-13qz1uu">
<div class="r-1adg3ll r-13qz1uu" style="padding-bottom: 100%;"></div>
<div class="r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-ipm5af r-13qz1uu">
<div class="css-1dbjc4n r-sdzlij r-ggadg3 r-1udh08x r-u8s1d r-8jfcpp" style="height: calc(100% - -4px); width: calc(100% - -4px);">
<a href="/kkanagas" role="link" class="css-4rbku5 css-18t94o4 css-1dbjc4n r-1niwhzg r-1loqt21 r-1pi2tsx r-1ny4l3l r-o7ynqc r-6416eg r-13qz1uu">
<div class="css-1dbjc4n r-sdzlij r-1wyvozj r-1udh08x r-633pao r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);">
<div class="css-1dbjc4n r-1niwhzg r-1pi2tsx r-13qz1uu"></div>
</div>
<div class="css-1dbjc4n r-sdzlij r-1wyvozj r-1udh08x r-633pao r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);">
<div class="css-1dbjc4n r-kemksi r-1pi2tsx r-13qz1uu"></div>
</div>
<div class="css-1dbjc4n r-kemksi r-sdzlij r-1wyvozj r-1udh08x r-633pao r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);">
<div class="css-1dbjc4n r-1adg3ll r-1udh08x" style="">
<div class="r-1adg3ll r-13qz1uu" style="padding-bottom: 100%;"></div>
<div class="r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-ipm5af r-13qz1uu">
<div aria-label="" class="css-1dbjc4n r-1p0dtai r-1mlwlqe r-1d2f490 r-1udh08x r-u8s1d r-zchlnj r-ipm5af r-417010">
<div class="css-1dbjc4n r-1niwhzg r-vvn4in r-u6sd8q r-4gszlv r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-zchlnj r-ipm5af r-13qz1uu r-1wyyakw" style="background-image: url("https://pbs.twimg.com/profile_images/3202473125/48583ddb2e2de5d9193020f2cf38694b_bigger.jpeg");"></div>
<img alt="" draggable="true" src="https://pbs.twimg.com/profile_images/3202473125/48583ddb2e2de5d9193020f2cf38694b_bigger.jpeg" class="css-9pa8cd">
</div>
</div>
</div>
</div>
<div class="css-1dbjc4n r-sdzlij r-1wyvozj r-1udh08x r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);">
<div class="css-1dbjc4n r-172uzmj r-1pi2tsx r-1ny4l3l r-o7ynqc r-6416eg r-13qz1uu"></div>
</div>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="css-1dbjc4n r-1iusvr4 r-16y2uox r-1777fci r-kzbkwu">
<div class="css-1dbjc4n">
<div class="css-1dbjc4n r-zl2h9q">
<div class="css-1dbjc4n r-k4xj1c r-18u37iz r-1wtj0ep">
<div class="css-1dbjc4n r-1d09ksm r-18u37iz r-1wbh5a2">
<div class="css-1dbjc4n r-1wbh5a2 r-dnmrzs">
<a href="/kkanagas" role="link" class="css-4rbku5 css-18t94o4 css-1dbjc4n r-1loqt21 r-1wbh5a2 r-dnmrzs r-1ny4l3l">
<div class="css-1dbjc4n r-1awozwy r-18u37iz r-1wbh5a2 r-dnmrzs r-1ny4l3l" id="id__bar6zadu3v9">
<div class="css-1dbjc4n r-1awozwy r-18u37iz r-dnmrzs">
<div dir="auto" class="css-901oao r-1awozwy r-1fmj7o5 r-6koalj r-37j5jr r-a023e6 r-b88u0q r-rjixqe r-bcqeeo r-1udh08x r-3s2u2q r-qvutc0"><span class="css-901oao css-16my406 css-bfa6kz r-poiln3 r-bcqeeo r-qvutc0"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">Karthik K</span></span></div>
<div dir="auto" class="css-901oao r-1fmj7o5 r-xoduu5 r-18u37iz r-1q142lx r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-qvutc0"></div>
</div>
<div class="css-1dbjc4n r-18u37iz r-1wbh5a2 r-13hce6t">
<div dir="ltr" class="css-901oao css-bfa6kz r-9ilb82 r-18u37iz r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-qvutc0"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">@kkanagas</span></div>
</div>
</div>
</a>
</div>
<div dir="auto" aria-hidden="true" class="css-901oao r-9ilb82 r-1q142lx r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-s1qlax r-qvutc0"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">·</span></div>
<a href="/kkanagas/status/1491996075573587994" dir="auto" aria-label="1 minute ago" role="link" class="css-4rbku5 css-18t94o4 css-901oao r-9ilb82 r-1loqt21 r-1q142lx r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-3s2u2q r-qvutc0" id="id__sxzis5aczdj"><time datetime="2022-02-11T04:42:39.000Z">1m</time></a>
</div>
<div class="css-1dbjc4n r-1joea0r">
<div class="css-1dbjc4n r-1awozwy r-6koalj r-18u37iz">
<div class="css-1dbjc4n">
<div class="css-1dbjc4n r-18u37iz r-1h0z5md">
<div aria-expanded="false" aria-haspopup="menu" aria-label="More" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-bt1l66 r-1ny4l3l r-bztko3 r-lrvibr" data-testid="caret">
<div dir="ltr" class="css-901oao r-1awozwy r-9ilb82 r-6koalj r-37j5jr r-a023e6 r-16dba41 r-1h0z5md r-rjixqe r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0">
<div class="css-1dbjc4n r-xoduu5">
<div class="css-1dbjc4n r-1niwhzg r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-1ny4l3l r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
<svg viewBox="0 0 24 24" aria-hidden="true" class="r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1hdv0qi">
<g>
<circle cx="5" cy="12" r="2"></circle>
<circle cx="12" cy="12" r="2"></circle>
<circle cx="19" cy="12" r="2"></circle>
</g>
</svg>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="css-1dbjc4n">
<div class="css-1dbjc4n">
<div lang="en" dir="auto" class="css-901oao r-1fmj7o5 r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-bnwqim r-qvutc0" id="id__ebx7cry08s9"><span class="r-18u37iz"><a dir="ltr" href="/hashtag/USA?src=hashtag_click" role="link" class="css-4rbku5 css-18t94o4 css-901oao css-16my406 r-1cvl2hr r-1loqt21 r-poiln3 r-b88u0q r-bcqeeo r-qvutc0">#USA</a></span><span class="css-901oao css-16my406 r-poiln3 r-b88u0q r-bcqeeo r-qvutc0"></span><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0"> and </span><span class="r-18u37iz"><a dir="ltr" href="/hashtag/CZE?src=hashtag_click" role="link" class="css-4rbku5 css-18t94o4 css-901oao css-16my406 r-1cvl2hr r-1loqt21 r-poiln3 r-bcqeeo r-qvutc0">#CZE</a></span><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0"> are tied 0-0 at the end of the first period of this Women's Hockey QF. </span><img alt="Eyes" draggable="false" src="https://abs-0.twimg.com/emoji/v2/svg/1f440.svg" title="Eyes" class="r-4qtqp9 r-dflpy8 r-sjv1od r-zw8f10 r-10akycc r-h9hxbl"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">
</span><span class="r-18u37iz"><a dir="ltr" href="/hashtag/Beijing2022?src=hashtag_click" role="link" class="css-4rbku5 css-18t94o4 css-901oao css-16my406 r-1cvl2hr r-1loqt21 r-poiln3 r-bcqeeo r-3s2u2q r-qvutc0">#Beijing2022<img alt="" draggable="false" src="https://abs.twimg.com/hashflags/Beijing_Winter_Olympics_Beijing2022_2022/Beijing_Winter_Olympics_Beijing2022_2022.png" class="r-4qtqp9 r-dflpy8 r-9cviqr r-zw8f10 r-10akycc r-h9hxbl"></a></span><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0"> </span><span class="r-18u37iz"><a dir="ltr" href="/hashtag/IceHockey?src=hashtag_click" role="link" class="css-4rbku5 css-18t94o4 css-901oao css-16my406 r-1cvl2hr r-1loqt21 r-poiln3 r-bcqeeo r-3s2u2q r-qvutc0">#IceHockey<img alt="" draggable="false" src="https://abs.twimg.com/hashflags/Beijing_Winter_Olympics_IceHockey_2022/Beijing_Winter_Olympics_IceHockey_2022.png" class="r-4qtqp9 r-dflpy8 r-9cviqr r-zw8f10 r-10akycc r-h9hxbl"></a></span>
</div>
</div>
<div class="css-1dbjc4n"></div>
<div class="css-1dbjc4n">
<div aria-label="" role="group" class="css-1dbjc4n r-1ta3fxp r-18u37iz r-1wtj0ep r-1s2bzr4 r-1mdbhws" id="id__eui52vqqmkn">
<div class="css-1dbjc4n r-18u37iz r-1h0z5md">
<div aria-label="0 Replies. Reply" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-bt1l66 r-1ny4l3l r-bztko3 r-lrvibr" data-testid="reply">
<div dir="ltr" class="css-901oao r-1awozwy r-9ilb82 r-6koalj r-37j5jr r-a023e6 r-16dba41 r-1h0z5md r-rjixqe r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0">
<div class="css-1dbjc4n r-xoduu5">
<div class="css-1dbjc4n r-1niwhzg r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-1ny4l3l r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
<svg viewBox="0 0 24 24" aria-hidden="true" class="r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1hdv0qi">
<g>
<path d="M14.046 2.242l-4.148-.01h-.002c-4.374 0-7.8 3.427-7.8 7.802 0 4.098 3.186 7.206 7.465 7.37v3.828c0 .108.044.286.12.403.142.225.384.347.632.347.138 0 .277-.038.402-.118.264-.168 6.473-4.14 8.088-5.506 1.902-1.61 3.04-3.97 3.043-6.312v-.017c-.006-4.367-3.43-7.787-7.8-7.788zm3.787 12.972c-1.134.96-4.862 3.405-6.772 4.643V16.67c0-.414-.335-.75-.75-.75h-.396c-3.66 0-6.318-2.476-6.318-5.886 0-3.534 2.768-6.302 6.3-6.302l4.147.01h.002c3.532 0 6.3 2.766 6.302 6.296-.003 1.91-.942 3.844-2.514 5.176z"></path>
</g>
</svg>
</div>
<div class="css-1dbjc4n r-xoduu5 r-1udh08x"><span data-testid="app-text-transition-container" style="transition-property: transform; transition-duration: 0.3s; transform: translate3d(0px, 0px, 0px);"><span class="css-901oao css-16my406 r-poiln3 r-n6v787 r-1cwl3u0 r-vc7bo5 r-1pn2ns4 r-qvutc0"></span></span></div>
</div>
</div>
</div>
<div class="css-1dbjc4n r-18u37iz r-1h0z5md">
<div aria-expanded="false" aria-haspopup="menu" aria-label="0 Retweets. Retweet" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-bt1l66 r-1ny4l3l r-bztko3 r-lrvibr" data-testid="retweet">
<div dir="ltr" class="css-901oao r-1awozwy r-9ilb82 r-6koalj r-37j5jr r-a023e6 r-16dba41 r-1h0z5md r-rjixqe r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0">
<div class="css-1dbjc4n r-xoduu5">
<div class="css-1dbjc4n r-1niwhzg r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-1ny4l3l r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
<svg viewBox="0 0 24 24" aria-hidden="true" class="r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1hdv0qi">
<g>
<path d="M23.77 15.67c-.292-.293-.767-.293-1.06 0l-2.22 2.22V7.65c0-2.068-1.683-3.75-3.75-3.75h-5.85c-.414 0-.75.336-.75.75s.336.75.75.75h5.85c1.24 0 2.25 1.01 2.25 2.25v10.24l-2.22-2.22c-.293-.293-.768-.293-1.06 0s-.294.768 0 1.06l3.5 3.5c.145.147.337.22.53.22s.383-.072.53-.22l3.5-3.5c.294-.292.294-.767 0-1.06zm-10.66 3.28H7.26c-1.24 0-2.25-1.01-2.25-2.25V6.46l2.22 2.22c.148.147.34.22.532.22s.384-.073.53-.22c.293-.293.293-.768 0-1.06l-3.5-3.5c-.293-.294-.768-.294-1.06 0l-3.5 3.5c-.294.292-.294.767 0 1.06s.767.293 1.06 0l2.22-2.22V16.7c0 2.068 1.683 3.75 3.75 3.75h5.85c.414 0 .75-.336.75-.75s-.337-.75-.75-.75z"></path>
</g>
</svg>
</div>
<div class="css-1dbjc4n r-xoduu5 r-1udh08x"><span data-testid="app-text-transition-container" style="transition-property: transform; transition-duration: 0.3s; transform: translate3d(0px, 0px, 0px);"><span class="css-901oao css-16my406 r-poiln3 r-n6v787 r-1cwl3u0 r-vc7bo5 r-1pn2ns4 r-qvutc0"></span></span></div>
</div>
</div>
</div>
<div class="css-1dbjc4n r-18u37iz r-1h0z5md">
<div aria-label="0 Likes. Like" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-bt1l66 r-1ny4l3l r-bztko3 r-lrvibr" data-testid="like">
<div dir="ltr" class="css-901oao r-1awozwy r-9ilb82 r-6koalj r-37j5jr r-a023e6 r-16dba41 r-1h0z5md r-rjixqe r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0">
<div class="css-1dbjc4n r-xoduu5">
<div class="css-1dbjc4n r-1niwhzg r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-1ny4l3l r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
<svg viewBox="0 0 24 24" aria-hidden="true" class="r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1hdv0qi">
<g>
<path d="M12 21.638h-.014C9.403 21.59 1.95 14.856 1.95 8.478c0-3.064 2.525-5.754 5.403-5.754 2.29 0 3.83 1.58 4.646 2.73.814-1.148 2.354-2.73 4.645-2.73 2.88 0 5.404 2.69 5.404 5.755 0 6.376-7.454 13.11-10.037 13.157H12zM7.354 4.225c-2.08 0-3.903 1.988-3.903 4.255 0 5.74 7.034 11.596 8.55 11.658 1.518-.062 8.55-5.917 8.55-11.658 0-2.267-1.823-4.255-3.903-4.255-2.528 0-3.94 2.936-3.952 2.965-.23.562-1.156.562-1.387 0-.014-.03-1.425-2.965-3.954-2.965z"></path>
</g>
</svg>
</div>
<div class="css-1dbjc4n r-xoduu5 r-1udh08x"><span data-testid="app-text-transition-container" style="transition-property: transform; transition-duration: 0.3s; transform: translate3d(0px, 0px, 0px);"><span class="css-901oao css-16my406 r-poiln3 r-n6v787 r-1cwl3u0 r-vc7bo5 r-1pn2ns4 r-qvutc0"></span></span></div>
</div>
</div>
</div>
<div class="css-1dbjc4n r-18u37iz r-1h0z5md">
<div aria-expanded="false" aria-haspopup="menu" aria-label="Share Tweet" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-bt1l66 r-1ny4l3l r-bztko3 r-lrvibr">
<div dir="ltr" class="css-901oao r-1awozwy r-9ilb82 r-6koalj r-37j5jr r-a023e6 r-16dba41 r-1h0z5md r-rjixqe r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0">
<div class="css-1dbjc4n r-xoduu5">
<div class="css-1dbjc4n r-1niwhzg r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-1ny4l3l r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
<svg viewBox="0 0 24 24" aria-hidden="true" class="r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1hdv0qi">
<g>
<path d="M17.53 7.47l-5-5c-.293-.293-.768-.293-1.06 0l-5 5c-.294.293-.294.768 0 1.06s.767.294 1.06 0l3.72-3.72V15c0 .414.336.75.75.75s.75-.336.75-.75V4.81l3.72 3.72c.146.147.338.22.53.22s.384-.072.53-.22c.293-.293.293-.767 0-1.06z"></path>
<path d="M19.708 21.944H4.292C3.028 21.944 2 20.916 2 19.652V14c0-.414.336-.75.75-.75s.75.336.75.75v5.652c0 .437.355.792.792.792h15.416c.437 0 .792-.355.792-.792V14c0-.414.336-.75.75-.75s.75.336.75.75v5.652c0 1.264-1.028 2.292-2.292 2.292z"></path>
</g>
</svg>
</div>
</div>
</div>
</div>
**</div>**
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</article>
我试图找到它的代码:
try:
tweets = wait.until(EC.presence_of_all_elements_located(
(By.CSS_SELECTOR, "article[data-testid='tweet']a[href]")))
links = [link.get_attribute('href')
for link in tweets]
print(links)
except TimeoutException:
print("Couldn't detect any tweets")
错误:
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
(Session info: chrome=98.0.4758.82)
Stacktrace:
Backtrace:
Ordinal0 [0x00977AC3+2587331]
Ordinal0 [0x0090ADD1+2141649]
Ordinal0 [0x00803BB8+1063864]
Ordinal0 [0x008063D7+1074135]
Ordinal0 [0x0080629E+1073822]
Ordinal0 [0x00806510+1074448]
Ordinal0 [0x0082FF42+1244994]
Ordinal0 [0x008303CB+1246155]
Ordinal0 [0x0085A64C+1418828]
Ordinal0 [0x008486D4+1345236]
Ordinal0 [0x00858A0A+1411594]
Ordinal0 [0x008484A6+1344678]
Ordinal0 [0x008253F6+1201142]
Ordinal0 [0x008262E6+1204966]
GetHandleVerifier [0x00B1DF22+1680738]
GetHandleVerifier [0x00BD0DBC+2413564]
GetHandleVerifier [0x00A0D151+563089]
GetHandleVerifier [0x00A0BF13+558419]
Ordinal0 [0x0091081E+2164766]
Ordinal0 [0x00915508+2184456]
Ordinal0 [0x00915650+2184784]
Ordinal0 [0x0091F5BC+2225596]
BaseThreadInitThunk [0x753FFA29+25]
RtlGetAppContainerNamedObjectPath [0x76FE7A9E+286]
RtlGetAppContainerNamedObjectPath [0x76FE7A6E+238]
好像哪里不对?
这个错误信息...
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
...表示您使用的 定位器策略 无效
您需要在 article[data-testid='tweet']
和 a[href]
之间插入一个 space 以使其成为有效的
try:
tweets = wait.until(EC.presence_of_all_elements_located(
(By.CSS_SELECTOR, "article[data-testid='tweet'] a[href]")))
links = [link.get_attribute('href')
for link in tweets]
print(links)
except TimeoutException:
print("Couldn't detect any tweets")
但是,要提取 href
属性的 value,即 /kkanagas/status/1491996075573587994
您可以使用以下定位器策略:
try:
tweets = wait.until(EC.presence_of_all_elements_located(
(By.CSS_SELECTOR, "article[data-testid='tweet'] a[href*='status']")))
links = [link.get_attribute('href')
for link in tweets]
print(links)
except TimeoutException:
print("Couldn't detect any tweets")