爬虫学习笔记-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()
相关推荐
进击的明明2 小时前
当AI开始“边想边干”:深入聊聊ReAct,以及如何用Python手搓一个简单Agent
python·面试·agent
全栈弄潮儿3 小时前
Python实战第8期:函数
后端·python·编程语言
卷无止境5 小时前
暗流与破局:AI时代的代码安全审查工具全景指南
后端·python
卷无止境7 小时前
一人公司的全栈选型指南:让 AI Agent 写得快,也让生意跑得稳
后端·python
WJ.Polar8 小时前
Java封装、继承、多态
java·开发语言
摇滚侠9 小时前
Java 中如何表示负数,有符号和无符号的区别,补码的计算规则,char byte short int long 的范围
java·开发语言
滴啦嘟啦哒9 小时前
【具身笔记】【π0.5 微调与仿真】五、在MuJoCo中实现可视化仿真
笔记·python·vla·具身
weixin_307779139 小时前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab
全栈练习生9 小时前
AI安全与红队测试
python·ai
爱读书的小胖9 小时前
Codewars:Rot13(C)解题分析
c语言·开发语言