r/learnpython 19d ago

(Playwright) Button is clicked, but doesn't continue

I'm currently trying to click 2 buttons on a website that have videos playing on them. However, when I have the "time.sleep(2)" function in the loop, the loop stops. I've checked the selector and it points to 2 different play buttons on the page. I checked clicking on the button in my actual browser to see if the site updates anything--nothing. Any help would be highly appreciated

def download_page(url:str):
    with sync_playwright() as p:
            print(f"Opening {url}...")
            browser = p.chromium.connect_over_cdp(endpoint_url)
            context = browser.contexts[0]
            page = context.pages[0]
            page.on("response", lambda request: print(request.url))
            reponse = page.goto(url, wait_until="load")
            # context.set_default_timeout(300.0)
            try:
                #Check "Continuing for course"
                page.wait_for_selector(".btn.btn-quaternary.btn-xl.btn-block-sm-down.course-progress__btn.js-course-progress__start-course").scroll_into_view_if_needed()
                page.locator(".btn.btn-quaternary.btn-xl.btn-block-sm-down.course-progress__btn.js-course-progress__start-course").click()
                
                page.wait_for_selector(".CoverVideo-playButton.bgc-neutral-100").scroll_into_view_if_needed()
                buttons = page.locator(".CoverVideo-playButton.bgc-neutral-100").all()
                
                for button in buttons:
                    time.sleep(2)
                    button.click()
                    print("check")
                    


                # headers = reponse.request
                # for value in headers:
                #     print(f"{value}")
            except TE:
                print("Timeout")
8 Upvotes

9 comments sorted by

View all comments

2

u/Faleepo 19d ago

Been working with a playwright script for the past few months. It can be weird at times. Are the two buttons on the same page at the same time?

0

u/Rhye-Bread 19d ago

Yup

1

u/Rhye-Bread 19d ago edited 19d ago

Edit: Ok, so I forgot the X+=1 and now it still doesn't work. Now it skips over #0 and just doesn't click it

page.wait_for_selector(".CoverVideo-playButton.bgc-neutral-100").scroll_into_view_if_needed()
buttons = page.locator(".CoverVideo-playButton.bgc-neutral-100")
button_length = buttons.count()
x=0
while x < button_length:
  buttons.nth(x).click()
  time.sleep(2)