How to use query_selector_all method in Playwright Python

Best Python code snippet using playwright-python

fb_page.py

Source:fb_page.py Github

copy

Full Screen

...73 async def get_posts(self, full: int = 0, limit: int = 0, as_text: int = 0, include_comments: int = 0) -> List[Post]:74 posts = []75 posts_urls = []76 if self._is_main_page:77 script = [await i.inner_html() for i in await self.page.query_selector_all('script') if78 'share_fbid' in await i.inner_html()][0]79 posts = [i for i in nested_lookup('story', json.loads(80 re.search(r'{\"define.*\);}\);}\);', script).group().replace(');});});', ''))) if81 (i.get('url') or i.get('permalink')) and ('/posts/' in i.get('url') or 'permalink' in i.get('url'))82 and i.get('creation_time')]83 posts_urls: List[str] = sorted([i['url'] for i in posts], key=lambda x: x.split('/')[:-1],84 reverse=not self._is_group)85 if not self._is_group:86 start = 087 else:88 if posts_urls:89 start = 1 if len(posts_urls) > 1 else 090 else:91 start = 192 if not full:93 if not self._is_group:94 posts_count = 195 else:96 if posts_urls:97 posts_count = len(posts_urls)98 else:99 posts_count = 2100 else:101 posts_count: int = len(await self.page.query_selector_all(self.posts_selector))102 posts_items = []103 for item in range(start, posts_count):104 if limit and item > limit:105 continue106 full_text = ""107 post_html = None108 if not self._is_main_page:109 posts: List[ElementHandle] = await self.page.query_selector_all(self.posts_selector)110 if posts_urls:111 post_url = posts_urls[item]112 else:113 post_url = await posts[item].get_attribute('href')114 if post_url.startswith('/'):115 post_url = f"https://mbasic.facebook.com{post_url}"116 else:117 post_url = post_url.replace("m.facebook", "mbasic.facebook")118 await self.open(post_url)119 await self.page.wait_for_selector(self._post_selector)120 post = await self.page.query_selector(self._post_selector)121 if not posts_urls:122 date = await post.query_selector(self._publish_date_selector)123 if date:124 date = await date.text_content()125 else:126 date = datetime.fromtimestamp(posts[item]['creation_time'])127 profile_name = await post.query_selector(self._author_selector)128 if profile_name:129 profile_name = await profile_name.text_content()130 full_text = f"{profile_name} posted:\n"131 if not as_text:132 post_html = ""133 await self.page.wait_for_selector(self._post_content_selector)134 html_parts = await self.page.query_selector_all(135 f"{self._post_selector}{self._post_content_selector}") \136 if not posts_urls else await self.page.query_selector(137 f"{self._post_selector}{self._post_content_selector}")138 if isinstance(html_parts, list):139 for part in html_parts:140 post_html += strip_tags(await part.inner_html())141 else:142 post_html += strip_tags(await html_parts.inner_html())143 post_html = re.sub(r'href=\"/', 'href=\"https://facebook.com/', post_html, re.M)144 text_obj = await post.query_selector(self._post_text_selector)145 text = await text_obj.text_content()146 title = f"{text[:30]}..."147 else:148 if date:149 full_text += date150 text_chunks = await post.query_selector_all(self._post_text_selector)151 text = [await i.text_content() for i in text_chunks]152 text = '\n'.join(text) + '\n'153 title = f"{text[:30]}..."154 full_text += text155 attached_link = await post.query_selector(self._attached_link_selector)156 if attached_link:157 attached_link_href = await attached_link.get_attribute('href')158 if not attached_link_href.startswith("http"):159 attached_link_href = "https://facebook.com" + attached_link_href160 attached_link_text = await attached_link.text_content()161 if attached_link_text:162 attached_link_text = f"[{attached_link_text}]"163 full_text += f"{attached_link_text}({attached_link_href})\n"164 images = await post.query_selector_all(self._image_selector)165 if images:166 for image in images:167 full_text += f"{await image.get_attribute('src')}\n"168 videos = await post.query_selector_all(self._video_selector)169 if videos:170 for video in videos:171 video_link_href = await video.get_attribute('href')172 if not video_link_href.startswith("http"):173 video_link_href = "https://facebook.com" + video_link_href174 full_text += f"{video_link_href}\n"175 # print(full_text)176 if include_comments:177 # TODO: Fix wrong comments in main site178 comments = await self.page.query_selector_all(self._comments_selector)179 if comments:180 for comment in comments:181 comment_content = await comment.query_selector(f"xpath={self._comment_content_selector}")182 comment_author = await comment.query_selector(f"xpath={self._comment_author_selector}")183 comment_author_text = await comment_author.text_content()184 if not as_text:185 comment_html = strip_tags(await comment_content.inner_html())186 comment_html = re.sub(r'href=\"/', 'href=\"https://facebook.com/', comment_html, re.M)187 post_html += f'<br><br>{comment_author_text}: {comment_html}'188 else:189 comment_text = await comment.text_content()190 full_text += f'\n\n{comment_author_text}: {comment_text}'191 if full:192 await random_sleep()...

Full Screen

Full Screen

scripts.py

Source:scripts.py Github

copy

Full Screen

...16 title = get_text(site_map_category.query_selector(SiteMapLocators.title))17 categories[title] = dict()18 producer_logger.debug(f"Grabbing sub-categories for Category -> {title}")19 # grab sub-categories under title/categories 20 site_map_sections = site_map_category.query_selector_all(SiteMapLocators.site_map_sections)21 for site_map_section in site_map_sections:22 # grab the text for the sub-categories23 site_map_item_title = site_map_section.query_selector(SiteMapLocators.site_map_item_title)24 site_map_item_title = get_text(site_map_item_title)25 26 # grab all hrefs of items under each sub-categories27 site_map_items = site_map_section.query_selector_all(SiteMapLocators.site_map_items)[1:]28 site_map_items_dict = dict()29 30 for site_map_item in site_map_items:31 site_map_items_dict[get_text(site_map_item)] = site_map_item.get_attribute('href')32 33 # add the dict holding the sub-categories and their hrefs34 categories[title][site_map_item_title] = site_map_items_dict35def get_categories() -> Dict:36 ''' Categories - Kadin, Erkek, Teen, Cocuk '''37 categories = dict()38 producer_logger.debug("Getting SiteMap Categories")39 # site map category elements40 site_map_categories = page.query_selector_all(SiteMapLocators.site_map_categories)41 producer_logger.debug("SiteMap Categories Found")42 producer_logger.debug("Traversing SiteMap Categories")43 for site_map_category in site_map_categories:44 get_sub_categories(categories, site_map_category)45 return categories46def traverse_sitemap() -> None:47 sitemap_url: str = f"{base_url}/tr/sitemap"48 # traverse the sitemap url49 producer_logger.info(f"Going to {sitemap_url}")50 page.goto(sitemap_url)51 categories = get_categories()52 return categories53def get_products_href(count: int = 0) -> None:54 # while True:55 products = page.query_selector_all('li[id*="product-key-id"]')56 current_number_of_products = len(products)57 # if count < current_number_of_products:58 for product in products[count:10]:59 product.scroll_into_view_if_needed()60 href = product.query_selector('a').get_attribute('href')61 products_hrefs.add(href)62 producer_logger.debug(f"Found -> {href}")63 count += 164 # else:65 # break66 # wait some seconds for product list to update67 sleep()68def set_category(category: str, product: Dict) -> None:69 producer_logger.debug(category)70 product['Category'] = category71def set_sub_category(subcategory: str, product: Dict) -> None:72 producer_logger.debug(subcategory)73 product['Sub Category'] = subcategory74def get_name(product: Dict) -> None:75 # product name76 product_name = get_text(page.wait_for_selector(ProductPageLocators.product_name))77 producer_logger.debug(product_name)78 product['Name'] = product_name79def join_collection(collection: Any) -> str:80 ''' helper function '''81 if isinstance(collection, dict):82 return '\n\n'.join([f"{key}:{collection.get(key)}" for key in collection ])83 elif isinstance(collection, list):84 return '\n\n'.join(collection)85def get_description(product: Dict) -> None:86 # description87 product_info_block = page.query_selector_all('div.product-info-block')88 description = dict()89 for block in product_info_block:90 product_info_title = get_text(block.query_selector('h2.product-info-title')) 91 product_info_text = get_text(block.query_selector('p.product-info-text')) 92 description[product_info_title] = product_info_text93 producer_logger.debug(description)94 product['Description'] = join_collection(description)95def get_colors(product: Dict) -> None:96 # color option97 product_colors = page.query_selector(ProductPageLocators.product_colors)98 colors_info = product_colors.query_selector_all(ProductPageLocators.colors_info)99 colors_img = product_colors.query_selector_all(ProductPageLocators.colors_img)100 color_option = { get_text(key): value.get_attribute('src') for key, value in zip(colors_info, colors_img) } 101 producer_logger.debug(color_option)102 product['Color'] = join_collection(color_option)103def get_size(product: Dict) -> None:104 # size option105 size_options = page.query_selector_all(ProductPageLocators.size_options)106 # size_option = [ {get_text(size): size.get_attribute('data-value')} for size in size_options ]107 size_option = [ f"{get_text(size)}:{size.get_attribute('data-value')}" for size in size_options ]108 producer_logger.debug(size_option)109 product['Size'] = join_collection(size_option)110def get_prices(product: Dict) -> None:111 product_prices = page.query_selector(ProductPageLocators.product_prices)112 113 sale_price = get_text(product_prices.query_selector(ProductPageLocators.product_sale))114 product['Sale Price'] = sale_price115 sale_percentage = get_text(product_prices.query_selector('span.product-discount')) 116 product['Sale Percentage'] = sale_percentage117 original_price = get_text(product_prices.query_selector('span.product-sale--cross'))118 product['Original Price'] = original_price119def get_sku(product: Dict) -> None:120 # product SKU121 product_sku = get_text(page.query_selector(ProductPageLocators.product_sku))122 producer_logger.debug(product_sku) 123 product['SKU'] = product_sku124def get_seo(product: Dict) -> None:125 # SEO126 seo = page.query_selector(ProductPageLocators.seo).get_attribute('content').strip()127 producer_logger.debug(seo)128 product['SEO'] = seo129def get_images(product: Dict) -> None:130 # product images131 images = page.query_selector_all(ProductPageLocators.images)132 product_images = [ f"https:{image.get_attribute('src')}" for image in images ]133 producer_logger.debug(product_images)134 product['Images'] = join_collection(product_images)135def get_tags(product: Dict) -> None:136 # product tags137 tag_elements = page.query_selector_all(ProductPageLocators.tag_elements)138 product_tags = [ get_text(tag) for tag in tag_elements ] 139 producer_logger.debug(product_tags)140 product["Tags"] = join_collection(product_tags)141def get_product(url: str) -> None:142 product = dict()143 product_url = f"{base_url}{url}"144 page.goto(product_url)145 product['Product URL'] = product_url146 set_category(category, product)147 set_sub_category(sub_category, product) 148 sleep()149 get_name(product); get_description(product)150 get_colors(product); get_size(product)151 get_prices(product); get_sku(product)...

