How to use wait_for_selector method in Playwright Python

Best Python code snippet using playwright-python

get-laurier-schedule.py

Source:get-laurier-schedule.py Github

copy

Full Screen

...7import os8from dotenv import load_dotenv9load_dotenv()10# Find element with WebDriverWait to prevent flakinesss11def wait_for_selector(driver, selector, seconds=10):12 wait = WebDriverWait(driver, seconds)13 element = wait.until(14 EC.element_to_be_clickable((By.CSS_SELECTOR, selector))15 )16 return element17# Use selenium to get all the Laurier class info18# for the current term19def getClassSchedule(driver):20 driver.get("https://loris.wlu.ca/register/ssb/term/termSelection?mode=search")21 # Select desired term22 open_search = wait_for_selector(driver, "b[role='presentation']")23 open_search.click()24 search = wait_for_selector(driver, "input#s2id_autogen1_search")25 search.send_keys(os.getenv('LAURIER_TERM'))26 select = wait_for_selector(driver, ".select2-result-label div")27 select.click()28 go_button = wait_for_selector(driver, "button#term-go")29 go_button.click()30 # Select to search all Business courses31 search = wait_for_selector(driver, "#s2id_txt_subject input")32 search.click()33 search.send_keys("Business")34 correct_option = wait_for_selector(driver, ".select2-results .select2-result-label div")35 correct_option.click()36 go_button = wait_for_selector(driver, "button#search-go")37 go_button.click()38 # Scrape each class39 courses = {}40 page = 141 while True:42 tbody = wait_for_selector(driver, "tbody")43 wait_for_selector(tbody, "tr")44 class_rows = tbody.find_elements(By.CSS_SELECTOR, "tr")45 page += 146 for class_row in class_rows:47 class_data = {}48 class_data["term"] = wait_for_selector(driver, "h4.search-results-header").text.replace("Term: ", "")49 # Open details popup50 wait_for_selector(class_row, "a.section-details-link").click() 51 wrapper_div = wait_for_selector(driver, "#classDetailsContentDetailsDiv")52 class_data["classNumber"] = wait_for_selector(wrapper_div, "#courseReferenceNumber").text53 class_data["section"] = wait_for_selector(wrapper_div, "#sectionNumber").text54 class_data["campusLocation"] = wrapper_div.text.split("\n")[2].replace("Campus: ", "")55 # Prepend type to section number for clarity56 type = wrapper_div.text.split("\n")[3].replace("Schedule Type: ", "")57 class_data["section"] = type + " " + wait_for_selector(wrapper_div, "#sectionNumber").text58 # Get course code59 course_name = "BU" + wait_for_selector(wrapper_div, "#courseNumber").text60 # Meeting details section61 wait_for_selector(driver, "#facultyMeetingTimes").click()62 # Schedule63 try:64 side_divs = WebDriverWait(driver, 10).until(65 EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".right > div"))66 )67 except TimeoutException:68 side_divs = []69 if (len(side_divs) < 2):70 # The date information is not there71 class_data["time"] = "Unknown"72 else:73 days_text = wait_for_selector(74 driver, ".right > div").text.replace("A", "").replace("P", "").replace("M", "").replace(" ", "")75 schedule_list = wait_for_selector(driver, "#classDetailsContentDiv ul")76 days = schedule_list.find_elements(By.CSS_SELECTOR, "li")77 for day in days:78 if day.get_attribute("aria-checked") == "true":79 days_text += day.get_attribute("data-abbreviation")80 class_data["time"] = days_text.replace("R", "Th")81 # Instructor82 class_data["instructor"] = wait_for_selector(83 driver, ".meeting-faculty").text.replace("Instructor:", "").strip(" ")84 # Location85 try:86 location = wait_for_selector(driver, ".right > div:nth-child(2)").text87 location_list = location.split("|")88 if len(location_list) == 3:89 location = location_list[2].replace(" Room ", "")90 except TimeoutException:91 # The location information is not there92 location = "Unknown"93 class_data["room"] = location94 # Enrollment/Waitlist details section95 wait_for_selector(driver, "#enrollmentInfo").click()96 # Enrollment97 wait_for_selector(driver, "#classDetailsContentDetailsDiv span[dir='ltr']")98 spans = driver.find_elements(By.CSS_SELECTOR, "#classDetailsContentDetailsDiv span[dir='ltr']")99 class_data["enrolCap"] = spans[1].text100 class_data["enrolTotal"] = spans[0].text101 # Close details popup102 wait_for_selector(driver, ".ui-icon-closethick").click()103 # Add to course dictionary104 print(class_data)105 if course_name in courses:106 courses[course_name].append(class_data)107 else:108 courses[course_name] = [class_data]109 110 # Try to go to the next page and get all the rows again111 old_page_num = wait_for_selector(driver, ".page-number").get_attribute("value")112 try:113 print("Loading next page...")114 wait_for_selector(driver, "button[title='Next'].enabled").click()115 except TimeoutException:116 # No more next button117 print("No next page")118 print("All pages scraped")119 break120 # Make sure the page actually changed (takes time to load)121 try:122 WebDriverWait(driver, 10).until(123 lambda _: driver.find_element_by_css_selector(".page-number").get_attribute("value") != old_page_num)124 except StaleElementReferenceException:125 # Page changed before the value could be checked126 pass127 print(f"Loaded page {page}")128 return courses...

Full Screen

Full Screen

solver.py

Source:solver.py Github

copy

Full Screen

...77 async def goto_page(self):78 await self.page.goto(self.pageurl,wait_until="commit")79 await self.page.wait_for_load_state("load")80 async def click_checkbox(self):81 checkbox = await self.checkbox_frame.wait_for_selector(82 "#recaptcha-anchor")83 await checkbox.click()84 async def get_frames(self):85 await self.page.wait_for_selector("iframe[src*=\"api2/anchor\"]",86 state='attached')87 self.checkbox_frame = next(frame for frame in self.page.frames 88 if "api2/anchor" in frame.url)89 await self.page.wait_for_selector("iframe[src*=\"api2/bframe\"]",90 state='attached')91 self.image_frame = next(frame for frame in self.page.frames 92 if "api2/bframe" in frame.url)93 async def click_audio_button(self):94 audio_button = await self.image_frame.wait_for_selector("#recaptcha-au"95 "dio-button")96 await audio_button.click()97 async def check_detection(self, timeout):98 timeout = time.time() + timeout99 while time.time() < timeout:100 content = await self.image_frame.content()101 if 'Try again later' in content:102 return 'detected'103 elif 'Press PLAY to listen' in content:104 return 'solve'105 else:106 result = await self.page.evaluate('document.getElementById("g-'107 'recaptcha-response").value !== ""')108 if result:109 return result110 async def solve_audio(self):111 await self.click_audio_button()112 while 1:113 if result == 'solve':114 play_button = await self.image_frame.wait_for_selector("#audio-source",115 state="attached")116 audio_source = await play_button.evaluate("node => node.src")117 audio_data = await utils.get_page(audio_source, binary=True)118 tmpd = tempfile.mkdtemp()119 tmpf = os.path.join(tmpd, "audio.mp3")120 await utils.save_file(tmpf, data=audio_data, binary=True)121 audio_response = await utils.get_text(tmpf)122 audio_response_input = await self.image_frame.wait_for_selector("#audi"123 "o-response", state="attached")124 await audio_response_input.fill(audio_response['text'])125 recaptcha_verify_button = await self.image_frame.wait_for_selector("#r"126 "ecaptcha-verify-button", state="attached")127 await recaptcha_verify_button.click()128 shutil.rmtree(tmpd)129 result = await self.get_recaptcha_response()130 if result:131 return result132 else:133 break134 async def solve_image(self):135 im = image.SolveImage(self.page, self.image_frame, net=self.net)136 await im.solve_by_image()137 result = await self.get_recaptcha_response()138 if result:139 return result...

Full Screen

Full Screen

test_helper.py

Source:test_helper.py Github

copy

Full Screen

...5# Wait until an element with `result_selector` is available, and return that6# element.7def run_jupyter_cell(page, substr, result_selector, **kwargs):8 selector = f'pre[role="presentation"]:has-text("{substr}")'9 pre = page.wait_for_selector(selector)10 pre.click()11 cell = page.wait_for_selector(f'div.cell.selected:has({selector})')12 focus = cell.wait_for_selector('textarea')13 focus.scroll_into_view_if_needed()14 focus.press('Control+Enter')15 return cell, cell.wait_for_selector(result_selector, **kwargs)16def save(page):17 page.press('text=File', 'Control+S')18 page.wait_for_timeout(500) # Wait 0.5 sec just be sure.19def restart_kernel(page):20 print('Restarting the ipython kernel ...')21 # Restart sometimes takes forever (including the next cell execution), so we22 # need huge timeout.23 page.set_default_timeout(10000) # 10 sec.24 page.click('button.btn[title*="restart the kernel"]')25 page.wait_for_selector('text="Restart kernel?"')26 page.click('button:has-text("Restart")') # Confirm.27 page.wait_for_selector('.kernel_idle_icon[id=kernel_indicator_icon]')28def get_center_coord(elem):29 box = elem.bounding_box()30 return box['x'] + box['width'] / 2, box['y'] + box['height'] / 231def get_center_coord1(page, selector):32 item = page.wait_for_selector(selector)33 if item is None: return None34 return get_center_coord(item)35# Hover mouse cursor at the given coordinate, verify that the tooltip's content36# matches the given condition, and return.37#38# TODO: Get timeout from command line argument?39def verify_tooltip(page, cell, coord_callback, verify_callback, timeout=2500):40 x, y = -999, -99941 T = time.time()42 while time.time() < T + timeout * 0.001:43 x1, y1 = coord_callback()44 if abs(x - x1) + abs(y - y1) > 1.0:45 # import datetime; print(datetime.datetime.now())46 # print(f'Moving cursor from {x} {y} to {x1} {y1}')47 x, y = x1, y148 page.mouse.move(x, y)49 # Just wait 100 ms, because element coordinates may change.50 try:51 tooltip = cell.wait_for_selector(52 'div.cr_tooltip', state='visible', timeout=100)53 except playwright.async_api.TimeoutError:54 continue55 content = tooltip.text_content()56 if verify_callback(content): return content57 # page.pause()58 raise TimeoutError59# Check the plot is functioning by hovering at the center of the plot and60# checking that we get the correct label.61def check_center_label(page, cell, label, **kwargs):62 def coord_cb():63 grid = cell.wait_for_selector('.cr_grid')64 grid.scroll_into_view_if_needed()65 return get_center_coord(grid)...

Full Screen

Full Screen

test_unit.py

Source:test_unit.py Github

copy

Full Screen

...4 This test checks for selectors to download5 """6 page.goto("https://etf.dws.com/en-us/DBJP-msci-japan-hedged-equity-etf/")7 8 accept_button_1 = page.wait_for_selector("text=Accept and proceed")9 visible_accept_button_1 = accept_button_1.is_visible()10 assert visible_accept_button_111 12 page.click("text=Accept and proceed")13 14 role_type = page.wait_for_selector(".audience-selection__item-overlay")15 visible_role_type = role_type.is_visible()16 assert visible_role_type17 18 page.click(".audience-selection__item-overlay")19 20 accept_button_2 = page.wait_for_selector("text=Accept")21 visible_accept_button_2 = accept_button_2.is_visible()22 assert visible_accept_button_223 24 with page.expect_navigation():25 accept_button_2 = page.wait_for_selector("text=Accept")26 visible_accept_button_2 = accept_button_2.is_visible()27 assert visible_accept_button_228 29 page.click("text=Accept")30 31 download_button = page.wait_for_selector("text=Download Fund Holdings")32 visible_download_button = download_button.is_visible()33 assert visible_download_button34 35def test_unit_download(mocker):36 """37 This test checks for download functionality in a download function38 """39 page = mocker.MagicMock()40 download(page)41 page.goto.assert_called_with("https://etf.dws.com/en-us/DBJP-msci-japan-hedged-equity-etf/")42 calls = [mocker.call.click("text=Accept and proceed"),43 mocker.call.click(".audience-selection__item-overlay"),44 mocker.call.click("text=Accept"),45 mocker.call.click("text=Download Fund Holdings")]...

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