我試圖單擊網頁上的所有「like」按鈕。我知道如何點擊其中的一個,但我希望能夠點擊它們。他們具有相同的類名,但具有不同的ID。在Python中使用Selenium單擊具有相同類名稱的所有元素
我是否需要創建某種列表並告訴它單擊列表中的每個項目?有沒有寫「點擊全部」的方法?
這裏是我的代碼是什麼樣子(我刪除了登錄密碼):
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'
我知道我不能在列表上點擊,因爲它不是一個單一的對象,但我不知道如何否則我會去做這件事。
非常感謝您的幫助。
有人在java上回答了類似的問題,但我不知道如何將其轉換爲Python或者甚至可能。 http://stackoverflow.com/questions/15537930/getting-list-of-items-inside-div-using-selenium-webdriver –
我的解決方案沒有解決問題嗎? –