selenium--python,登陆,查询,下载

本文介绍使用Selenium进行自动化测试的方法,包括在Firefox浏览器中执行数据库操作网站的登陆、查询及下载功能。通过Python代码实现页面操作,如输入、获取结果等,并详细记录了设置下载属性的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

selenium的python操作

页面操作动作:登陆,页面跳转,输入,获取结果,下载

需求:自动在数据库操作网站,执行登陆,点击到相应的数据库,数据sql,点击查询,获取结果,下载保存;

代码实现过程中,保存动作失败

上代码:

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from lxml.html import fromstring, tostring
from lxml import etree
import time



content='select * from monitor_database'
#filename='rowchange-'+str(time.strftime('%Y-%m-%d',time.localtime()))
filename='rowchange'
print(filename)
#设置firefox浏览器的下载属性
profile = webdriver.FirefoxProfile()
#profile.set_preference('browser.download.dir', 'd:\\')#指定下载路径
#profile.set_preference('browser.download.folderList', 2)#设置成 2 表示使用自定义下载路径;设置成 0 表示下载到桌面;设置成 1 表示下载到默认路径
#profile.set_preference('browser.download.manager.showWhenStarting', False)#在开始下载时是否显示下载管理器
#profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/vnd.ms-excel')#对所给出文件类型不再弹出框进行询问

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList',2)
profile.set_preference('browser.download.dir','C:\\Users\\pwu\\Desktop\\')
profile.set_preference('browser.download.manager.showWhenStarting',False)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk','application/vnd.ms-excel')


driver=webdriver.Firefox(firefox_profile=profile)
try:
	
	driver.get("http://8.1.8.111/login/")
	#将浏览器最大化显示
	driver.maximize_window()
	time.sleep(2)
	# 填充用户名 密码 验证码
	driver.find_element_by_id("username").send_keys("pengwu")
	driver.find_element_by_id("password").send_keys("password")
	# 点击登录 登陆成功
	# driver.find_element_by_class_name("ant-btn ant-btn-primary ant-btn-lg").click()
	driver.find_element_by_xpath("//button").click()
	sreach_window = driver.current_window_handle
	time.sleep(2)
	driver.find_element_by_xpath('//div[@class="ant-select-selection-selected-value"]').click()
	sreach_window = driver.current_window_handle
	driver.find_element_by_id('db-320dc265-bb29-42bb-89ce-25d1898189cb').click()

	# driver.find_element_by_xpath('//div[@class="ant-col-21"]').send_keys(Keys.ENTER)
	driver.find_element_by_xpath('//div[@class="ant-col-21"]').click()
	
	time.sleep(2)
	driver.find_element_by_xpath('//textarea[@class="ace_text-input"]').send_keys(content)
	driver.find_element_by_id('execute-button').click()
	time.sleep(5)
	now_handle = driver.current_window_handle
	#点击下载按钮
	driver.find_element_by_xpath('//div[@style="display: inline-block; position: relative;"]').click()
	time.sleep(1)
	#点击存为csv格式的文件按钮
	driver.find_element_by_xpath('//div[@style="margin-left: 0px; padding: 0px 16px; position: relative;"]').click()
	#获取当前页
	driver.current_window_handle
	#获取另存为消息框
	dowload=driver.find_element_by_xpath('//input[starts-with(@id,"undefined-undefined--")]')
	#print(dowload)
	time.sleep(1)
	#点击另存为,文件名
	dowload.click()
	time.sleep(1)
	#点击另存为,输入文件名
	dowload.send_keys(filename)
	time.sleep(1)
	#点击另存为消息框的确定
	#qd=driver.find_element_by_xpath('//input[starts-with(@id,"undefined-undefined--")/../../../div[2]/button[2]')[0]
	driver.find_element_by_xpath('//input[starts-with(@id,"undefined-undefined--")]/../../../div[2]/button[2]').click()
	time.sleep(5)

	print(driver.current_url)
	print(driver.get_cookies())
	#print(driver.page_source)
	time.sleep(5)
finally:
	driver.close()

执行结果:
后期会不上录制的视频,上传在b站了,sky
https://www.bilibili.com/video/av34090029/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值