Skip to content

Instantly share code, notes, and snippets.

@guinslym
Created March 20, 2024 21:35
Show Gist options
  • Save guinslym/481f7174509823c80eb84994e2125f0d to your computer and use it in GitHub Desktop.
Save guinslym/481f7174509823c80eb84994e2125f0d to your computer and use it in GitHub Desktop.
from playwright.sync_api import Playwright, sync_playwright, expect
def run_site(playwright: Playwright, url: str, link_text: str, use_firefox: bool):
try:
if use_firefox:
# Use Firefox
browser = playwright.firefox.launch(headless=False)
else:
# Use Chromium
browser = playwright.chromium.launch(headless=False)
with browser:
with browser.new_context() as context:
page = context.new_page()
page.goto(url)
# Click on the link with the given text. Adjust the selector as needed.
page.click(f"text={link_text}")
# Wait for navigation or specific condition after click, adjust as needed
page.wait_for_timeout(5000) # Example: wait for 5 seconds
except Exception as e:
print(f"An error occurred: {e}")
# URLs and link texts to visit and click, along with the browser choice
sites = [
(
"https://www.youtube.com/channel/UCR9jARVCi369-HkKTsIiaQw/videos",
"capitalpaintingservices.ca",
True,
),
(
"http://torontobusinessdirectory.xyz/posts/carousel-bakery/",
"Capital Parking lot line Painting CPLS",
False,
),
(
"https://zaubee.com/biz/capital-parking-lot-lines-marking-cpls-xgrdz6qu",
"capitalpaintingservices.ca",
True,
),
(
"https://torontoblogs.ca/parking-lot-lines-companies-in-toronto/",
"capitalpaintingservices.ca",
True,
),
(
"http://torontobusinessdirectory.xyz/posts/dry-cleaners-ca/",
"Capital Parking lot line Painting CPLS",
False,
),
]
with sync_playwright() as playwright:
for url, link_text, use_firefox in sites:
run_site(playwright, url, link_text, use_firefox)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment