简体   繁体   中英

Python Selenium to automate Twitter likes

I'm trying to automate the process of liking all the tweets in one page with Selenium for Python but I can't understand how to perform the click action on the specific like button.

This is the html for the like button:

<button class="ProfileTweet-actionButton js-actionButton js-actionFavorite" type="button" aria-describedby="profile-tweet-action-favorite-count-aria-1056501001434349568">
    <div class="IconContainer js-tooltip" data-original-title="Like">
      <span role="presentation" class="Icon Icon--heart Icon--medium"></span>
      <div class="HeartAnimation"></div>
      <span class="u-hiddenVisually">Like</span>
    </div>
      <span class="ProfileTweet-actionCount">
    <span class="ProfileTweet-actionCountForPresentation" aria-hidden="true">1</span>
  </span>

  </button>

I'm trying this but the action does not take place:

driver.find_element_by_class_name("js-actionFavorite").click()

Any suggestions?

I may be wrong, but you cannot use compound names with find_element_by_class_name.

Try this

driver.find_element_by_css_selector('button[class="ProfileTweet-actionButton js-actionButton js-actionFavorite"]').click()

I'm not sure if you're already with the new twitter layout. In my case, everything has already changed. I was able to do the like in the first item of the profile only.

# var for css selection item

like_it = "section.css-1dbjc4n > div:nth-child(2) > div:nth-child(1) > 
div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1)
> article:nth-child(1) > div:nth-child(2) > div:nth-child(2) > div:nth-child(2) >
div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1)"

# like 1st tweet timeline user.
driver.find_element_by_css_selector(like_it).click()

ps: To give like in several profile tweets, I still have not figured out how to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM