How to use expose_function method in Playwright Python

Best Python code snippet using playwright-python

biu.py

Source:biu.py Github

copy

Full Screen

...72 page = context.new_page()73 page.add_style_tag(content=".focusClass {border:10px solid #FE0000;}")74 # page.add_init_script(path="./dom_monitoring.js")75 # page.on("dialog", handle_dialog)76 # page.expose_function("getlink", getlink)77 # page.expose_function("test_css", test_css)78 if pageConfig:79 if "pageLoadTimeout" in pageConfig:80 curPageLoadTimeout = pageConfig["pageLoadTimeout"]81 curPageLoadTimeoutMilliSec = curPageLoadTimeout * 100082 page.set_default_navigation_timeout(curPageLoadTimeoutMilliSec)83 page.set_default_timeout(curPageLoadTimeoutMilliSec)84 return page, context85def closeBrowser(browser):86 browser.close()87def firstOpen(target):88 page = initPage(browser=browser)89 page.goto(target, wait_until="networkidle", timeout=600000)90# def tagFocus(page):91# page.evaluate('''() => {92# function focusInput() {93# console.log("注入了tagFocus js脚本")94# var elements = document.querySelectorAll("input,span,a,link,select,textarea");95# console.log(elements);96# for (var i=0; i < elements.length; i++) {97# elements[i].onfocus = function() { this.className = 'focusClass'; };98# elements[i].onblur = function() { this.className = ''; };99# }100# }101# setTimeout(function() {102# focusInput()103# }, 100);104# }''')105class crawlTar:106 def __init__(self, page, tarurl):107 self.page = page108 self.target = tarurl109 self.js_content_list = [] # 对已经触发过的tag打标,防止重复点击110 self.req_list = [] # 已经请求过的链接,重复的请求不再收集111 self.url_list = [] # 忘了112 self.q = Queue(maxsize=0) # 任务队列113 self.wbk = xlwt.Workbook() # 初始化workbook对象114 self.sheet = self.wbk.add_sheet('My_Worksheet') # 创建表115 self.sheet.write(0, 0, "链接")116 self.sheet.write(0, 1, "请求方式")117 self.sheet.write(0, 2, "headers头")118 self.sheet.write(0, 3, "data数据")119 self.page.on("request", lambda request: self.handle_request(request))120 # self.page.on("console", lambda msg: self.echo_console(msg))121 self.page.on("dialog", lambda dialog: dialog.accept())122 # 标注链接及来源123 n = 0124 def getlink(self, link, source):125 # #print("【修复前】获取到的链接为:\t", link, "\t", source)126 new_url = self.repair_url(link)127 # #print("【修复后】获取到的链接为:\t", new_url, "\t", source)128 tarurl_domain = urlparse(self.target).netloc129 url_domain = urlparse(new_url).netloc130 # #print(tarurl_domain, url_domain)131 # 如果是同域的132 if tarurl_domain == url_domain:133 if self.parse_link_static(new_url):134 pass135 else:136 getlinkdict = {}137 getlinkdict["url"] = new_url138 getlinkdict["source"] = source139 # #print(getlinkdict, "\tvvvvvsssss\t", self.url_list)140 if new_url in self.url_list:141 pass142 else:143 #print("【同域】:\t", new_url, "\t", source)144 self.url_list.append(new_url)145 logger.info("request\t>>>\t{}".format(new_url))146 self.q.put(new_url)147 def echo_console(self, msg):148 if "Error" in msg.text or "Failed" in msg.text:149 pass150 else:151 print("console info:\t", msg.text)152 def handle_request(self, request):153 req_data = {}154 # print(request.url)155 # #print("handle_request:\t", request.url, request.method)156 # #print("当前请求:\t", request.url, "\tvs\t", "网页输入栏:\t", self.page.url)157 if request.is_navigation_request() and not self.page.frames[0].parent_frame:158 # self.page.route(request.url,lambda route: route.fulfill(159 # status=204160 # ))161 # print("handle_navigation:\t", request.url)162 self.getlink(request.url, "handle_navigation")163 else:164 self.getlink(request.url, "handle_request")165 # if is_target(request.url, tarurl):166 # if parse_link_static(request.url) == False:167 # n = n + 1168 # sheet.write(n, 0, request.url)169 # sheet.write(n, 1, request.method)170 # sheet.write(n, 2, json.dumps(request.headers))171 # sheet.write(n, 3, json.dumps(request.post_data))172 # q.put(request.url)173 req_data["url"] = request.url174 req_data["method"] = request.method175 req_data["headers"] = request.headers176 if request.post_data:177 req_data["body_data"] = request.post_data178 else:179 req_data["body_data"] = ""180 # 最后将结果写入excle181 for i in self.req_list:182 if request.url == i["url"] and request.method == i["method"]:183 pass184 185 if self.set_req_list(request):186 # print(request.url,"\n",req_data,"\n",self.req_list)187 self.req_list.append(req_data)188 def set_req_list(self,request):189 for i in self.req_list:190 if request.url == i["url"] and request.method == i["method"]:191 return False192 return True193 def test_css(self):194 #print("add_style_tag")195 self.page.add_style_tag(196 content=".focusClass {border:2px solid #FF0400;outline:none}")197 self.page.add_script_tag(198 content='console.log("111111111111111111111111")')199 def repair_url(self, url):200 tarurl_domain = urlparse(self.target).netloc201 tarurl_scheme = urlparse(self.target).scheme202 url_domain = urlparse(url).netloc203 # 判断是否为完整链接204 new_url = ""205 if "http://" in url or "https://" in url:206 return url207 else:208 new_url = tarurl_scheme + "://" + tarurl_domain + url209 return new_url210 def listening_event(self):211 self.page.evaluate('''() => {212 function interceptClickEvent(e) {213 var href;214 var target = e.target || e.srcElement;215 if (target.tagName === 'A') {216 href = target.href;217 console.log(href);218 e.preventDefault();219 }220 }221 document.body.addEventListener('click', interceptClickEvent);222 const unchange = {"writable": false, "configurable": false};223 //hook History API224 window.history.pushState = function(a, b, url) { window.getlink(url,"history")}225 Object.defineProperty(window.history,"pushState",unchange);226 window.history.replaceState = function(a, b, url) { window.getlink(url,"history")}227 Object.defineProperty(window.history,"replaceState",unchange);228 //hook new window229 window.open = function (url) { window.getlink(url,"open")}230 Object.defineProperty(window,"open",unchange);231 //hook close window232 window.close = function() {console.log("trying to close page.");};233 Object.defineProperty(window,"close",unchange);234 //hook hash change route235 window.addEventListener("hashchange", function () {236 window.getlink(document.location.href,"hashchange")237 console.log("#hashchange#",document.location.href);238 });239 // hook new requests240 let oldWebSocket = window.WebSocket;241 window.WebSocket = function (url) {242 window.getlink(url,"WebSocket")243 console.log(`WebSocket: ${url}`);244 return new oldWebSocket(url);245 };246 let oldEventSource = window.EventSource;247 window.EventSource = function (url) {248 window.getlink(url,"EventSource")249 console.log(`EventSource: ${url}`);250 return new oldEventSource(url);251 };252 let oldFetch = window.fetch;253 window.fetch = function (url) {254 window.getlink(url,"fetch")255 console.log(`fetch: ${url}`);256 return oldFetch(url);257 };258 // hook form reset259 HTMLFormElement.prototype.reset = function () {260 console.log("cancel reset form")261 };262 Object.defineProperty(HTMLFormElement.prototype, "reset", unchange);263 // hook time func264 let oldSetTimeout = window.setTimeout;265 window.setTimeout = function (time) {266 //console.log(`setInterval: ${time}`);267 return oldSetTimeout(1.5);268 };269 let oldSetInterval = window.setInterval;270 window.setInterval = function (time) {271 //console.log(`setInterval: ${time}`);272 return oldSetInterval(1.5);273 };274 }275 ''')276 def listening_dom(self):277 self.page.evaluate('''() => {278 let xy_dict = {};279 //dom monitor280 console.log("开始监听dom")281function findnodeclass(data) {282 if (data.className) {283 let nodeclass = data.className;284 if (nodeclass == "available") {285 return true;286 }287 } else {288 return findnodeclass(data.parentNode);289 }290 return false;291}292let findId = (function (findNode) {293 return function fn(data) {294 if (data.children && data.children.length) {295 for (let f = 0; f < data.children.length; f++) {296 // console.log(data.children[f].tagName);297 if (data.children[f].tagName == "SPAN") {298 if (findnodeclass(data.children[f])) {299 findNode = data.children[f];300 //console.log("28", findNode);301 return findNode;302 }303 }304 if ((findNode = fn(data.children[f]))) break;305 }306 return findNode;307 }308 };309})(null);310let find_ul_span = (function (findNode) {311 return function fn(data) {312 if (data.children && data.children.length) {313 for (let f = 0; f < data.children.length; f++) {314 // console.log(data.children[f].tagName);315 if (data.children[f].tagName == "SPAN") {316 findNode = data.children[f];317 // console.log("28", findNode);318 return findNode;319 }320 if ((findNode = fn(data.children[f]))) break;321 }322 return findNode;323 }324 };325})(null);326let findtable = (function (findNode) {327 let table_list = [];328 return function fn(data) {329 if (data.children && data.children.length) {330 for (let f = 0; f < data.children.length; f++) {331 //console.log(data.children[f].tagName);332 if (data.children[f].tagName == "TABLE") {333 findNode = data.children[f];334 if (table_list.indexOf(findNode) != -1) {335 continue;336 } else {337 // console.log("获取到table的节点>>>", findNode);338 table_list.push(findNode);339 break;340 }341 } else fn(data.children[f]);342 }343 // console.log("50", table_list);344 return table_list;345 }346 };347})(null);348let findul = (function (findNode) {349 let ul_list = [];350 return function fn(data) {351 if (data.children && data.children.length) {352 for (let f = 0; f < data.children.length; f++) {353 // console.log(data.children[f].tagName);354 if (data.children[f].tagName == "UL") {355 findNode = data.children[f];356 // console.log("72", findNode);357 if (ul_list.indexOf(findNode) != -1) {358 continue;359 } else {360 // console.log("获取到Ul的节点>>>", findNode);361 ul_list.push(findNode);362 break;363 }364 } else fn(data.children[f]);365 }366 // console.log("50", table_list);367 return ul_list;368 }369 };370})(null);371//dom monitor372var observer = new MutationObserver(function (mutations) {373 let dom_list = [];374 // console.log('eventLoop nodesMutated:', mutations.length);375 mutations.forEach(function (mutation) {376 //console.log("dom改变的类型>>>",mutation.type)377 if (mutation.type === "childList") {378 for (let i = 0; i < mutation.addedNodes.length; i++) {379 let addedNode = mutation.addedNodes[i];380 if (dom_list.indexOf(addedNode) != -1) {381 continue;382 } else {383 dom_list.push(addedNode);384 // console.log("新增dom内容为: ", addedNode);385 //自动选择下拉框386 let ulNode = findul(addedNode);387 // console.log("102", ulNode);388 if (ulNode) {389 for (let i = 0; i < ulNode.length; ++i) {390 let ulul = find_ul_span(ulNode[i]);391 if (ulul) {392 // console.log("105", ulul);393 ulul.click();394 }395 }396 }397 // 自动点击时间选择器398 let tableNode = findtable(addedNode);399 if (tableNode) {400 for (let i = 0; i < tableNode.length; ++i) {401 let ss = findId(tableNode[i]);402 //console.log("126",ss);403 if (ss) {404 console.log("71", ss);405 //ss.click();406 setTimeout(function () {407 ss.click();408 }, 1000);409 }410 }411 }412 }413 }414 } else if (mutation.type === "attributes") {415 // 某节点的一个属性值被更改416 let element = mutation.target;417 var element_val = element.getAttribute(mutation.attributeName);418 // console.log(mutation.attributeName, "->", element_val);419 var change_dom = mutation.target.parentNode;420 var change_dom_tagname = mutation.target.parentNode.tagName;421 // console.log(change_dom_tagname,change_dom)422 // if (change_dom && change_dom_tagname == "DIV") {423 // // console.log("对应更改的DOM>>>>", change_dom);424 // let ulNode = findul(change_dom);425 // if (ulNode) {426 // for (let i = 0; i < ulNode.length; ++i) {427 // let ulul = find_ul_span(ulNode[i]);428 // if (ulul) {429 // ulul.click();430 // }431 // }432 // }433 // }434 }435 });436});437observer.observe(window.document.documentElement, {438 childList: true,439 attributes: true,440 characterData: false,441 subtree: true,442 characterDataOldValue: false,443});444 //node list445 //446 var treeWalker = document.createTreeWalker(447 document.body,448 NodeFilter.SHOW_ELEMENT,449 {450 acceptNode: function (node) {451 return NodeFilter.FILTER_ACCEPT;452 },453 }454 );455 while (treeWalker.nextNode()) {456 var element = treeWalker.currentNode;457 let xy_list = []458 for (k = 0; k < element.attributes.length; k++) {459 //console.log("338",element);460 //console.log("339",element.tagName);461 attr = element.attributes[k];462 //console.log("341",attr);463 //console.log("342",attr.nodeName);464 if (attr.nodeName.startsWith("on")) {465 let eventName = attr.nodeName.replace("on", "");466 let dict_xy = {};467 var X1 =468 element.getBoundingClientRect().left +469 document.documentElement.scrollLeft;470 var Y1 =471 element.getBoundingClientRect().top +472 document.documentElement.scrollLeft;473 dict_xy["x"] = X1;474 dict_xy["y"] = Y1;475 dict_xy["event"] = eventName;476 //console.log("559。。。。。。。。。。。。",dict_xy);477 xy_list.push(dict_xy);478 if (element.tagName == "TR") {479 let arr = element.querySelectorAll("td");480 for (let i = 0; i < arr.length; ++i) {481 let dict_xy = {};482 //console.log(arr[i]);483 let X =484 arr[i].getBoundingClientRect().left +485 arr[i].getBoundingClientRect().width / 2 +486 document.documentElement.scrollLeft;487 let Y =488 arr[i].getBoundingClientRect().top +489 arr[i].getBoundingClientRect().height / 2 +490 document.documentElement.scrollLeft;491 console.log("x: " + X, "y: " + Y);492 dict_xy["x"] = X;493 dict_xy["y"] = Y;494 dict_xy["event"] = eventName;495 xy_list.push(dict_xy);496 }497 }498 xy_dict["data"] = xy_list499 }500 }501 }502 //console.log("xy_dict",xy_dict);503 }504 ''')505 def find_a(self):506 # 获取A标签的完整链接507 #print("开始探测A标签...")508 self.page.evaluate('''() => {509 console.log("开始获取A标签.....")510 // 不点击a标签的情况下,获取完整href链接,调用方式为getAbsoluteUrl(链接)511 var getAbsoluteUrl = (function() {512 var a;513 return function(url) {514 if(!a) a = document.createElement('a');515 a.href = url;516 return a.href;517 };})();518 function getSrcAndHrefLinks(nodes) {519 for(let node of nodes){520 //console.log(node);521 let src = node.getAttribute("src");522 let href = node.getAttribute("href");523 let action = node.getAttribute("action");524 if (src){525 //console.log(src);526 window.getlink(getAbsoluteUrl(src),"src");527 console.log("262",getAbsoluteUrl(src));528 }529 if (href){530 console.log("412###########",href);531 window.getlink(getAbsoluteUrl(href),"href");532 if (href.startsWith("javascript")){533 console.log("417",href,getAbsoluteUrl(href))534 eval(href.substring(11))535 }536 console.log("414",getAbsoluteUrl(href));537 }538 if(action){539 window.getlink(getAbsoluteUrl(action),"action");540 console.log(getAbsoluteUrl(action));541 }542 }543 }544 getSrcAndHrefLinks(document.querySelectorAll("[src],[href],[action]"))545 // 监听click事件与mousedown事件546 function interceptClickEvent(e) {547 var href;548 var target = e.target || e.srcElement;549 if (target.tagName === 'A') {550 href = target.href;551 window.getlink(href,"interceptClickEvent");552 console.log(href);553 // 这里延迟是因为与window.open 冲突了554 setTimeout(function(){e.preventDefault()},1000);555 //e.preventDefault();556 };557 };558 document.body.addEventListener('click', interceptClickEvent);559 document.body.addEventListener('mousedown', interceptClickEvent);560 }561 ''')562 def input_list(self):563 #print("开启探测input....")564 inputs = self.page.query_selector_all("input")565 n = 0566 for i in inputs:567 # 获取父节点,判断input的父节点是否为form568 try:569 tagName = i.get_property("parentNode").evaluate(570 "node => node.tagName")571 if tagName == "FORM":572 self.get_form_script()573 continue574 else:575 n = n + 1576 #print(i.evaluate("node => node.outerHTML"))577 # 获取placeholder的值578 placeholder_v = i.get_attribute("placeholder")579 i_type = i.get_attribute("type")580 #print("input_type:\t", i_type)581 if i_type == "radio":582 if self.marktag(i, "radio"):583 # name = i.get_attribute("name")584 try:585 value = i.get_attribute("value")586 self.page.select_option(587 "#"+name, value, timeout=0)588 except:589 pass590 elif i_type == "text":591 #print("发现text input")592 input_node = i.evaluate("node => node.outerHTML")593 # print("input_outerHTML", i.evaluate(594 # "node => node.outerHTML"))595 if self.marktag(input_node, "input_text"):596 name_v = i.get_attribute("name")597 readonly_v = i.get_attribute("readonly")598 value_v = i.get_attribute("value")599 #print("判断是否可编辑...")600 # self.page.pause()601 if readonly_v and placeholder_v:602 self.page.click(603 "[placeholder=\"{}\"]".format(placeholder_v))604 elif placeholder_v:605 #print("获取input_placeholder")606 self.page.click(607 "[placeholder=\"{}\"]".format(placeholder_v))608 self.page.fill("[placeholder=\"{}\"]".format(609 placeholder_v), "test")610 elif name_v:611 #print("获取input_name")612 self.page.click("[name=\"{}\"]".format(name_v))613 self.page.fill("[name=\"{}\"]".format(614 name_v), "test")615 elif value_v:616 try:617 self.page.fill("#"+name, "test")618 except Exception as e:619 traceback.print_exc()620 #print("报错了:", e)621 elif i_type == "hidden":622 inputs = self.page.query_selector_all("input")623 #print("发现隐藏input")624 i.evaluate("node => node.value='test'")625 elif i_type == "password":626 password_node = i.evaluate("node => node.outerHTML")627 if self.marktag(password_node, "input_text"):628 # 获取placeholder的值629 placeholder_v = i.get_attribute("placeholder")630 if placeholder_v != None:631 #print("获取input_placeholder")632 self.page.click(633 "[placeholder=\"{}\"]".format(placeholder_v))634 self.page.fill("[placeholder=\"{}\"]".format(635 placeholder_v), "test")636 name_v = i.get_attribute("name")637 #print("622", name_v)638 if name_v != None:639 #print("获取input_name")640 self.page.click("[name=\"{}\"]".format(name_v))641 self.page.fill("[name=\"{}\"]".format(642 name_v), "test")643 value_v = i.get_attribute("value")644 if value_v != None:645 try:646 self.page.fill("#"+name, "test")647 except Exception as e:648 traceback.print_exc()649 #print("报错了:", e)650 elif i_type == "submit":651 #print("发现submit input")652 input_node = i.evaluate("node => node.outerHTML")653 #print("提交input表单....")654 if self.marktag(input_node, "input_submit"):655 i.click()656 elif placeholder_v:657 self.page.click(658 "[placeholder=\"{}\"]".format(placeholder_v))659 except:660 pass661 def all_a_click(self):662 #print("点击所有包含javascript的标签...")663 # 获取链接的标签664 link_attrs = "[src],[href],[action],[data-url],[longDesc],[lowsrc]"665 nodes = self.page.query_selector_all(link_attrs)666 # print (nodes)667 for node in nodes:668 # #print(node)669 for attr in link_attrs.split(","):670 link = node.get_attribute(671 attr.replace('[', '').replace(']', ''))672 if not link:673 # #print("1",link)674 pass675 elif self.parse_link_static(link):676 pass677 else:678 # #print("2",link)679 try:680 if "javascript" in link:681 #print(link)682 node.click()683 except Exception as e:684 traceback.print_exc()685 # 获取form表单686 def get_form_script(self):687 #print("开启探测form....")688 # 返回值需要是一个可序列化的值,不然返回值为none689 form_data = self.page.evaluate('''() => {690 let formdicts = {};691 let formItem_list =[]692 for (let i = 0; i < document.forms.length; i++) {693 form = document.forms[i];694 console.log(form.action)695 let formdict = {};696 let form_para= []697 for (var j = 0; j < form.length; j++) {698 let formItem = {};699 input = form[j];700 formItem["nodename"] = input.nodeName;701 formItem["placeholder"] = input.getAttribute("placeholder");702 formItem["type"] = input.type;703 formItem["name"] = input.name;704 formItem["value"] = input.value;705 console.log(formItem);706 form_para.push(formItem);707 } 708 formdict[form.action] = form_para709 console.log("33",formdict)710 formItem_list.push(formdict)711 }712 formdicts["data"] = formItem_list713 return JSON.stringify(formdicts)714 }715 ''')716 # #print("575", form_data, type(form_data))717 form_json = json.loads(form_data)718 inputs = self.page.query_selector_all("input")719 textareas = self.page.query_selector_all("textarea")720 if len(form_json["data"]) > 0:721 for form in form_json["data"]:722 # #print("573", form)723 if self.marktag(form, "form"):724 form_tag = form.values()725 for tags in form_tag:726 # #print("577", tags)727 # tags728 # [{'nodename': 'INPUT', 'placeholder': None, 'type': 'hidden', 'name': 'name', 'value': 'anonymous user'},729 # {'nodename': 'TEXTAREA', 'placeholder': None,'type': 'textarea', 'name': 'text', 'value': ''},730 # {'nodename': 'INPUT', 'placeholder': None, 'type': 'submit', 'name': 'submit', 'value': 'add message'}]731 for tag in tags:732 input_name = tag["name"]733 input_type = tag["type"]734 input_placeholder = tag["placeholder"]735 if tag["nodename"] == "INPUT":736 #print(input_type)737 input_node_i = self.input_node(738 inputs, input_type, input_name, input_placeholder)739 if input_type == "text":740 input_node_i.click()741 input_node_i.fill("test")742 elif input_type == "hidden":743 input_node_i.evaluate(744 "node => node.value='test'")745 elif input_type == "submit":746 input_node_i.click()747 elif input_type == "button":748 input_node_i.click()749 elif tag["nodename"] == "TEXTAREA":750 textarea_node_i = self.textarea_node(751 textareas, input_name)752 if input_type == "textarea":753 textarea_node_i.click()754 textarea_node_i.fill("test")755 def input_node(self, inputs, n_type, n_name, n_placeholder):756 for i in inputs:757 i_type = i.get_attribute("type")758 i_name = i.get_attribute("name")759 i_placeholder = i.get_attribute("placeholder")760 #print(n_type, n_name, n_placeholder, i_placeholder)761 if i_type == n_type and i_name == n_name:762 return i763 elif i_placeholder:764 if i_type == n_type and i_placeholder == n_placeholder:765 return i766 def textarea_node(self, textareas, n_name):767 for i in textareas:768 i_name = i.get_attribute("name")769 if i_name == n_name:770 return i771 def find_span(self):772 #print("开启探测span....")773 # 获取span标签774 spans = self.page.query_selector_all("span")775 for i in spans:776 try:777 i_text = i.inner_text()778 # i.get_attribute("class")779 print(i)780 print("span的文本:\t",i_text)781 # self.page.click('span:has-text(\"{}\")'.format(i_text))782 try:783 i.click()784 except:785 pass786 # 监听dom变化787 #print("开始监听dom变化....")788 xy_list = self.listening_dom()789 # playwright事件:click\dblclick\down\move\up790 # 页面的事件:click\dblclick\mousedown\mouseout\mouseup791 num = 0792 #print("572。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。")793 #print("循环点击变化的dom....", xy_list)794 if xy_list:795 for i in xy_list:796 #print("583", i)797 num = num+1798 # #print(num,"获取坐标及动作:",i)799 self.page.mouse.move(i["x"], i["y"])800 if i["event"] == "click":801 self.page.mouse.click(i["x"], i["y"])802 if i["event"] == "dblclick":803 self.page.mouse.dblclick(i["x"], i["y"])804 if i["event"] == "mousedown":805 self.page.mouse.down()806 if i["event"] == "mouseup":807 self.page.mouse.up()808 # r.sadd("target_url", link)809 # 获取input的标签 (6.py)810 # 获取ul811 listItems = self.page.locator.locator('ul > li')812 #print(listItems)813 for i in len(listItems):814 listItems.nth(i).click()815 # 获取select的标签816 selects = self.page.query_selector_all("select")817 # 获取option内容818 options = self.page.query_selector_all("option")819 for select in selects:820 name = select.get_attribute("name")821 # #print(name)822 # 获取单选823 for option in options:824 value = option.get_attribute("value")825 self.page.select_option("#"+name, value, timeout=0)826 self.input_list()827 except Exception as e:828 traceback.print_exc()829 #print("报错了:", e)830 def close_dialog(self):831 #print("开启探测弹窗....")832 self.page.evaluate('''() => {833 function focusInput() {834 //console.log("注入了button js脚本")835 var elements = document.getElementsByTagName("button");836 //console.log(elements);837 for (var i=0; i < elements.length; i++) {838 //console.log(elements[i].outerHTML);839 if (elements[i].outerHTML.indexOf("dialog") != -1) {840 //console.log(elements[i]);841 elements[i].click();842 }843 }844 }845 setInterval(function() {846 focusInput()847}, 100);848 }''')849 def test_script(self):850 #print("开启注入js脚本....")851 crawlTar.test_css(self)852 self.page.evaluate('''() => {853 console.log("注入了tagFocus js脚本")854 var elements = document.querySelectorAll("input,span,a,link,select,textarea");855 //var elements = document.getElementsByTagName("input");856 //console.log(elements);857 for (var i=0; i < elements.length; i++) {858 elements[i].setAttribute('tabindex',"1")859 //console.log( elements[i])860 //console.log(elements[i],elements[i].onfocus)861 elements[i].onfocus = function() { this.className = 'focusClass'; };862 //elements[i].onfocus = function() { alert("ttttt") };863 //elements[i].onfocus = function() { console.log("bbbbbbbbbbbbb"); };864 //console.log(elements[i],elements[i].onfocus)865 elements[i].onblur = function() { this.className = ''; };866 }867 }''')868 def marktag(self, js_content, source):869 js_dict = {}870 js_dict["js_content"] = js_content871 js_dict["source"] = source872 if js_dict in self.js_content_list:873 return False874 else:875 self.js_content_list.append(js_dict)876 return True877 def parse_link_static(self, link):878 if link.startswith('http'):879 parsedLink = parse.urlparse(link)880 hostname = parsedLink.netloc # 主机名不带端口号881 path = parsedLink.path882 link = path.split('/')[-1].split('.')[-1]883 if link == '':884 return False885 else:886 blacklists = ['css', 'svg', "png", "gif", "jpg", "mp4", "mp3", "mng", "pct", "bmp", "jpeg", "pst", "psp", "ttf",887 "tif", "tiff", "ai", "drw", "wma", "ogg", "wav", "ra", "aac", "mid", "au", "aiff",888 "dxf", "eps", "ps", "svg", "3gp", "asf", "asx", "avi", "mov", "mpg", "qt", "rm",889 "wmv", "m4a", "bin", "xls", "xlsx", "ppt", "pptx", "doc", "docx", "odt", "ods", "odg",890 "odp", "exe", "zip", "rar", "tar", "gz", "iso", "rss", "pdf", "txt", "dll", "ico",891 "gz2", "apk", "crt", "woff", "map", "woff2", "webp", "less", "dmg", "bz2", "otf", "swf",892 "flv", "mpeg", "dat", "xsl", "csv", "cab", "exif", "wps", "m4v", "rmvb"]893 result = any(link in black for black in blacklists)894 if result == True:895 return True896 else:897 return False898 else:899 return True900 def goto(self, target):901 self.page.goto(target, wait_until="networkidle", timeout=600000)902 # 每次打开个新网页,就注入一次js,主要是高亮标签903 # self.page.pause()904 # self.test_script()905 try:906 # self.page.expose_function("test_script", self.test_script)907 # 注册新的函数,以采集请求908 self.page.expose_function("getlink", self.getlink)909 self.page.expose_function("find_a", self.find_a)910 except:911 pass912 # 监听dom变化913 self.listening_dom()914 # 监听事件变化915 self.listening_event()916 # 寻找A标签917 self.find_a()918 # 寻找input标签919 self.input_list()920 # 寻找form表单921 self.get_form_script()922 # 寻找span标签923 # self.find_span()...

Full Screen

Full Screen

test_browsercontext.py

Source:test_browsercontext.py Github

copy

Full Screen

...287 assert binding_source[0]["page"] == page288 assert binding_source[0]["frame"] == page.main_frame289 assert result == 11290async def test_expose_function_should_work(context):291 await context.expose_function("add", lambda a, b: a + b)292 page = await context.new_page()293 await page.expose_function("mul", lambda a, b: a * b)294 await context.expose_function("sub", lambda a, b: a - b)295 result = await page.evaluate(296 """async function() {297 return { mul: await mul(9, 4), add: await add(9, 4), sub: await sub(9, 4) }298 }"""299 )300 assert result == {"mul": 36, "add": 13, "sub": 5}301async def test_expose_function_should_throw_for_duplicate_registrations(302 context, server303):304 await context.expose_function("foo", lambda: None)305 await context.expose_function("bar", lambda: None)306 with pytest.raises(Error) as exc_info:307 await context.expose_function("foo", lambda: None)308 assert exc_info.value.message == 'Function "foo" has been already registered'309 page = await context.new_page()310 with pytest.raises(Error) as exc_info:311 await page.expose_function("foo", lambda: None)312 assert (313 exc_info.value.message314 == 'Function "foo" has been already registered in the browser context'315 )316 await page.expose_function("baz", lambda: None)317 with pytest.raises(Error) as exc_info:318 await context.expose_function("baz", lambda: None)319 assert (320 exc_info.value.message321 == 'Function "baz" has been already registered in one of the pages'322 )323async def test_expose_function_should_be_callable_from_inside_add_init_script(324 context, server325):326 args = []327 await context.expose_function("woof", lambda arg: args.append(arg))328 await context.add_init_script("woof('context')")329 page = await context.new_page()330 await page.add_init_script("woof('page')")331 args = []332 await page.reload()333 assert args == ["context", "page"]334async def test_expose_bindinghandle_should_work(context):335 targets = []336 def logme(t):337 targets.append(t)338 return 17339 page = await context.new_page()340 await page.expose_binding("logme", lambda source, t: logme(t), handle=True)341 result = await page.evaluate("logme({ foo: 42 })")...

