Python教程模拟网页点击
发布于 2021-04-15 21:41 ,所属分类:知识学习综合资讯
还有一种动态数据,例如GPS这类系统,需要鼠标点击列表中的元素完成向地图服务器发送请求,然后接受反馈的信息,然后在页面弹出一个窗口显示位置信息。
首先需要完成第一步,打开浏览器,输入账号密码,点击登录,之后再是找到列表并点击列表中的元素。
爬虫准备:
安装selenium : :cmd安装pip installselenium
下载chromedriver ,下载后将chromedriver.exe文件放至 python.exe所在目录:
http://chromedriver.storage.googleapis.com/index.html下载
(根据你的google浏览器版本下载对应的chromedriver 版本)
打开登录页面
右键审查元素,点击右上角小箭头,鼠标指向需要填写账号的位置,点击,右边elements中会直接找到对应的网页代码位置






from selenium import webdriver
import time
opt = webdriver.ChromeOptions() #创建浏览
# opt.set_headless() #无窗口模式
driver = webdriver.Chrome(options=opt) #创建浏览器对象
driver.get('网页') #打开网页
# driver.maximize_window() #最大化窗口
time.sleep(1) #加载等待
# 填入账号
elem = driver.find_element_by_name("username")
# 清空原有内容
elem.clear()
# 填入账号
elem.send_keys("账号")
time.sleep(1) #加载等待
# 填入密码
elem = driver.find_element_by_name("password")
# 清空原有内容
elem.clear()
# 填入密码
elem.send_keys("密码")
time.sleep(1) #加载等待
# 得到 登录按钮并单击
elem = driver.find_element_by_xpath("/html/body/div/div[2]/div/form/div/input").click()
time.sleep(2) #加载等待
driver.switch_to.frame("mainFrame") #进入iframe不然无法操作下面的元素
time.sleep(2) #加载等待
driver.find_element_by_xpath("//table[@id='contentTable']//tr[2]/td[1]").click() #点击列表中的元素
/html/body/div/div[2]/div/table/tbody/tr[2]
为了帮助大家更轻松的学好Python,我给大家分享一套Python学习资料,帮助大家在成为Python高手的道路上披荆斩棘
需要这份资料,那么帮忙点个再看转发一下分享出去
然后加下python的学习qq群:775063632(或者点击下方阅读全文即可跳转至加群页面!)
PS:如果觉得本篇文章对您有所帮助,欢迎关注、订阅、顶!

觉得不错,点个“在看”然后转发出去
相关资源