简体   繁体   English

在Python中使用Selenium单击具有相同类名的所有元素

[英]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. 它们具有相同的类名,但ID不同。

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. 非常感谢您的帮助。

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. 这很不幸,你有两半的整体,你找不到id的多个元素,因为ID对于单个元素是唯一的。

so combine the iterative method you use with id and the find by elements with classes to get: 所以将你使用的迭代方法与id和使用类的元素的find相结合,得到:

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. 如果没有,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Selenium Python 中查找一个 class 名字的所有元素 - Find all elements of a class name in Selenium Python 在python中使用Selenium遍历所有元素 - Iterate through all elements using selenium in python 过滤由 selenium 通过.find_elements_by_class_name 使用 python 选择的元素 - Filtering elements selected by selenium through .find_elements_by_class_name using python 使用 Selenium 我试图单击页面上具有相同类的所有元素,但它不起作用 - Using Selenium I am trying to click all elements on the page with the same class but it is not working 如何使用 Selenium 和 Python 使用 find_elements_by_class_name() 单击元素 - How to click on element using find_elements_by_class_name() using Selenium and Python 当有多个相同的 class 并且没有使用 Python 的 id 元素时,使用 selenium 创建点击事件 - Create a click event using selenium when there are multiple same class and no id elements using Python 如何使用for循环点击selenium中具有相同XPATH的多个元素? - How to click through multiple elements with same XPATH in selenium using for loop? Python Webscrape - Selenium 'CLASS_NAME' - 'not' 包括所有元素 - Python Webscrape - Selenium 'CLASS_NAME' - 'not' to include all elements 将具有相同 class 的所有元素添加到具有 selenium 和 python 的列表 - Add all elements with the same class to list with selenium and python 点击列表元素 selenium python - click through list elements with selenium python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM