How to use query_selector_all method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

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

gamls.py

Source: gamls.py Github

copy
1import pprint
2import time
3import sqlite3
4import requests
5from selenium import webdriver
6from selenium.webdriver.support.ui import Select
7from selenium.webdriver.common.proxy import Proxy, ProxyType
8
9'''headers = {
10    'Referer': 'https://www.gamls.com/',
11    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
12}
13payload = 'username=OLIVERCRYSTE&password=DreamTeam18!&sendLogin='
14s = requests.session()
15s.get("https://www.gamls.com")
16resp = s.post("https://www.gamls.com/login/login", data=payload, headers=headers)
17html = resp.text
18f = open("login.html", "w")
19f.write(html)
20f.close()
21'''
22
23WAIT = 5
24driver = webdriver.Chrome()
25driver.maximize_window()
26driver.get("https://www.gamls.com")
27time.sleep(WAIT)
28driver.find_element_by_id('username2').send_keys("OLIVERCRYSTE")
29driver.find_element_by_id('Password2').send_keys("DreamTeam18!")
30driver.find_element_by_id('sendLoginSm').click()
31time.sleep(WAIT*2)
32
33
34
35'''with sync_playwright() as p:
36    for browser_type in [p.chromium]:
37        browser = browser_type.launch(headless=False)
38        page = browser.new_page()
39        page.set_default_timeout(120000)
40        page.goto('https://www.gamls.com/secure/default')
41        #input("solve captcha and enter to continue")
42        page.fill("#username", "OLIVERCRYSTE")
43        page.fill("#password", "DreamTeam18!")
44        page.click("#sendLoginLg")
45        time.sleep(WAIT*2)
46        page = 1
47        page.goto(f"https://www.gamls.com/secure/membership/index.cfm?Typ=Member&Fn=Name&Page={page}")
48        for tr in page.query_selector_all("tr"):
49            name = tr.query_selector_all("td")[0].inner_text()
50            company = tr.query_selector_all("td")[6].inner_text()
51            office = tr.query_selector_all("td")[7].inner_text()
52            cell = tr.query_selector_all("td")[8].inner_text()
53            email = tr.query_selector_all("td")[9].inner_text()
54            print(name,company,office,cell,email)
55
56
57'''
Full Screen

test_honey_integration.py

Source: test_honey_integration.py Github

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

scripts.py

Source: scripts.py Github

