Python爬虫利用selenium滚动下滑
from selenium import webdriver
from selenium.webdriver.chrome import service
from selenium.webdriver.chrome.service import Service
import time
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
url = 'https://baidu.com'
browser.get(url)
input_button = browser.find_element(by='xpath', value='//input[@id="kw"]')
time.sleep(2)
input_button.send_keys('罗翔')
submit = browser.find_element(by='xpath', value='//input[@id="su"]')
submit.click()
time.sleep(3)
js_button = 'document.documentElement.scrollTop=100000'
browser.execute_script(js_button)
time.sleep(2)
next_page = browser.find_element(by='xpath', value='//a[@class="n"]')
next_page.click()
time.sleep(2)
browser.back()
time.sleep(2)
browser.forward()
time.sleep(3)
browser.quit()
主要就是在js代码那一块的之前,我们需要给它时间暂缓一下。
|