How to use frame_element method in Playwright Python

Best Python code snippet using playwright-python

generate_scores.py

Source:generate_scores.py Github

copy

Full Screen

1import os2import argparse3import itertools4import numpy as np5import pandas as pd6import config7import entailment8import utils9__author__ = 'chetannaik'10def get_question_group_key(question_group, question):11 for q in question_group.groups.keys():12 if question.strip() in q.strip():13 return q14 return None15def get_question_frames(question, question_frames, experiment):16 """Question frame extractor.17 Args:18 question: A string representing the question without options.19 question_frames: Contents of question_frames.tsv file.20 Returns: A list of python dictionaries containing the the frames for the21 question extracted from question_frames.22 """23 q_group = question_frames.groupby('QUESTION')24 if len(filter(bool, set(question.strip().split('.')))) > 1:25 q_sentences = set(question.strip().split('.'))26 q_sentences = filter(bool, map(lambda x: x.strip(), q_sentences))27 else:28 q_sentences = []29 q_sentences.append(question.strip())30 dfs = []31 for q in q_sentences:32 key = get_question_group_key(q_group, q)33 if key:34 df = q_group.get_group(key)35 dfs.append(df)36 ret_df = pd.concat(dfs)37 ret_df = ret_df[config.ROLES[experiment]]38 ret_df.drop_duplicates(inplace=True)39 return ret_df.to_dict(orient='records')40def get_answer_group_key(process_group, process):41 for group in process_group.groups.keys():42 names = group.split(" | ")43 if utils.get_lemma(process) in names:44 return group45 return None46def get_answer_frames(answer, process_db, experiment):47 """Answer frame extractor.48 Args:49 answer: A string representing the answer choice.50 process_db: Contents of process_frames.tsv file.51 Returns: a list of python dictionaries, containing frames for the answer52 with frame elements extracted from process_db.53 """54 p_group = process_db.groupby('PROCESS')55 key = get_answer_group_key(p_group, answer)56 if key:57 afs = p_group.get_group(key)58 afs = afs[config.ROLES[experiment]]59 afs.drop_duplicates(inplace=True)60 return afs.to_dict(orient='records')61 else:62 return []63def get_alignment_data(question_frames, answer_choices, process_db, experiment):64 """Gets alignment scores by calling aligner.65 Args:66 question_frames: A list of python dictionaries containing question67 frames.68 answer_choices: A python list containing answer choices.69 process_db: Contents of process_frames.tsv file.70 Returns: A dictionary for each answer choice containing a list of71 dictionaries frame roles, role elements and their entailment scores.72 """73 answer_data = dict()74 for answer in answer_choices:75 answer_frames = get_answer_frames(answer, process_db, experiment)76 answer_scores = aligner(question_frames, answer_frames, experiment)77 answer_data[answer] = answer_scores78 return answer_data79def get_frame_directional_score(question_frame, answer_frame, experiment):80 frame_scores = dict()81 temp_scores = dict()82 temp_scores["FORWARD"] = dict()83 for frame_element in config.ROLES[experiment]:84 q_element = question_frame[frame_element]85 a_element = answer_frame[frame_element]86 if pd.isnull(q_element) or pd.isnull(a_element):87 score = np.NaN88 else:89 ret = entailment.get_ai2_textual_entailment(a_element, q_element)90 a_scores = map(lambda x: x['score'], ret['alignments'])91 if len(a_scores):92 mean_a_score = np.mean(a_scores)93 else:94 mean_a_score = 095 confidence = ret['confidence'] if ret['confidence'] else 096 score = mean_a_score * confidence97 temp_scores["FORWARD"][frame_element] = (q_element, a_element, score)98 temp_scores["BACKWARD"] = dict()99 for frame_element in config.ROLES[experiment]:100 q_element = question_frame[frame_element]101 a_element = answer_frame[frame_element]102 if pd.isnull(q_element) or pd.isnull(a_element):103 score = np.NaN104 else:105 ret = entailment.get_ai2_textual_entailment(q_element, a_element)106 a_scores = map(lambda x: x['score'], ret['alignments'])107 if len(a_scores):108 mean_a_score = np.mean(a_scores)109 else:110 mean_a_score = 0111 confidence = ret['confidence'] if ret['confidence'] else 0112 score = mean_a_score * confidence113 temp_scores["BACKWARD"][frame_element] = (q_element, a_element, score)114 best_score = 0115 best_direction = None116 for k, v in temp_scores.iteritems():117 direction = k118 score = sum(map(lambda x: x[2], v.values()))119 if score >= best_score:120 best_score = score121 best_direction = direction122 for frame_element in config.ROLES[experiment]:123 q_element = question_frame[frame_element]124 a_element = answer_frame[frame_element]125 if pd.notnull(q_element) and pd.notnull(a_element):126 q_list = map(lambda x: x.strip(), q_element.split("|"))127 a_list = map(lambda x: x.strip(), a_element.split("|"))128 if utils.has_filter_keyword(q_list):129 frame_scores[frame_element] = ("", a_element, np.NaN)130 elif utils.has_filter_keyword(a_list):131 frame_scores[frame_element] = (q_element, "", np.NaN)132 else:133 frame_scores[frame_element] = \134 temp_scores[best_direction][frame_element]135 elif pd.notnull(q_element):136 frame_scores[frame_element] = (q_element, "", score)137 elif pd.notnull(a_element):138 frame_scores[frame_element] = ("", a_element, score)139 else:140 frame_scores[frame_element] = ("", "", np.NaN)141 # frame_scores[utils.DEFINITION] = answer_frame[utils.DEFINITION]142 return frame_scores143def get_role_directional_score(question_frame, answer_frame, experiment):144 frame_scores = dict()145 for frame_element in config.ROLES[experiment]:146 q_element = question_frame[frame_element]147 a_element = answer_frame[frame_element]148 if config.ENTAILMENT_TYPE == 'BEST_TEXT_SPAN':149 if pd.notnull(q_element):150 q_spans = q_element.split(" | ")151 else:152 q_spans = [pd.NaT]153 if pd.notnull(a_element):154 a_spans = a_element.split(" | ")155 else:156 a_spans = [pd.NaT]157 all_combinations = itertools.product(q_spans, a_spans)158 best_combination = (None, None, 0)159 for q_span, a_span in all_combinations:160 if pd.isnull(q_span) or pd.isnull(a_span):161 score = np.NaN162 best_combination = (q_span, a_span, score)163 else:164 ret = entailment.get_ai2_textual_entailment(165 utils.remove_filter_words(a_span),166 utils.remove_filter_words(q_span))167 a_scores = map(lambda x: x['score'], ret['alignments'])168 if len(a_scores):169 mean_a_score = np.mean(a_scores)170 else:171 mean_a_score = 0172 confidence = ret['confidence'] if ret['confidence'] else 0173 score1 = mean_a_score * confidence174 ret = entailment.get_ai2_textual_entailment(175 utils.remove_filter_words(q_span),176 utils.remove_filter_words(a_span))177 a_scores = map(lambda x: x['score'], ret['alignments'])178 if len(a_scores):179 mean_a_score = np.mean(a_scores)180 else:181 mean_a_score = 0182 confidence = ret['confidence'] if ret['confidence'] else 0183 score2 = mean_a_score * confidence184 score = max(score1, score2)185 if score > best_combination[2]:186 best_combination = (q_span, a_span, score)187 if pd.notnull(best_combination[0]) and pd.notnull(best_combination[1]):188 frame_scores[frame_element] = best_combination189 elif pd.notnull(best_combination[0]):190 frame_scores[frame_element] = (best_combination[0], "", best_combination[2])191 elif pd.notnull(best_combination[1]):192 frame_scores[frame_element] = ("", best_combination[1], best_combination[2])193 else:194 frame_scores[frame_element] = ("", "", np.NaN)195 else:196 if pd.isnull(q_element) or pd.isnull(a_element):197 score = np.NaN198 else:199 ret = entailment.get_ai2_textual_entailment(200 utils.remove_filter_words(a_element),201 utils.remove_filter_words(q_element))202 a_scores = map(lambda x: x['score'], ret['alignments'])203 if len(a_scores):204 mean_a_score = np.mean(a_scores)205 else:206 mean_a_score = 0207 confidence = ret['confidence'] if ret['confidence'] else 0208 score1 = mean_a_score * confidence209 ret = entailment.get_ai2_textual_entailment(210 utils.remove_filter_words(q_element),211 utils.remove_filter_words(a_element))212 a_scores = map(lambda x: x['score'], ret['alignments'])213 if len(a_scores):214 mean_a_score = np.mean(a_scores)215 else:216 mean_a_score = 0217 confidence = ret['confidence'] if ret['confidence'] else 0218 score2 = mean_a_score * confidence219 score = max(score1, score2)220 if pd.notnull(q_element) and pd.notnull(a_element):221 if q_element in ["process", "processes"]:222 frame_scores[frame_element] = ("", a_element, np.NaN)223 else:224 frame_scores[frame_element] = (q_element, a_element, score)225 elif pd.notnull(q_element):226 frame_scores[frame_element] = (q_element, "", score)227 elif pd.notnull(a_element):228 frame_scores[frame_element] = ("", a_element, score)229 else:230 frame_scores[frame_element] = ("", "", np.NaN)231 # frame_scores[utils.DEFINITION] = answer_frame[utils.DEFINITION]232 return frame_scores233def aligner(question_frames, answer_frames, experiment):234 """Aligns a question frame with a answer frame and calls entailment service235 to get a match score.236 Args:237 question_frames: A list of python dictionaries containing question238 frames.239 answer_frames: A list of python dictionaries containg answer frame240 elements.241 Returns: A number representing the match score of question frame with all242 the answer frames.243 """244 answer_scores = []245 for question_frame in question_frames:246 for answer_frame in answer_frames:247 if config.SCORE_DIRECTION_ABSTRACTION == "FRAME":248 frame_scores = get_frame_directional_score(question_frame,249 answer_frame,250 experiment)251 else:252 frame_scores = get_role_directional_score(question_frame,253 answer_frame,254 experiment)255 answer_scores.append(frame_scores)256 return answer_scores257def process_shard(shard_experiment):258 shard_num, experiment = shard_experiment259 process_db = pd.read_csv(260 "data/" + experiment + "/frames.cv." + str(shard_num) + ".tsv",261 sep='\t')262 process_db.PROCESS = process_db.PROCESS.str.lower()263 questions = pd.read_csv(264 "data/" + experiment + "/question.list.cv." + str(shard_num) + ".tsv",265 sep='\t')266 question_frames = pd.read_csv(267 "data/" + experiment + "/question.framepredict.cv." + str(268 shard_num) + ".tsv", sep='\t')269 ret_list = []270 for qid, row in questions.iterrows():271 if qid > 0 and (qid + 1) % 60 == 0:272 print "."273 else:274 print ".",275 question = row.QUESTION276 q_frames = get_question_frames(question, question_frames, experiment)277 answer_choices = filter(pd.notnull, row[config.OPTIONS].tolist())278 answer_data = get_alignment_data(q_frames, answer_choices, process_db,279 experiment)280 correct_answer = row[row.ANSWER]281 for answer, data in answer_data.iteritems():282 for roles in data:283 out_row = [question, answer] # add roles[DEFINITION] here284 contents = map(lambda x: list(roles[x]),285 config.ROLES[experiment])286 contents = list(itertools.chain.from_iterable(contents))287 out_row.extend(contents)288 out_row.append(correct_answer)289 ret_list.append(out_row)290 return ret_list291def main(num_shards, experiment):292 utils.get_filter_words()293 if not os.path.exists("output/" + experiment):294 os.makedirs("output/" + experiment)295 result = []296 for shard_num in range(num_shards):297 result.extend(process_shard((shard_num, experiment)))298 result_df = pd.DataFrame(result)299 col_list = ['QUESTION', 'ANSWER_CHOICE'] # add 'ANSWER_SENTENCE' here300 contents = map(lambda r: ['Q_' + r, 'A_' + r, r + '_SCORE'],301 config.ROLES[experiment])302 contents = list(itertools.chain.from_iterable(contents))303 col_list.extend(contents)304 col_list.append('CORRECT_ANSWER')305 result_df.columns = col_list306 result_df.to_csv("output/" + experiment + "/features.tsv", sep="\t")307if __name__ == '__main__':308 parser = argparse.ArgumentParser(description='Frame alignment for QA.')309 parser.add_argument('--num_shards', type=int,310 help='number of shards/folds')311 parser.add_argument('--experiment', help='experiment to run')312 args = parser.parse_args()...

Full Screen

Full Screen

framenet.py

Source:framenet.py Github

copy

Full Screen

1#! /usr/bin/env python2# Author: Kapil Thadani (kapil@cs.columbia.edu)3from __future__ import division, with_statement4from nltk.corpus import framenet5###############################################################################6# Names of all frames in Framenet (1019 total)7frames = sorted(frame.name for frame in framenet.frames())8# Names of all possible FEs (1170 total)9fes = sorted(set(fe for frame in framenet.frames() for fe in frame.FE.keys()))10# Names of all possible frames and FEs (9633 total)11frame_fes = sorted([(frame.name, fe)12 for frame in framenet.frames()13 for fe in frame.FE.iterkeys()],14 key=lambda x: x[0] + x[1])15###############################################################################16# Core types of FEs17coretypes = ['Core', 'Peripheral', 'Extra-Thematic']18# Names of all possible FEs and coretypes (1491 total)19fe_coretypes = sorted(set((fe, frame_element.coreType)20 for frame in framenet.frames()21 for fe, frame_element in frame.FE.iteritems()),22 key=lambda x: x[0] + x[1])23# Names of all possible frames and FEs and coretypes (9633 total)24frame_fe_coretypes = sorted([(frame.name, fe, frame_element.coreType)25 for frame in framenet.frames()26 for fe, frame_element in frame.FE.iteritems()],27 key=lambda x: x[0] + x[1] + x[2])28###############################################################################29# Names of core FEs that are specific to a frame (857 total)30core_fes = sorted(set(fe for frame in framenet.frames()31 for fe, frame_element in frame.FE.iteritems()32 if frame_element.coreType == 'Core'))33# Names of extra-thematic FEs that belong to larger frames (236 total)34# - intersected with core: 9035extrathematic_fes = sorted(set(fe for frame in framenet.frames()36 for fe, frame_element in frame.FE.iteritems()37 if frame_element.coreType == 'Extra-Thematic'))38# Names of peripheral FEs that aren't specific to a frame (349 total)39# - intersected with core: 15740# - intersected with extra-thematic: 10241# - intersected with both: 6342peripheral_fes = sorted(set(fe for frame in framenet.frames()43 for fe, frame_element in frame.FE.iteritems()44 if frame_element.coreType == 'Peripheral'))45###############################################################################46def get_frame_ancestors(frame_name, limit=None):47 """Return names of all ancestors of the given frame in the Framenet48 taxonomy up to a specified distance.49 """50 traversed = []51 successors = [relation.Parent.name52 for relation in framenet.frame_relations(frame_name)53 if relation.type.name == 'Inheritance'54 and relation.Parent.name != frame_name]55 if limit is not None:56 limit -= 157 if limit is None or limit >= 0:58 for successor in successors:59 traversed.append(successor)60 traversed.extend(get_frame_ancestors(successor, limit=limit))61 return traversed62def get_coretype(frame_name, fe):63 """Return the core type of the given FE from the set of string types64 {'Core', 'Peripheral', 'Extra-Thematic'}65 """...

Full Screen

Full Screen

test_frames.py

Source:test_frames.py Github

copy

Full Screen

1import unittest, sys2from lxml.tests.common_imports import make_doctest, doctest3import lxml.html4from lxml.html import html_parser, XHTML_NAMESPACE5class FrameTest(unittest.TestCase):6 def test_parse_fragments_fromstring(self):7 parser = lxml.html.HTMLParser(encoding='utf-8', remove_comments=True)8 html = """<frameset>9 <frame src="main.php" name="srcpg" id="srcpg" frameborder="0" rolling="Auto" marginwidth="" marginheight="0">10 </frameset>"""11 etree_document = lxml.html.fragments_fromstring(html, parser=parser)12 self.assertEqual(len(etree_document), 1)13 root = etree_document[0]14 self.assertEqual(root.tag, "frameset")15 frame_element = root[0]16 self.assertEqual(frame_element.tag, 'frame')17 def test_parse_fromstring(self):18 parser = lxml.html.HTMLParser(encoding='utf-8', remove_comments=True)19 html = """<html><frameset>20 <frame src="main.php" name="srcpg" id="srcpg" frameborder="0" rolling="Auto" marginwidth="" marginheight="0">21 </frameset></html>"""22 etree_document = lxml.html.fromstring(html, parser=parser)23 self.assertEqual(etree_document.tag, 'html')24 self.assertEqual(len(etree_document), 1)25 frameset_element = etree_document[0]26 self.assertEqual(len(frameset_element), 1)27 frame_element = frameset_element[0]28 self.assertEqual(frame_element.tag, 'frame')29def test_suite():30 loader = unittest.TestLoader()...

Full Screen

Full Screen

switch.py

Source:switch.py Github

copy

Full Screen

1import pytest2from webdriver import StaleElementReferenceException3from tests.support.asserts import assert_error, assert_success4from tests.support.inline import inline, iframe5def switch_to_parent_frame(session):6 return session.transport.send(7 "POST", "session/{session_id}/frame/parent".format(**vars(session)))8def test_null_response_value(session):9 session.url = inline(iframe("<p>foo"))10 frame_element = session.find.css("iframe", all=False)11 session.switch_frame(frame_element)12 response = switch_to_parent_frame(session)13 value = assert_success(response)14 assert value is None15def test_no_browsing_context(session, closed_window):16 response = switch_to_parent_frame(session)17 assert_error(response, "no such window")18def test_stale_element_from_iframe(session):19 session.url = inline(iframe("<p>foo"))20 frame_element = session.find.css("iframe", all=False)21 session.switch_frame(frame_element)22 stale_element = session.find.css("p", all=False)23 result = switch_to_parent_frame(session)24 assert_success(result)25 with pytest.raises(StaleElementReferenceException):...

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