copy
1import time
2from random import randint
3from pprint import pprint
4from typing import Dict, Any
5
6from playwright.sync_api import sync_playwright
7
8from locators import ProductPageLocators, SiteMapLocators
9from loggers import *
10
11
12def sleep(secs: int = randint(1, 5)) -> None:
13	producer_logger.info(f"Snoozing {secs} seconds")
14	time.sleep(secs)
15
16
17def get_text(element: object) -> str:
18	# a helper function - sift, strip text_content() of page element
19	return element.text_content().strip() if element else ''
20
21
22def get_sub_categories(categories: Dict, site_map_category: object) -> None:
23	''' Sub-categories - Giyim, Aksesuarlar '''
24	title = get_text(site_map_category.query_selector(SiteMapLocators.title))
25	categories[title] = dict()
26	producer_logger.debug(f"Grabbing sub-categories for Category -> {title}")
27	# grab sub-categories under title/categories 
28	site_map_sections = site_map_category.query_selector_all(SiteMapLocators.site_map_sections)
29
30	for site_map_section in site_map_sections:
31		# grab the text for the sub-categories
32		site_map_item_title = site_map_section.query_selector(SiteMapLocators.site_map_item_title)
33		site_map_item_title = get_text(site_map_item_title)
34			
35		# grab all hrefs of items under each sub-categories
36		site_map_items = site_map_section.query_selector_all(SiteMapLocators.site_map_items)[1:]
37		site_map_items_dict = dict()
38		
39		for site_map_item in site_map_items:
40			site_map_items_dict[get_text(site_map_item)] = site_map_item.get_attribute('href')
41		
42		# add the dict holding the sub-categories and their hrefs
43		categories[title][site_map_item_title] = site_map_items_dict
44
45
46def get_categories() -> Dict:
47	''' Categories - Kadin, Erkek, Teen, Cocuk '''
48	categories = dict()
49	producer_logger.debug("Getting SiteMap Categories")
50	# site map category elements
51	site_map_categories = page.query_selector_all(SiteMapLocators.site_map_categories)
52	producer_logger.debug("SiteMap Categories Found")
53	producer_logger.debug("Traversing SiteMap Categories")
54	for site_map_category in site_map_categories:
55		get_sub_categories(categories, site_map_category)
56	return categories
57
58
59def traverse_sitemap() -> None:
60	sitemap_url: str = f"{base_url}/tr/sitemap"
61	# traverse the sitemap url
62	producer_logger.info(f"Going to {sitemap_url}")
63	page.goto(sitemap_url)
64	categories = get_categories()
65	return categories
66
67
68def get_products_href(count: int = 0) -> None:
69	# while True:
70	products = page.query_selector_all('li[id*="product-key-id"]')
71	current_number_of_products = len(products)
72	# if count < current_number_of_products:
73	for product in products[count:10]:
74		product.scroll_into_view_if_needed()
75		href = product.query_selector('a').get_attribute('href')
76		products_hrefs.add(href)
77		producer_logger.debug(f"Found -> {href}")
78		count += 1
79	# else:
80	# 	break
81	# wait some seconds for product list to update
82	sleep()
83
84
85def set_category(category: str, product: Dict) -> None:
86	producer_logger.debug(category)
87	product['Category'] = category
88
89
90def set_sub_category(subcategory: str, product: Dict) -> None:
91	producer_logger.debug(subcategory)
92	product['Sub Category'] = subcategory
93
94
95def get_name(product: Dict) -> None:
96	# product name
97	product_name = get_text(page.wait_for_selector(ProductPageLocators.product_name))
98	producer_logger.debug(product_name)
99	product['Name'] = product_name
100
101
102def join_collection(collection: Any) -> str:
103	''' helper function '''
104	if isinstance(collection, dict):
105		return '\n\n'.join([f"{key}:{collection.get(key)}" for key in collection ])
106	elif isinstance(collection, list):
107		return '\n\n'.join(collection)
108
109
110def get_description(product: Dict) -> None:
111	# description
112	product_info_block = page.query_selector_all('div.product-info-block')
113	description = dict()
114	for block in product_info_block:
115		product_info_title = get_text(block.query_selector('h2.product-info-title')) 
116		product_info_text = get_text(block.query_selector('p.product-info-text'))  
117		description[product_info_title] = product_info_text
118	producer_logger.debug(description)
119	product['Description'] = join_collection(description)
120
121
122def get_colors(product: Dict) -> None:
123	# color option
124	product_colors = page.query_selector(ProductPageLocators.product_colors)
125	colors_info = product_colors.query_selector_all(ProductPageLocators.colors_info)
126	colors_img = product_colors.query_selector_all(ProductPageLocators.colors_img)
127	color_option = { get_text(key): value.get_attribute('src') for key, value in zip(colors_info, colors_img) } 
128	producer_logger.debug(color_option)
129	product['Color'] = join_collection(color_option)
130
131
132def get_size(product: Dict) -> None:
133	# size option
134	size_options = page.query_selector_all(ProductPageLocators.size_options)
135	# size_option = [ {get_text(size): size.get_attribute('data-value')} for size in size_options ]
136	size_option = [ f"{get_text(size)}:{size.get_attribute('data-value')}" for size in size_options ]
137	producer_logger.debug(size_option)
138	product['Size'] = join_collection(size_option)
139
140
141def get_prices(product: Dict) -> None:
142	product_prices = page.query_selector(ProductPageLocators.product_prices)
143	
144	sale_price = get_text(product_prices.query_selector(ProductPageLocators.product_sale))
145	product['Sale Price'] = sale_price
146	sale_percentage = get_text(product_prices.query_selector('span.product-discount')) 
147	product['Sale Percentage'] = sale_percentage
148	original_price = get_text(product_prices.query_selector('span.product-sale--cross'))
149	product['Original Price'] = original_price
150
151
152def get_sku(product: Dict) -> None:
153	# product SKU
154	product_sku = get_text(page.query_selector(ProductPageLocators.product_sku))
155	producer_logger.debug(product_sku) 
156	product['SKU'] = product_sku
157
158
159def get_seo(product: Dict) -> None:
160	# SEO
161	seo = page.query_selector(ProductPageLocators.seo).get_attribute('content').strip()
162	producer_logger.debug(seo)
163	product['SEO'] = seo
164
165
166def get_images(product: Dict) -> None:
167	# product images
168	images = page.query_selector_all(ProductPageLocators.images)
169	product_images = [ f"https:{image.get_attribute('src')}" for image in images ]
170	producer_logger.debug(product_images)
171	product['Images'] = join_collection(product_images)
172
173
174def get_tags(product: Dict) -> None:
175	# product tags
176	tag_elements = page.query_selector_all(ProductPageLocators.tag_elements)
177	product_tags = [ get_text(tag) for tag in tag_elements ] 
178	producer_logger.debug(product_tags)
179	product["Tags"] = join_collection(product_tags)
180
181
182def get_product(url: str) -> None:
183	product = dict()
184	product_url = f"{base_url}{url}"
185	page.goto(product_url)
186
187	product['Product URL'] = product_url
188	set_category(category, product)
189	set_sub_category(sub_category, product) 
190	sleep()
191	get_name(product); get_description(product)
192	get_colors(product); get_size(product)
193	get_prices(product); get_sku(product)
194	get_seo(product); get_images(product)
195	get_tags(product); pprint(product)
196
197
198base_url = "https://shop.mango.com"
199
200timeout = 45 * 1000
201
202playwright = sync_playwright().start() 
203browser = playwright.chromium.launch(headless=False)
204context = browser.new_context()
205context.set_default_timeout(timeout)
206context.set_default_navigation_timeout(timeout)
207page = context.new_page()
208
209categories = traverse_sitemap()
210
211category = 'Kadin'
212kadin = categories[category]
213
214sub_category = 'Giyim'
215giyim = kadin[sub_category]
216sub_sub_cat = giyim['Elbise ve tulum']
217
218sub_sub_cat_page = f"{base_url}{sub_sub_cat}"
219page.goto(sub_sub_cat_page)
220page.wait_for_selector('li[id*="product-key-id"]')
221
222products_hrefs = set()
223get_products_href()
224
225for url in products_hrefs:
226	get_product(url)
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)