Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Selenium in Python to click through all elements with the same class name

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.

like image 375
Gus Gabel Avatar asked Jul 10 '15 20:07

Gus Gabel


People also ask

How do you get the second element with the same class name in Selenium?

We can get text from multiple elements with the same class in Selenium webdriver. We have to use find_elements_by_xpath(), find_elements_by_class_name() or find_elements_by_css_selector() method which returns a list of all matching elements.

Can we find element based on class name using Selenium?

Selenium By.This method makes it possible to locate an element by referencing its class name. The class() method is found inside the By class of the Selenium WebDriver JavaScript library. The class also contains other alternative methods for locating elements. let loginBtn = await driver.

How do I select a class in Selenium Python?

New Selenium IDE We can select a drop-down menu option value with Selenium webdriver. The Select class in Selenium is used to handle drop-down. In an html document, the drop-down is identified with the <select> tag.

How can Selenium select each div separately that have the same class?

To identify each of these div elements separately, we shall use the method findElements and pass the value of the class attribute as a parameter to this method. The findElements method returns a list of matching elements. We have to iterate through this list and identify each div separately.


1 Answers

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.

like image 134
Going hamateur Avatar answered Oct 17 '22 12:10

Going hamateur