Write a python Google automated test library by yourself

Before that, I have been exposed to selenium . There are various language calling interfaces and support for various browser kernels, but we have to build an automated test library by ourselves for convenience. Throw the library under our program and use it without dependencies. Currently, it only supports window Each kernel browser has its own web debugging protocol. Here I use the WebHttp WebSocket communication used by Google's Chrome DevTools . Please find the documentation yourself. We only give the library files written in c++ and the python package. The following gives py package code

# -*- coding: UTF-8 -*-
import IChromeLink

def StartUp(host):
	IChromeLink.StartUp(host+" --remote-debugging-port=9222")
def GetTabs(index):
	return IChromeLink.GetTabs("localhost:9222")[index]["WebSocketDebuggerUrl"]
class ChromeTagLink:
    m_pChromeAccessor = 0
    def __init__(self,ws):
        ChromeTagLink.m_pChromeAccessor=IChromeLink.chrome_start(ws)
    def IsValid(self):
        return IChromeLink.chrome_IsValid(ChromeTagLink.m_pChromeAccessor)
    def Navigate(self,url,Referrer=""):
        return IChromeLink.chrome_Navigate(ChromeTagLink.m_pChromeAccessor,url,Referrer)
    def ReLoad(self,Cache):
        return IChromeLink.chrome_ReLoad(ChromeTagLink.m_pChromeAccessor,Cache)
    def UpdateFrame(self):
        return IChromeLink.chrome_UpdateFrame(ChromeTagLink.m_pChromeAccessor)
    def IsLoading(self):
        return IChromeLink.chrome_IsLoading(ChromeTagLink.m_pChromeAccessor)
    def GetChildFrameCount(self):
        return IChromeLink.chrome_GetChildFrameCount(ChromeTagLink.m_pChromeAccessor)
    def GetMainFrame(self):
        return IChromeLink.chrome_GetMainFrame(ChromeTagLink.m_pChromeAccessor)
    def contextid_count(self):
        return IChromeLink.chrome_contextid_count(ChromeTagLink.m_pChromeAccessor)
    def Eval(self,code):
		fid=self.GetMainFrame()["FrameId"]
		for x in self.contextid_count():
			if x["FrameId"]==fid:
				return IChromeLink.chrome_Eval(ChromeTagLink.m_pChromeAccessor,x["dwExecutionContextId"],code)
    def Close(self):
        IChromeLink.Close(ChromeTagLink.m_pChromeAccessor)
    def hmbb(self):
        return "666"
    def css_get_value(self,path):#获取元素值
        return self.Eval("document.querySelector('{}').value".format(path))
    def css_set_value(self,path,value):#设置元素值
        return self.Eval("document.querySelector('{}').value='{}'".format(path,value))
    def css_get_innerText(self,path):#获取元素text文本
        return self.Eval("document.querySelector('{}').innerText".format(path))	
    def css_set_innerText(self,path,innerText):#设置元素text文本
        return self.Eval("document.querySelector('{}').innerText='{}'".format(path,innerText))	
    def css_get_innerHTML(self,path):#获取元素html文本
        return self.Eval("document.querySelector('{}').innerHTML".format(path))	
    def css_set_innerHTML(self,path,innerHTML):#设置元素html文本
        return self.Eval("document.querySelector('{}').innerHTML='{}'".format(path,innerHTML))
    def css_get_getAttribute(self,path,name):#元素得到属性
        return self.Eval("document.querySelector('{}').getAttribute('{}')".format(path,name))	
    def css_set_getAttribute(self,path,name,value):#元素置属性
        return self.Eval("document.querySelector('{}').setAttribute('{}','{}')".format(path,name,value))
    def css_event(self,path):#元素事件
        return self.Eval("document.querySelector('{}').event()".format(path))	
    def css_blur(self,path):#取消元素焦点
        return self.Eval("document.querySelector('{}').blur()".format(path))		
    def css_focus(self,path):#元素获取焦点
        return self.Eval("document.querySelector('{}').focus()".format(path))		
    def css_click(self,path):#元素点击
        return self.Eval("document.querySelector('{}').click()".format(path))
    def css_checked(self,path,checked):#checked  false|true
        return self.Eval("document.querySelector('{}').checked={}".format(path,checked))
    def css_get_action(self,path):#读取表单地址
        return self.Eval("document.querySelector('{}').action".format(path))
    def css_set_action(self,path,action):#设置表单地址
        return self.Eval("document.querySelector('{}').action='{}'".format(path,action))	
    def css_reset_action(self,path):#重置表单
        return self.Eval("document.querySelector('{}').reset()".format(path))		
    def css_submit_action(self,path):#提交表单
        return self.Eval("document.querySelector('{}').submit()".format(path))		
    def css_set_scrollBy(self,xn,yn):#置滚动条距离
        return self.Eval("window.scrollBy({},{})".format(xn,yn))			
    def css_set_scrollTo(self,x,y):#置滚动条位置
        return self.Eval("window.scrollTo({},{})".format(x,y))		
    def css_get_htmls(self,path):#取元素列表
        atd="var htmls=[];[].forEach.call(document.querySelectorAll('"+path+"'),function(a){htmls.push(a.innerText)});JSON.stringify(htmls);"
        return self.Eval(atd)#innerText innerHTML

The above implements commonly used script execution and form filling operations and events. Just write an example.

# -*- coding: UTF-8 -*-
from chrome import *#这里我们引用上面的封装
import time
StartUp('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')#准备一个谷歌浏览器
 time.sleep(3)#等三秒谷歌启动完毕
chrome = ChromeTagLink(GetTabs(0))#初始化页面
chrome.Navigate("http://www.biquge.tv/xuanhuanxiaoshuo/")#跳转页面
for num in range(0,3):
    print chrome.css_get_htmls(".s3")#输出类为s3的列表 
    #注意这里的css_get_htmls 可以自己改为想要获取的任何元素
    chrome.css_click(".ngroup") #拿到数据 点击翻页
    time.sleep(4) #等待四秒翻页继续获取
print "爬完毕"
chrome.Close()#销毁类

Note that the time.sleep(4) seconds here are filled in according to the actual situation of your computer and are not fixed

Enter image description

Due to the limited space, this is for the time being

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325553671&siteId=291194637