Full Screen

Full Screen

test_honey_integration.py

Source:test_honey_integration.py Github

copy

Full Screen

...3 id = input_for_label(page, label)4 return page.fill(f"#{id}", value)5def input_for_label(page, label):6 assert "'" not in label7 labels = page.query_selector_all(f"label:has-text('{label}')")8 assert len(labels) == 19 return labels[0].get_attribute("for")10def labeled_input_value(page, label):11 id = input_for_label(page, label)12 return page.input_value(f"#{id}")13def errors(page):14 return [elt.inner_text() for elt in page.query_selector_all(".errormsg")]15def nav(meth, *args, **kwargs):16 __tracebackhide__ = True17 with meth.__self__.expect_response("*") as response_info:18 meth(*args, **kwargs)19 response = response_info.value20 assert response.status == 200, f"{response.request.method} {response.url} returned {response.status}"21@pytest.fixture(autouse=True)22def fail_faster(page):23 page.set_default_timeout(5000)24PREVIEW = "Preview >>"25ADD_IT = "Add it >>"26BLOG_POST = "/blog/200203/my_first_job_ever.html"27@pytest.mark.parametrize("url, title", [28 ("/blog/202108/me_on_bug_hunters_caf.html", "Me on Bug Hunters Café"),29 ("/blog/200203/my_first_job_ever.html", "My first job ever"),30])31def test_blog_post(page, url, title):32 nav(page.goto, url)33 assert page.inner_text("h1") == title34 assert page.title() == f"{title} | Ned Batchelder"35 # No errors on the page.36 assert errors(page) == []37 # No previewed comment.38 assert page.query_selector_all(".comment.preview") == []39 # There is a preview button, but no Add It button.40 assert len(page.query_selector_all(f"input:has-text('{PREVIEW}')")) == 141 assert len(page.query_selector_all(f"input:has-text('{ADD_IT}')")) == 042def test_previewing(page):43 # Load the page and fill in the comment form.44 nav(page.goto, BLOG_POST)45 assert len(page.query_selector_all(".comment.preview")) == 046 fill_labeled_input(page, "Name:", "Thomas Edison")47 fill_labeled_input(page, "Email:", "tom@edison.org")48 fill_labeled_input(page, "Comment:", "This is a great blog post!")49 # Click "preview", the page has a preview, and filled in fields.50 nav(page.click, f"input:has-text('{PREVIEW}')")51 assert errors(page) == []52 assert len(page.query_selector_all(".comment.preview")) == 153 assert page.query_selector(".comment.preview .who").inner_text() == "Thomas Edison"54 assert page.query_selector(".comment.preview .commenttext").inner_text() == "This is a great blog post!"55 assert labeled_input_value(page, "Name:") == "Thomas Edison"56 assert labeled_input_value(page, "Email:") == "tom@edison.org"57 assert labeled_input_value(page, "Comment:") == "This is a great blog post!"58 assert len(page.query_selector_all(f"input:has-text('{PREVIEW}')")) == 159 assert len(page.query_selector_all(f"input:has-text('{ADD_IT}')")) == 160 # Reload the page, it has no preview, but the simple fields are filled in.61 nav(page.goto, BLOG_POST)62 assert len(page.query_selector_all(".comment.preview")) == 063 assert labeled_input_value(page, "Name:") == "Thomas Edison"64 assert labeled_input_value(page, "Email:") == "tom@edison.org"65 assert labeled_input_value(page, "Comment:") == ""66NAME_MSG = "You must provide a name."67EMWB_MSG = "You must provide either an email or a website."68COMM_MSG = "You didn't write a comment!"69@pytest.mark.parametrize("name, email, website, comment, msgs", [70 (False, False, False, True, [NAME_MSG, EMWB_MSG]),71 (True, False, False, True, [EMWB_MSG]),72 (False, True, False, True, [NAME_MSG]),73 (True, True, False, False, [COMM_MSG]),74 (True, False, True, False, [COMM_MSG]),75 (True, False, False, False, [EMWB_MSG, COMM_MSG]),76])77def test_missing_info(page, name, email, website, comment, msgs):78 # Load the page and fill in the comment form.79 nav(page.goto, BLOG_POST)80 if name:81 fill_labeled_input(page, "Name:", " Thomas Edison")82 if email:83 fill_labeled_input(page, "Email:", "tom@edison.org ")84 if website:85 fill_labeled_input(page, "Web site:", " https://edison.org ")86 if comment:87 fill_labeled_input(page, "Comment:", "This is a great blog post!")88 # Click "preview", the page has errors and no preview.89 nav(page.click, f"input:has-text('{PREVIEW}')")90 assert errors(page) == msgs91 assert len(page.query_selector_all(".comment.preview")) == 092 assert labeled_input_value(page, "Comment:") == ("This is a great blog post!" if comment else "")93 assert labeled_input_value(page, "Name:") == ("Thomas Edison" if name else "")94 assert labeled_input_value(page, "Email:") == ("tom@edison.org" if email else "")95 assert labeled_input_value(page, "Web site:") == ("https://edison.org" if website else "")96 assert len(page.query_selector_all(f"input:has-text('{PREVIEW}')")) == 1...

