Loui Lin
Loui Lin

Reputation: 23

How can I click this button via selenium, it always says list object has no attribute 'click'

I want to click the "show more results" button in google image search via selenium.

The page is https://www.google.co.jp/search?q=circle&source=lnms&tbm=isch&sa=X&ved=0ahUKEwifxd-wqr_VAhXEopQKHQpSB_AQ_AUICigB&biw=1249&bih=927

The page should be scroll down 4 times so that you can see the button.

the html code is : enter image description here

my code is below

from selenium import webdriver
import time

class ImgCrawler:
    def __init__(self,searchlink = None):
        self.link = searchlink
        self.soupheader = {'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3176.2 Safari/537.36"}
        self.scrolldown = None
        self.jsdriver = None

    def getChromeCanary(self):
        self.jsdriver = webdriver.Chrome("f:\software\python\chromedriver_win32\chromedriver.exe")
        self.jsdriver.implicitly_wait(30)
        self.jsdriver.get(self.link)

    def scrollDownUseChromeCanary(self, scrolltimes = 1, sleeptime = 10):
        for i in range(scrolltimes):
            self.jsdriver.execute_script('window.scrollTo(0,document.body.scrollHeight);')
            time.sleep(sleeptime)

    def clickNextPage(self):
        return self.jsdriver.find_elements_by_css_selector("input.ksb._kvc").click()


if __name__ == '__main__':
    weblink = "https://www.google.com.hk/search?hl=en&site=imghp&tbm=isch&source=hp&biw=1461&bih=950&q=circle&oq=circle&gs_l=img.3..0l10.1497.3829.0.4548.10.9.1.0.0.0.185.1136.0j7.7.0....0...1.1.64.img..2.7.974...0i10k1.4YZkQiWXzGo"
    img = ImgCrawler(weblink)
    img.getChromeCanary()
    img.scrollDownUseChromeCanary(4,5)
    img.clickNextPage()

Upvotes: 1

Views: 70

Answers (2)

Andersson
Andersson

Reputation: 52695

find_elements_...() methods usually used

  • to get list of WebElements for following iteration through it:

    [element.text for element in driver.find_elements_by_tag_name('a')]
    
  • to locate element by index if there is no unique identifier:

    driver.find_elements_by_class_name('some_class_name')[1]
    

In other cases you can simply use find_element_...() methods that returns single WebElement

Try to replace

self.jsdriver.find_elements_by_css_selector("input.ksb._kvc").click()

with

self.jsdriver.find_element_by_css_selector("input.ksb._kvc").click()

Upvotes: 1

PRMoureu
PRMoureu

Reputation: 13347

You need to point the first element of your query :

self.jsdriver.find_elements_by_css_selector("input.ksb._kvc")[0].click()

http://selenium-python.readthedocs.io/locating-elements.html

To find multiple elements (these methods will return a list):

Upvotes: 2

Related Questions