Home > Mobile >  How do I click on "Next" button until it disappears in playwright (python)
How do I click on "Next" button until it disappears in playwright (python)

Time:12-25

Here is the code I am using to click next button the problem is after the first page is loded it closes the browser rather than clicking on the next button until it disappears. (I know it is html website but I am learning Playwright so starting light.)

I am using get_by_text() function, I have used this loop to achieve similar results but with selenium python.

Any suggestion how to make this happen?

with sync_playwright() as p:
        browser = p.firefox.launch(headless=False)
        page = browser.new_page()
        page.goto("https://books.toscrape.com/")

while True:
try: 
        next = page.get_by_text("Next")     ## next clicker
        next.click()
except:
    break

CodePudding user response:

Maybe if you put a break in each loop:

from playwright.sync_api import sync_playwright
from time import sleep

with sync_playwright() as p:
    browser = p.firefox.launch(headless=False)
    page = browser.new_page()
    page.goto("https://books.toscrape.com/")

    while True:
        try: 
            next = page.get_by_text("Next")  # next clicker
            next.click()
            sleep(2)
        except Exception:
            break
  • Related