Full Screen

Full Screen

test_focus.py

Source:test_focus.py Github

copy

Full Screen

...19 assert await page.evaluate("() => document.activeElement.id") == "d1"20async def test_should_emit_focus_event(page):21 await page.set_content("<div id=d1 tabIndex=0></div>")22 focused = []23 await page.expose_function("focusEvent", lambda: focused.append(True))24 await page.evaluate("() => d1.addEventListener('focus', focusEvent)")25 await page.focus("#d1")26 assert focused == [True]27async def test_should_emit_blur_event(page):28 await page.set_content(29 "<div id=d1 tabIndex=0>DIV1</div><div id=d2 tabIndex=0>DIV2</div>"30 )31 await page.focus("#d1")32 focused = []33 blurred = []34 await page.expose_function("focusEvent", lambda: focused.append(True))35 await page.expose_function("blurEvent", lambda: blurred.append(True))36 await page.evaluate("() => d1.addEventListener('blur', blurEvent)")37 await page.evaluate("() => d2.addEventListener('focus', focusEvent)")38 await page.focus("#d2")39 assert focused == [True]40 assert blurred == [True]41async def test_should_traverse_focus(page):42 await page.set_content('<input id="i1"><input id="i2">')43 focused = []44 await page.expose_function("focusEvent", lambda: focused.append(True))45 await page.evaluate("() => i2.addEventListener('focus', focusEvent)")46 await page.focus("#i1")47 await page.keyboard.type("First")48 await page.keyboard.press("Tab")49 await page.keyboard.type("Last")50 assert focused == [True]51 assert await page.eval_on_selector("#i1", "e => e.value") == "First"52 assert await page.eval_on_selector("#i2", "e => e.value") == "Last"53async def test_should_traverse_focus_in_all_directions(page):54 await page.set_content('<input value="1"><input value="2"><input value="3">')55 await page.keyboard.press("Tab")56 assert await page.evaluate("() => document.activeElement.value") == "1"57 await page.keyboard.press("Tab")58 assert await page.evaluate("() => document.activeElement.value") == "2"...

Full Screen

Full Screen

__main__.py

Source:__main__.py Github

copy

Full Screen

...74 expose_interop_values(he)75 # expose all shared type of data structures76 expose_shared_data_structures(he)77 # expose magic functions78 he.expose_function(None, __hbi_init__)79 he.expose_function(None, __hbi_cleanup__)80 return he81async def serve_chatting():82 server = await serve_tcp(83 # listening IP address(es)84 service_addr,85 # the hosting env factory function86 he_factory,87 )88 logger.info(89 "HBI Chatting Server listening:\n * "90 + "\n * ".join(91 ":".join(str(v) for v in s.getsockname()) for s in server.sockets92 )93 )...

Full Screen

Full Screen

Playwright tutorial

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful