爬虫学习笔记-selenium交互

 1.导包

from selenium import webdriver 
import time
from selenium.webdriver.common.by import By

2.打开浏览器访问百度页面,睡眠2秒

url = 'https://www.baidu.com'
browser = webdriver.Chrome()
browser.get(url)
time.sleep(2)

3.获取输入框,输入搜索的内容,睡眠2秒

input = browser.find_element(By.ID,'kw')
input.send_keys('周杰伦')
time.sleep(2)

4.获取百度一下按钮,点击,睡眠2秒

button = browser.find_element(By.ID,'su')
button.click()
time.sleep(2)

5.执行js代码,滑动到浏览器最下方,睡眠2秒

js_button = 'document.documentElement.scrollTop = 100000'
browser.execute_script(js_button)
time.sleep(2)

6.获取下一页按钮,点击一下,睡眠2秒

next_button = browser.find_element(By.XPATH,"//a[@class='n']")
next_button.click()
time.sleep(2)

7.执行后退,前进操作

browser.back()
time.sleep(2)
browser.forward()
time.sleep(2)

8.关闭浏览器

browser.quit()

9.源码

from selenium import webdriver

import time

from selenium.webdriver.common.by import By

url = 'https://www.baidu.com'
browser = webdriver.Chrome()
browser.get(url)
time.sleep(2)

input = browser.find_element(By.ID,'kw')
input.send_keys('周杰伦')
time.sleep(2)
button = browser.find_element(By.ID,'su')
button.click()
time.sleep(2)
js_button = 'document.documentElement.scrollTop = 100000'
browser.execute_script(js_button)
time.sleep(2)
next_button = browser.find_element(By.XPATH,"//a[@class='n']")
next_button.click()
time.sleep(2)
browser.back()
time.sleep(2)
browser.forward()
time.sleep(2)
browser.quit()

你可能感兴趣的:(python,开发语言)