Full Screen

Full Screen

test_todo.py

Source:test_todo.py Github

copy

Full Screen

...41 page.goto("http://localhost:8000/")42 page.fill("#title", "Task title")43 page.fill("#dueDate", "2021-10-20")44 page.click("#addTaskBtn")45 all_rows = page.query_selector_all("table[id='taskList'] > tbody > tr")46 new_row = all_rows[-1]47 assert len(all_rows) == 148 assert new_row.query_selector_all("td")[1].inner_text() == "Task title"49 assert new_row.query_selector_all("td")[2].inner_text() == "Aardvark"50 assert new_row.query_selector_all("td")[3].inner_text() == "Low"51 assert new_row.query_selector_all("td")[4].inner_text() == "2021-10-20"52@pytest.mark.parametrize(53 "worker",54 [55 "Aardvark",56 "Beaver",57 "Cheetah",58 "Dolphin",59 "Elephant",60 "Flamingo",61 "Giraffe",62 "Hippo",63 ],64)65def test_select_worker(page: Page, worker):66 page.goto("http://localhost:8000/")67 page.fill("#title", "Task title")68 page.fill("#dueDate", "2021-10-20")69 page.select_option("#assignedTo", worker)70 page.click("#addTaskBtn")71 new_row = page.query_selector_all("table[id='taskList'] > tbody > tr")[-1]72 assert new_row.query_selector_all("td")[2].inner_text() == worker73@pytest.mark.parametrize("priority", ["Low", "Normal", "Important", "Critical"])74def test_select_priority(page: Page, priority):75 page.goto("http://localhost:8000/")76 page.fill("#title", "Task title")77 page.fill("#dueDate", "2021-10-20")78 page.select_option("#priority", priority)79 page.click("#addTaskBtn")80 new_row = page.query_selector_all("table[id='taskList'] > tbody > tr")[-1]81 assert new_row.query_selector_all("td")[3].inner_text() == priority82 assert priority.lower() in new_row.get_attribute("class")83def test_remove_row(page: Page):84 page.goto("http://localhost:8000/")85 page.fill("#title", "Task title")86 page.fill("#dueDate", "2021-10-20")87 page.click("#addTaskBtn")88 all_rows = page.query_selector_all("table[id='taskList'] > tbody > tr")89 assert len(all_rows) == 190 page.check("table[id='taskList'] > tbody > tr > td > input[type='checkbox']")91 sleep(3)92 all_rows = page.query_selector_all("table[id='taskList'] > tbody > tr")93 assert len(all_rows) == 094if __name__ == "__main__":...

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