Issue
I was working on some simple crawler to scrape retweet counts on Twitter. And I'm stuck with this:
<span class="ProfileTweet-actionCountForAria" id="profile-tweet-action-retweet-count-aria-123456789123456789">리트윗 0개</span>
That's the targeted tags I want to collect. And you can see the id of the tag has some varying id number for each user. So I was trying to collect those with find_elements_by_xpath like this:
retweets = driver.find_elements_by_xpath("//span[@id='profile-tweet-action-retweet-count-area-*'].text")
I thought * worked in some places in selenium, but it doesn't work in that code.
So, in short, how can I find elements with id including 'profile-tweet-action-retweet-count-area'?
Thanks for your attention. I couldn't find the questions like this (maybe I didn't search it with the right question, hmm), but good references or other links are also found with me!
Solution
Css selector would be :
span[id*="profile-tweet-action-retweet-count-aria"]
or a way better css selector would be :
span[id^='profile-tweet-action-retweet-count-aria']
If you have multiple entries with that, you can use find_elements
method which will give you list of web element.
If you don't want css selector and would want to stick with xpath :
//span[contains(@id,"profile-tweet-action-retweet-count-aria")]
Code :
list_retweet = driver.find_elements_by_xpath("//span[contains(@id,"profile-tweet-action-retweet-count-aria")]")
for retweet in list_retweet:
print(retweet.text)
Answered By - cruisepandey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.