Reputation: 23
I want to click the "show more results" button in google image search via selenium.
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
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
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