Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python-examples/e-commerce-nested/api/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
50 changes: 22 additions & 28 deletions python-examples/e-commerce-nested/api/details.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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


Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down
18 changes: 8 additions & 10 deletions python-examples/e-commerce-nested/api/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
42 changes: 19 additions & 23 deletions python-examples/hybrid-automation/api/scraper/details.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down
18 changes: 8 additions & 10 deletions python-examples/hybrid-automation/api/scraper/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions python-examples/network-interception/api/api-interceptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 2 additions & 4 deletions python-examples/scrapy/api/scrapy-crawler-js.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down
Loading