python之selenium调用xpath实现网页操作
源码
#coding=utf-8
#program:供本司运维人员填写bbs日志使用
#author:sundz
#V1 20220623 创建代码 实现点击
#V2 20220626 从Excel中获取数据自动填充;实现从交易所网站获取当日实时成交量
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import xlrd
import datetime
s = Service("msedgedriver.exe")
driver = webdriver.Edge(service=s)
driver.get('http://*********************') #打开网页
#URL = input('Enter Your BBS-URL : ')
#driver.get(URL) #打开网页
driver.maximize_window() #最大化窗口
#进入填写当前日志frame
driver.switch_to.frame("leftFrame")
driver.find_element(by=By.XPATH, value="/html/body/a[1]").click()
sleep(1)
driver.switch_to.default_content() #切换回主frame 以进入列表frame进行填写
driver.switch_to.frame("mainFrame") #列表和进入列表的frame都是mainFrame
def commit(): #提交
driver.find_element(by=By.XPATH, value="/html/body/form/p/input[1]").click() #提交按钮
#sleep(1)
#处理网页弹框
alert=driver.switch_to.alert
#print(alert.text)
alert.accept()
alert.accept()
def review(): #复核
driver.find_element(by=By.XPATH, value="/html/body/form/p/input[2]").click() #复核按钮
#sleep(1)
#处理网页弹框
alert=driver.switch_to.alert
#print(alert.text)
alert.accept()
alert.accept()
sleep(1)
def listname(listname): #进入表格
driver.find_element(by=By.XPATH, value=listname).click() #进入表格 也是mainframe
commit() #提交
#复核
driver.find_element(by=By.XPATH, value=listname).click()
#sleep(1)
review() #复核
def xls_to_URL(a,b,c): #excel数据处理进网页,a b是excel坐标,c是需要填充的xpath
filename = (datetime.datetime.now().strftime("%Y%m%d") + '运行日报委托成交统计.xls')
work_book = xlrd.open_workbook(filename)
table = work_book.sheet_by_index(0) #通过索引获取sheet
work_book.sheet_loaded(0) # 检查某个sheet是否导入完毕
str_0 = table.cell_value(a,b) # 返回单元格中的数据类型
str_1 = str_0.split(":", 2)[1] #单元格数据处理完成
#填充网页数据,先置空再填充
driver.find_element(by=By.XPATH, value=c).send_keys("")
driver.find_element(by=By.XPATH, value=c).send_keys(str_1)
#listname("/html/body/table/tbody/tr[2]/td[2]/a")
#对第一个列表处理 因为填充数据
driver.find_element(by=By.XPATH, value="/html/body/table/tbody/tr[2]/td[2]/a").click() #进入表格 也是mainframe
xls_to_URL(1,2,"/html/body/form/table[2]/tbody/tr[21]/td[5]/textarea")
xls_to_URL(2,2,"/html/body/form/table[2]/tbody/tr[22]/td[5]/textarea")
xls_to_URL(3,2,"/html/body/form/table[2]/tbody/tr[23]/td[5]/textarea")
xls_to_URL(4,2,"/html/body/form/table[2]/tbody/tr[24]/td[5]/textarea")
xls_to_URL(5,2,"/html/body/form/table[2]/tbody/tr[25]/td[5]/textarea")
xls_to_URL(6,2,"/html/body/form/table[2]/tbody/tr[26]/td[5]/textarea")
xls_to_URL(7,2,"/html/body/form/table[2]/tbody/tr[27]/td[5]/textarea")
xls_to_URL(8,2,"/html/body/form/table[2]/tbody/tr[30]/td[5]/textarea")
xls_to_URL(9,2,"/html/body/form/table[2]/tbody/tr[31]/td[5]/textarea")
xls_to_URL(10,2,"/html/body/form/table[2]/tbody/tr[32]/td[5]/textarea")
xls_to_URL(11,2,"/html/body/form/table[2]/tbody/tr[33]/td[5]/textarea")
xls_to_URL(12,2,"/html/body/form/table[2]/tbody/tr[34]/td[5]/textarea")
xls_to_URL(13,2,"/html/body/form/table[2]/tbody/tr[35]/td[5]/textarea")
xls_to_URL(14,2,"/html/body/form/table[2]/tbody/tr[36]/td[5]/textarea")
commit() #提交
driver.find_element(by=By.XPATH, value="/html/body/table/tbody/tr[2]/td[2]/a").click() #再次进入复核
review() #复核
listname("/html/body/table/tbody/tr[3]/td[2]/a")
listname("/html/body/table/tbody/tr[4]/td[2]/a")
listname("/html/body/table/tbody/tr[5]/td[2]/a")
listname("/html/body/table/tbody/tr[6]/td[2]/a")
listname("/html/body/table/tbody/tr[7]/td[2]/a")
listname("/html/body/table/tbody/tr[8]/td[2]/a")
listname("/html/body/table/tbody/tr[9]/td[2]/a")
listname("/html/body/table/tbody/tr[10]/td[2]/a")
listname("/html/body/table/tbody/tr[11]/td[2]/a")
listname("/html/body/table/tbody/tr[12]/td[2]/a")
listname("/html/body/table/tbody/tr[13]/td[2]/a")
listname("/html/body/table/tbody/tr[14]/td[2]/a")
listname("/html/body/table/tbody/tr[15]/td[2]/a")
listname("/html/body/table/tbody/tr[16]/td[2]/a")
listname("/html/body/table/tbody/tr[17]/td[2]/a")
listname("/html/body/table/tbody/tr[18]/td[2]/a")
listname("/html/body/table/tbody/tr[19]/td[2]/a")
listname("/html/body/table/tbody/tr[20]/td[2]/a")
listname("/html/body/table/tbody/tr[21]/td[2]/a")
listname("/html/body/table/tbody/tr[28]/td[2]/a")
listname("/html/body/table/tbody/tr[30]/td[2]/a")
listname("/html/body/table/tbody/tr[31]/td[2]/a")
listname("/html/body/table/tbody/tr[32]/td[2]/a")
#以下内容填充沪深成交额
#上海成交额
driver.get('http://q.10jqka.com.cn/zs/detail/code/1A0001/') #打开网页
a = driver.find_element(by=By.XPATH, value="/html/body/div[2]/div[3]/div[2]/div/div/div[1]/div[2]/dl[7]/dd").text
#深圳成交额
driver.get('http://www.szse.cn/') #打开网页
b = driver.find_element(by=By.XPATH, value="/html/body/div[11]/div/div/div[1]/div[3]/div[1]/div[1]/div/ul/li[1]/span[4]").text
c = "沪:" + a + " 深:" + b
driver.get('http://192.168.28.101/WebLog/index.jsp?userName=sundz') #打开网页
#进入填写当前日志frame
driver.switch_to.frame("leftFrame")
driver.find_element(by=By.XPATH, value="/html/body/a[1]").click()
sleep(1)
driver.switch_to.default_content() #切换回主frame 以进入列表frame进行填写
driver.switch_to.frame("mainFrame") #列表和进入列表的frame都是mainFrame
driver.find_element(by=By.XPATH, value="/html/body/table/tbody/tr[2]/td[2]/a").click() #进入表格 也是mainframe
#print (c)
driver.find_element(by=By.XPATH, value="/html/body/form/table[2]/tbody/tr[20]/td[5]/textarea").send_keys(c)
commit() #提交
driver.find_element(by=By.XPATH, value="/html/body/table/tbody/tr[2]/td[2]/a").click() #再次进入复核
review() #复核
过程中遇到的主要问题
1.调用xpath之前 需要先调用frame 以定位!!!
2.定位到新的frame 需要先退出之前的frame
主要涉及的技术处理
1.xpath调用
2.浏览器驱动调用
3.网页弹框处理
4.xlrd库
存在疑问:
1.对已经填充xpath数据的网页 怎么把数据置空
2.检测浏览器是否打开 打开则继续使用