Best Python code snippet using playwright-python
badminton.py
Source:badminton.py
...55 ):56 page = context.new_page()57 # badminton workflow url58 page.goto("https://oa.shanghaitech.edu.cn/workflow/request/AddRequest.jsp?workflowid=14862")59 page.frame_locator('iframe[name="bodyiframe"]').locator("#field31898_0").wait_for()60 frame0 = page.frame("bodyiframe")61 frame0.locator("#field31898_0").fill(Class_info.list2d_jieyongren[0][0])62 frame0.locator("#field31899_0").fill(Class_info.list2d_jieyongren[0][1])63 frame0.locator("#field31898_1").fill(Class_info.list2d_jieyongren[1][0])64 frame0.locator("#field31899_1").fill(Class_info.list2d_jieyongren[1][1])65 # åºé¦ç±»å66 frame0.select_option('select#field32340',"4")67 # 使ç¨æ¥æ68 frame0.locator('button#field31901browser').click()69 frame1 = frame0.frame_locator('[id="_my97DP"] iframe')70 frame1.locator('[onclick="day_Click('+Class_info.str_date+');"]').click()71 # 使ç¨æ¶é´72 frame0.locator('button#field31902_browserbtn').click()73 frame2 = page.frame_locator('iframe[src="/systeminfo/BrowserMain.jsp?url=/interface/CommonBrowser.jsp?type=browser.sysjd|31902"]').frame_locator('iframe[name="main"]')74 frame2.locator('input[name="con31860_value"]').fill(Class_info.str_time)75 frame2.locator('input[id="btnsearch"]').click()76 frame2.locator('td:has-text("'+Class_info.str_time+'")').click()77 # 使ç¨åºå°78 frame0.click('button#field31883_browserbtn')79 frame3 = page.frame_locator('iframe[src="/systeminfo/BrowserMain.jsp?url=/formmode/tree/treebrowser/CustomTreeBrowser.jsp%3Fderecorderindex%3D%26type%3D63_256_256%26selectedids%3D"]').frame_locator('iframe[id="main"]').frame_locator('iframe[id="tabcontentframe"]')80 # åºé¦ä½¿ç¨æ
åµè¡¨81 # ä½è²é¦82 # 室å
ç¾½æ¯çåº83 for int_i in range(3):84 ico = frame3.locator("#CustomTree_"+str(int_i+1)+"_ico")85 button = ico.get_attribute('class')86 if (button == 'button ico_close' ):87 ico.click()88 elif ( button == 'button ico_open' ):89 pass90 elif ( button == 'button ico_docu' ):91 print('æ¤æ¶é´æ®µç³è¯·äººå·²æ»¡ï¼')92 return93 # ç¾½æ¯çåºå°1å·...
main.py
Source:main.py
...38 page.goto("http://43.248.49.97/")39 time.sleep(1)40 pyautogui.hotkey('win', 'up')41 # Select 342 page.frame_locator("iframe").nth(1).locator("select[name=\"year\"]").select_option("2021")43 page.wait_for_timeout(100)44 page.frame_locator("iframe").nth(1).locator("select[name=\"year\"]").click()45 page.wait_for_timeout(100)46 page.frame_locator("iframe").nth(1).locator("select[name=\"startMonth\"]").select_option("1")47 page.wait_for_timeout(100)48 page.frame_locator("iframe").nth(1).locator("select[name=\"startMonth\"]").click()49 page.wait_for_timeout(100)50 page.frame_locator("iframe").nth(1).locator("select[name=\"endMonth\"]").select_option("3")51 page.wait_for_timeout(100)52 page.frame_locator("iframe").nth(1).locator("select[name=\"endMonth\"]").click()53 page.wait_for_timeout(100)54 # Select CODE_TS55 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField1\"]").select_option("CODE_TS")56 page.wait_for_timeout(100)57 # Click input[name="outerValue1"]58 page.frame_locator("iframe").nth(1).locator("input[name=\"outerValue1\"]").click()59 page.wait_for_timeout(100)60 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField2\"]").select_option("ORIGIN_COUNTRY")61 page.wait_for_timeout(100)62 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField2\"]").click()63 # Select TRADE_MODE64 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField3\"]").select_option("TRADE_MODE")65 page.wait_for_timeout(100)66 # Select TRADE_CO_PORT67 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField3\"]").click()68 page.wait_for_timeout(100)69 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField4\"]").select_option("TRADE_CO_PORT")70 # Fill input[name="outerValue1"]71 page.wait_for_timeout(100)72 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField4\"]").click()73 page.wait_for_timeout(100)74 page.frame_locator("iframe").nth(1).locator("input[name=\"outerValue1\"]").fill(f"{key}")75 page.wait_for_timeout(100)76 # Click text=æ¥è¯¢77 page.frame_locator("iframe").nth(1).locator("text=æ¥è¯¢").click()78 page.wait_for_timeout(300)79 # Click text=ç¡®å®80 page.frame_locator("iframe").nth(1).locator("text=ç¡®å®").click()81 page.wait_for_timeout(2000)82 page.screenshot(path='page.png', full_page=True)83 print('=== screenshot page ==')84 crop_image('page.png')85 time.sleep(1)86 mouse_action(mark_edge('1.jpg'))87 try:88 page.frame_locator("iframe").nth(1).frame_locator("iframe[name=\"layui-layer-iframe2\"]").locator(89 "text=ç¡®å®").click()90 except:91 page.screenshot(path='page.png', full_page=True)92 print('=== screenshot page ==')93 crop_image('page.png')94 time.sleep(1)95 mouse_action(mark_edge('1.jpg'))96 # Close page97 page.wait_for_timeout(300)98 # Click text=ç¡®å®99 page.frame_locator("iframe").nth(1).frame_locator("iframe[name=\"layui-layer-iframe2\"]").locator("text=ç¡®å®").click()100 page.wait_for_timeout(200)101 # Click text=è¿å设置102 page.frame_locator("iframe").nth(1).locator("text=è¿å设置").click()103 page.wait_for_timeout(500)104 page.close()105 # ---------------------106 # context.close()107 # browser.close()108def mouse_action(x: int):109 pyautogui.moveTo(919, 675)110 pyautogui.click(919, 675)111 time.sleep(random.uniform(1, 2))112 pyautogui.dragTo(980 + x, 675, duration=random.uniform(1, 2))113 time.sleep(random.uniform(1, 2))114def mark_edge(image_path: str):115 p = cv2.imread(image_path)116 hsv = cv2.cvtColor(p, cv2.COLOR_BGR2HSV)...
demo1.py
Source:demo1.py
...30 # 0à click31 page.locator("html").click()32 page.wait_for_timeout(1000)33 # Select 334 page.frame_locator("iframe").nth(1).locator("select[name=\"year\"]").select_option("2021")35 page.wait_for_timeout(200)36 page.frame_locator("iframe").nth(1).locator("select[name=\"year\"]").click()37 page.wait_for_timeout(200)38 page.frame_locator("iframe").nth(1).locator("select[name=\"startMonth\"]").select_option("1")39 page.wait_for_timeout(200)40 page.frame_locator("iframe").nth(1).locator("select[name=\"startMonth\"]").click()41 page.wait_for_timeout(200)42 page.frame_locator("iframe").nth(1).locator("select[name=\"endMonth\"]").select_option("3")43 page.wait_for_timeout(200)44 page.frame_locator("iframe").nth(1).locator("select[name=\"endMonth\"]").click()45 page.wait_for_timeout(200)46 # Select CODE_TS47 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField1\"]").select_option("CODE_TS")48 page.wait_for_timeout(200)49 # Click input[name="outerValue1"]50 page.frame_locator("iframe").nth(1).locator("input[name=\"outerValue1\"]").click()51 page.wait_for_timeout(200)52 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField2\"]").select_option("ORIGIN_COUNTRY")53 page.wait_for_timeout(200)54 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField2\"]").click()55 # Select TRADE_MODE56 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField3\"]").select_option("TRADE_MODE")57 page.wait_for_timeout(200)58 # Select TRADE_CO_PORT59 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField3\"]").click()60 page.wait_for_timeout(200)61 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField4\"]").select_option("TRADE_CO_PORT")62 # Fill input[name="outerValue1"]63 page.wait_for_timeout(200)64 page.frame_locator("iframe").nth(1).locator("select[name=\"outerField4\"]").click()65 page.wait_for_timeout(200)66 page.frame_locator("iframe").nth(1).locator("input[name=\"outerValue1\"]").fill(f"{key}")67 page.wait_for_timeout(300)68 # Click text=æ¥è¯¢69 page.frame_locator("iframe").nth(1).locator("text=æ¥è¯¢").click()70 page.wait_for_timeout(300)71 # Click text=ç¡®å®72 page.frame_locator("iframe").nth(1).locator("text=ç¡®å®").click()73 page.wait_for_timeout(2000)74 page.screenshot(path='page.png', full_page=True)75 print('=== screenshot page ==')76 image_action('page.png')77 time.sleep(1)78 mouse_action(mark_edge('1.jpg'))79 # Close page80 page.wait_for_timeout(300)81 page.frame_locator("iframe").first.frame_locator("iframe[name=\"layui-layer-iframe2\"]").locator(82 "text=ç¡®å®").click()83 page.wait_for_timeout(500)84 page.close()85 # ---------------------86 context.close()87 browser.close()88# with sync_playwright() as playwright:89# run(playwright, '04063000')...
get_element_module.py
Source:get_element_module.py
1from JDI.core.settings.jdi_settings import JDISettings2class GetElementModule(object):3 FAILED_TO_FIND_ELEMENT_MESSAGE = "Can't find Element '{0}' during seconds"4 FIND_TO_MUCH_ELEMENTS_MESSAGE = "Find %s elements instead of one for Element '%s' during %s seconds"5 def __init__(self, by_locator=None, element=None):6 self.by_locator = by_locator7 self.element = element8 self.web_element = None9 self.web_elements = []10 self.frame_locator = None11 # self.logger = JDISettings.get_logger()12 def get_element(self):13 #self.logger.debug("Get Web Element: " + str(self.element))14 # element = self.web_element if self.web_element is not None else self.__get_element_action()15 # if self.web_element is None:16 self.web_element = self.__get_element_action()17 element = self.web_element18 # self.logger.debug("One Element found")19 return element20 def get_elements(self):21 result = self.__search_elements()22 if result is None:23 raise Exception("Can't get Web Elements")24 return result25 def __get_element_action(self):26 result = self.__get_one_or_more_elements()27 if len(result) == 0:28 raise Exception(GetElementModule.FAILED_TO_FIND_ELEMENT_MESSAGE.format(self.element))29 elif len(result) == 1:30 return result[0]31 elif len(result) > 1:32 result = [element for element in result if element.is_displayed()]33 return result[0]34 else:35 raise Exception(GetElementModule.FIND_TO_MUCH_ELEMENTS_MESSAGE % len(result), self.element)36 def __get_one_or_more_elements(self):37 return self.web_elements if self.web_elements else self.__search_elements()38 def __search_elements(self):39 # TODO: containsRoot40 locator = self.by_locator41 search_context = self.get_search_context(self.element.parent)42 if search_context is None:43 search_context = self.get_driver()44 # if self.frame_locator is not None:45 # self.get_driver().switch_to().frame(self.get_driver().find_element(self.frame_locator))46 return search_context.find_elements(locator[0], locator[1])47 @staticmethod48 def get_driver():49 return JDISettings.get_driver_factory().get_driver()50 def get_search_context(self, b_element):51 from JDI.web.selenium.elements.composite.web_site import WebSite52 try:53 if issubclass(type(self.element.parent), WebSite) or issubclass(self.element.parent, WebSite):54 return None55 except: pass56 if self.element.parent is not None:57 locator = b_element.avatar.by_locator58 if locator is None:59 return None60 driver = self.get_driver()61 if b_element.avatar.frame_locator is not None:62 self.switch_to_last_opened_window()63 res = driver.find_element(locator[0], locator[1])64 driver.switch_to_frame(b_element.avatar.frame_locator[1])65 return driver66 return driver.find_element(locator[0], locator[1])67 def switch_to_last_opened_window(self):...
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.
Get 100 minutes of automation test minutes FREE!!