爬虫学习笔记-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()
相关推荐
用户83580861879144 分钟前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L17 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅17 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅17 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L18 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅18 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L19 小时前
python的类&继承
python
Warson_L19 小时前
类型标注/type annotation
python
ThreeS21 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵1 天前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