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/