爬虫学习笔记-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()
相关推荐
共享家95274 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
疯狂的喵4 小时前
C++编译期多态实现
开发语言·c++·算法
2301_765703144 小时前
C++中的协程编程
开发语言·c++·算法
m0_748708054 小时前
实时数据压缩库
开发语言·c++·算法
Hgfdsaqwr5 小时前
Python在2024年的主要趋势与发展方向
jvm·数据库·python
lly2024065 小时前
jQuery Mobile 表格
开发语言
一晌小贪欢5 小时前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
小文数模5 小时前
2026年美赛数学建模C题完整参考论文(含模型和代码)
python·数学建模·matlab
惊讶的猫5 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
Halo_tjn5 小时前
基于封装的专项 知识点
java·前端·python·算法