在 Python 中使用 Selenium 单击具有相同 class 名称的所有元素
Using Selenium in Python to click through all elements with the same class name
我试图点击网页上的所有 "like" 按钮。我知道如何点击其中一个,但我希望能够全部点击。它们具有相同的 class 名称,但不同的 ID。
我是否需要创建某种列表并让它点击列表中的每一项?有没有办法写成"click all"?
我的代码如下所示(我删除了登录代码):
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'
我知道我不能点击一个列表,因为它不是一个单一的对象,但我不知道否则我会怎么做。
非常感谢您的帮助。
这很不幸,你得到了整体的两半,你不能通过 id 找到多个元素,因为 ID 对于单个元素是唯一的。
所以将您使用的迭代方法与 id 和按元素查找与 类 结合起来得到:
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()
我强烈怀疑这对你有用。如果没有请告诉我。
我试图点击网页上的所有 "like" 按钮。我知道如何点击其中一个,但我希望能够全部点击。它们具有相同的 class 名称,但不同的 ID。
我是否需要创建某种列表并让它点击列表中的每一项?有没有办法写成"click all"?
我的代码如下所示(我删除了登录代码):
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'
我知道我不能点击一个列表,因为它不是一个单一的对象,但我不知道否则我会怎么做。
非常感谢您的帮助。
这很不幸,你得到了整体的两半,你不能通过 id 找到多个元素,因为 ID 对于单个元素是唯一的。
所以将您使用的迭代方法与 id 和按元素查找与 类 结合起来得到:
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()
我强烈怀疑这对你有用。如果没有请告诉我。