5

I am trying to click on all of the "like" buttons on a webpage. I know how to click on one of them, but I'd like to be able to click them all. They have the same class name, but different id's.

Do I need to create some sort of list and tell it to click on each one of the items on the list? Is there a way to write "click all"?

Here's what my code looks like (I removed the login code):

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.set_window_size(650, 700)
browser.get('http://iconosquare.com/viewer.php#/tag/searchterm/grid')

mobile = browser.find_element_by_id('open-menu-mobile')
mobile.click()
search = browser.find_element_by_id('getSearch')
search.click()
search.send_keys('input search term' + Keys.RETURN)

#this gets me to the page I want to click the likes
fitness = browser.find_element_by_css_selector("a[href*='fitness/']")
fitness.click()

#here are the different codes I've tried to use to click all of the "like buttons"

#tried to create a list of all elements with "like" in the id and click on all of them.  It didn't work.
like = browser.find_elements_by_id('like')
for x in range(0,len(like)):
    if like[x].is_displayed():
        like[x].click()

#tried to create a list by class and click on everything within the list and it didn't work.
like = browser.find_elements_by_class_name('like_picto_unselected')
like.click()

AttributeError: 'list' object has no attribute 'click'

I know I can't click on a list because it isn't a single object, but I have no idea how I'd go about this otherwise.

Your help is greatly appreciated.

2
  • someone answered a similar question on java, but I don't know how to convert it to Python or if it is even possible. stackoverflow.com/questions/15537930/… Commented Jul 10, 2015 at 21:07
  • Did my solution not resolve the issue? Commented Jul 10, 2015 at 21:48

1 Answer 1

18

This is unfortunate, you got two halves of the whole, you cannot find multiple elements by id as ID is unique to a single element.

so combine the iterative method you use with id and the find by elements with classes to get:

like = browser.find_elements_by_class_name('like_picto_unselected')
for x in range(0,len(like)):
    if like[x].is_displayed():
        like[x].click()

I strongly suspect this will work for you. Please tell me if not.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your response!! It worked perfectly. I had no idea that you could only find one element using the find_id method.
this works awesome! would it be possible to do a print on how many are found? or how many it clicked?
@droopie you can just use len(like) to get the number of elements found with that class name. Meanwhile, to get the number of buttons that were clicked, you can just create an extra variable and increment it below the if block to get that count!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.