diff --git a/python-examples/e-commerce-nested/api/category.py b/python-examples/e-commerce-nested/api/category.py index 00e0dd22..0d915765 100644 --- a/python-examples/e-commerce-nested/api/category.py +++ b/python-examples/e-commerce-nested/api/category.py @@ -30,7 +30,7 @@ async def extract_categories(page: Page, store_url: str) -> list[Category]: # Replace selector with appropriate one for your store's menu menu_selector = ".has-submenu a.main-menu__link" - links = await page.query_selector_all(menu_selector) + links = await page.locator(menu_selector).all() for link in links: href = await link.get_attribute("href") diff --git a/python-examples/e-commerce-nested/api/details.py b/python-examples/e-commerce-nested/api/details.py index a0ae40ad..9205cf40 100644 --- a/python-examples/e-commerce-nested/api/details.py +++ b/python-examples/e-commerce-nested/api/details.py @@ -14,32 +14,28 @@ async def extract_price_info(page: Page) -> dict: # Check for sale price (old price exists) # Replace selector with appropriate one for your store - old_price_element = await page.query_selector("div.prices span.old") + old_price_locator = page.locator("div.prices span.old") - if old_price_element: - price = (await old_price_element.inner_text()).strip() + if await old_price_locator.count() > 0: + price = (await old_price_locator.first.inner_text()).strip() price = price.replace(",", ".") # Get current/sale price - replace selector - sale_element = await page.query_selector( - "div.prices span.value" - ) or await page.query_selector("div.prices span.current") - sale_price = (await sale_element.inner_text()).strip() if sale_element else None + sale_locator = page.locator("div.prices span.value, div.prices span.current") + sale_price = (await sale_locator.first.inner_text()).strip() if await sale_locator.count() > 0 else None sale_price = sale_price.replace(",", ".") if sale_price else None # Get sale offer text - replace selector - sale_offer_element = await page.query_selector("div.prices span.promotion") + sale_offer_locator = page.locator("div.prices span.promotion") sale_offer = ( - (await sale_offer_element.inner_text()).strip() - if sale_offer_element + (await sale_offer_locator.first.inner_text()).strip() + if await sale_offer_locator.count() > 0 else None ) else: # No sale, regular price only - replace selector - price_element = await page.query_selector( - "div.prices span.value" - ) or await page.query_selector("div.prices span.current") - price = (await price_element.inner_text()).strip() if price_element else None + price_locator = page.locator("div.prices span.value, div.prices span.current") + price = (await price_locator.first.inner_text()).strip() if await price_locator.count() > 0 else None price = price.replace(",", ".") if price else None return { @@ -58,13 +54,13 @@ async def extract_sizes(page: Page) -> list[Size]: # Replace selector with appropriate one for your store size_container_selector = "div.size-container > ul > li" - check_sizes_element = await page.query_selector(size_container_selector) + size_locator = page.locator(size_container_selector) - if check_sizes_element: - size_elements = await page.query_selector_all(size_container_selector) + if await size_locator.count() > 0: + size_elements = await size_locator.all() for size_element in size_elements: - size_text = await size_element.inner_text() if size_element else None + size_text = await size_element.inner_text() check_available = await size_element.get_attribute("class") # Check if size is available (not sold out) @@ -91,9 +87,9 @@ async def extract_description(page: Page) -> str | None: Replace selector with appropriate one for your store. """ # Replace selector with appropriate one for your store - description_element = await page.query_selector("ul.content-list") - if description_element: - return await description_element.inner_text() + description_locator = page.locator("ul.content-list") + if await description_locator.count() > 0: + return await description_locator.first.inner_text() return None @@ -103,11 +99,9 @@ async def extract_shipping_and_returns(page: Page) -> str | None: Replace selector with appropriate one for your store. """ # Replace selector with appropriate one for your store - shipping_element = await page.query_selector( - "#accordion-pdp-content-shipping-return div" - ) - if shipping_element: - return await shipping_element.inner_text() + shipping_locator = page.locator("#accordion-pdp-content-shipping-return div") + if await shipping_locator.count() > 0: + return await shipping_locator.first.inner_text() return None @@ -159,9 +153,9 @@ async def automation( await page.wait_for_selector("h1.product-name", timeout=60000) # Extract title - replace selector - title_element = await page.query_selector("h1.product-name") + title_locator = page.locator("h1.product-name") title = ( - await title_element.inner_text() if title_element else params.get("name", "") + await title_locator.first.inner_text() if await title_locator.count() > 0 else params.name ) # Extract price information diff --git a/python-examples/e-commerce-nested/api/list.py b/python-examples/e-commerce-nested/api/list.py index 3e33fc26..f78cf064 100644 --- a/python-examples/e-commerce-nested/api/list.py +++ b/python-examples/e-commerce-nested/api/list.py @@ -12,11 +12,9 @@ async def handle_modal(page: Page) -> None: Replace the selector with the appropriate one for your store. """ try: - modal_btn = await page.query_selector( - "#countrySwitcherModal .btn.btn-primary.dark-theme.full" - ) - if modal_btn: - await modal_btn.click() + modal_btn = page.locator("#countrySwitcherModal .btn.btn-primary.dark-theme.full") + if await modal_btn.count() > 0: + await modal_btn.first.click() except Exception: pass @@ -48,18 +46,18 @@ async def extract_products(page: Page, category_url: str) -> list[Product]: products: list[Product] = [] # Replace ".product-tile__link" with the appropriate selector for your store - product_links = await page.query_selector_all(".product-tile__link") + product_links = await page.locator(".product-tile__link").all() for link in product_links: href = await link.get_attribute("href") # Extract title - replace with appropriate selector - title_element = await link.query_selector(".product-tile__name") - name = await title_element.inner_text() if title_element else "" + title_locator = link.locator(".product-tile__name") + name = await title_locator.inner_text() if await title_locator.count() > 0 else "" # Extract price - replace with appropriate selector - price_element = await link.query_selector(".product-tile__price .current") - price = await price_element.inner_text() if price_element else "" + price_locator = link.locator(".product-tile__price .current") + price = await price_locator.inner_text() if await price_locator.count() > 0 else "" if href: details_url = urljoin(category_url, href) diff --git a/python-examples/hybrid-automation/api/scraper/details.py b/python-examples/hybrid-automation/api/scraper/details.py index f7de082d..5cf44395 100644 --- a/python-examples/hybrid-automation/api/scraper/details.py +++ b/python-examples/hybrid-automation/api/scraper/details.py @@ -75,32 +75,28 @@ async def extract_price_info(page: Page) -> dict: # Check for sale price (old price exists) # Replace selector with appropriate one for your store - old_price_element = await page.query_selector("div.prices span.old") + old_price_locator = page.locator("div.prices span.old") - if old_price_element: - price = (await old_price_element.inner_text()).strip() + if await old_price_locator.count() > 0: + price = (await old_price_locator.first.inner_text()).strip() price = price.replace(",", ".") # Get current/sale price - replace selector - sale_element = await page.query_selector( - "div.prices span.value" - ) or await page.query_selector("div.prices span.current") - sale_price = (await sale_element.inner_text()).strip() if sale_element else None + sale_locator = page.locator("div.prices span.value, div.prices span.current") + sale_price = (await sale_locator.first.inner_text()).strip() if await sale_locator.count() > 0 else None sale_price = sale_price.replace(",", ".") if sale_price else None # Get sale offer text - replace selector - sale_offer_element = await page.query_selector("div.prices span.promotion") + sale_offer_locator = page.locator("div.prices span.promotion") sale_offer = ( - (await sale_offer_element.inner_text()).strip() - if sale_offer_element + (await sale_offer_locator.first.inner_text()).strip() + if await sale_offer_locator.count() > 0 else None ) else: # No sale, regular price only - replace selector - price_element = await page.query_selector( - "div.prices span.value" - ) or await page.query_selector("div.prices span.current") - price = (await price_element.inner_text()).strip() if price_element else None + price_locator = page.locator("div.prices span.value, div.prices span.current") + price = (await price_locator.first.inner_text()).strip() if await price_locator.count() > 0 else None price = price.replace(",", ".") if price else None return { @@ -119,13 +115,13 @@ async def extract_sizes(page: Page) -> list[Size]: # Replace selector with appropriate one for your store size_container_selector = "div.size-container > ul > li" - check_sizes_element = await page.query_selector(size_container_selector) + size_locator = page.locator(size_container_selector) - if check_sizes_element: - size_elements = await page.query_selector_all(size_container_selector) + if await size_locator.count() > 0: + size_elements = await size_locator.all() for size_element in size_elements: - size_text = await size_element.inner_text() if size_element else None + size_text = await size_element.inner_text() check_available = await size_element.get_attribute("class") # Check if size is available (not sold out) @@ -152,9 +148,9 @@ async def extract_description(page: Page) -> str | None: Replace selector with appropriate one for your store. """ # Replace selector with appropriate one for your store - description_element = await page.query_selector("ul.content-list") - if description_element: - return await description_element.inner_text() + description_locator = page.locator("ul.content-list") + if await description_locator.count() > 0: + return await description_locator.first.inner_text() return None @@ -270,9 +266,9 @@ async def automation( await page.wait_for_selector("h1.product-name") # Extract title - replace selector - title_element = await page.query_selector("h1.product-name") + title_locator = page.locator("h1.product-name") title = ( - await title_element.inner_text() if title_element else params.get("name", "") + await title_locator.first.inner_text() if await title_locator.count() > 0 else params.name ) # Extract price information diff --git a/python-examples/hybrid-automation/api/scraper/list.py b/python-examples/hybrid-automation/api/scraper/list.py index 6b48abec..3f0521f0 100644 --- a/python-examples/hybrid-automation/api/scraper/list.py +++ b/python-examples/hybrid-automation/api/scraper/list.py @@ -23,11 +23,9 @@ async def handle_modal(page: Page) -> None: Replace the selector with the appropriate one for your store. """ try: - modal_btn = await page.query_selector( - "#countrySwitcherModal .btn.btn-primary.dark-theme.full" - ) - if modal_btn: - await modal_btn.click() + modal_btn = page.locator("#countrySwitcherModal .btn.btn-primary.dark-theme.full") + if await modal_btn.count() > 0: + await modal_btn.first.click() except Exception: pass @@ -59,18 +57,18 @@ async def extract_products(page: Page, category_url: str) -> list[Product]: products: list[Product] = [] # Replace ".product-tile__link" with the appropriate selector for your store - product_links = await page.query_selector_all(".product-tile__link") + product_links = await page.locator(".product-tile__link").all() for link in product_links: href = await link.get_attribute("href") # Extract title - replace with appropriate selector - title_element = await link.query_selector(".product-tile__name") - name = await title_element.inner_text() if title_element else "" + title_locator = link.locator(".product-tile__name") + name = await title_locator.inner_text() if await title_locator.count() > 0 else "" # Extract price - replace with appropriate selector - price_element = await link.query_selector(".product-tile__price .current") - price = await price_element.inner_text() if price_element else "" + price_locator = link.locator(".product-tile__price .current") + price = await price_locator.inner_text() if await price_locator.count() > 0 else "" if href: details_url = urljoin(category_url, href) diff --git a/python-examples/network-interception/api/api-interceptor.py b/python-examples/network-interception/api/api-interceptor.py index aef3bf34..6d6c61b2 100644 --- a/python-examples/network-interception/api/api-interceptor.py +++ b/python-examples/network-interception/api/api-interceptor.py @@ -87,9 +87,9 @@ async def automation( while current_page < max_pages: # Check if next button exists and is visible # Replace "#next-page-btn" with the appropriate selector for your store - next_button = await page.query_selector("#next-page-btn") + next_button = page.locator("#next-page-btn") - if not next_button: + if await next_button.count() == 0: print("Next button not found, stopping pagination") break diff --git a/python-examples/scrapy/api/scrapy-crawler-js.py b/python-examples/scrapy/api/scrapy-crawler-js.py index 350083ac..1e8f248f 100644 --- a/python-examples/scrapy/api/scrapy-crawler-js.py +++ b/python-examples/scrapy/api/scrapy-crawler-js.py @@ -24,13 +24,11 @@ def parse(self, response): async def is_next_page_available(page: Page) -> bool: - next_link = await page.query_selector("li.next a") - return next_link is not None + return await page.locator("li.next a").count() > 0 async def go_to_next_page(page: Page) -> None: - next_link = await page.query_selector("li.next a") - await next_link.click() + await page.locator("li.next a").click() await page.wait_for_load_state("networkidle")