爬虫学习笔记-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.源码

python 复制代码
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()
相关推荐
ZhengEnCi22 分钟前
M3-markconv库找不到wkhtmltopdf问题
python
2301_764441333 小时前
LISA时空跃迁分析,地理时空分析
数据结构·python·算法
014-code3 小时前
订单超时取消与库存回滚的完整实现(延迟任务 + 状态机)
java·开发语言
lly2024064 小时前
组合模式(Composite Pattern)
开发语言
游乐码4 小时前
c#泛型约束
开发语言·c#
Dontla4 小时前
go语言Windows安装教程(安装go安装Golang安装)(GOPATH、Go Modules)
开发语言·windows·golang
chushiyunen4 小时前
python rest请求、requests
开发语言·python
cTz6FE7gA4 小时前
Python异步编程:从协程到Asyncio的底层揭秘
python
铁东博客4 小时前
Go实现周易大衍筮法三变取爻
开发语言·后端·golang
baidu_huihui4 小时前
在 CentOS 9 上安装 pip(Python 的包管理工具)
开发语言·python·pip