练习使用selenium操作键盘进行下拉式翻页,以Bing搜索“中共一大会址”为关键字的图片搜索结果为例
思考:如何判断是否到底?
from selenium import webdriver
from selenium.webdriver.common.by import By
chrome_driver_file = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
b = webdriver.Chrome(executable_path=chrome_driver_file)
url = "https://cn.bing.com/images/search?q=中共一大会址"
b.get(url)
pics = b.find_elements(By.XPATH, '//*[@id="mmComponent_images_1"]/ul/li')
print(len(pics))
C:\Users\fangb\AppData\Local\Temp\ipykernel_16472\60224640.py:5: DeprecationWarning: executable_path has been deprecated, please pass in a Service object b = webdriver.Chrome(executable_path=chrome_driver_file)
35
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
for i in range(3):
ActionChains(b).key_down(Keys.END).key_up(Keys.END).perform()
#暂停5秒等待页面加载
time.sleep(5)
pics = b.find_elements(By.XPATH, '//*[@id="mmComponent_images_1"]/ul/li')
print(len(pics))
70 105 140
b.quit()