Playwright basics

Playwright automation testing framework.

playwright with web scraper and Development|lower


PLAYWRIGHT GETSTRATED

1. Install. $ pip install playwright

2. Install browsers $ playwright install (chromium, firefox, webkit). Actually better to skip this step it is possible to use native browser with command browser = playwright.chromium.launch(channel="chromium")

3. Basic start:

from playwright.sync_api import sync_playwright

with sync_playwright() as pet:

bird = pet.chromium.launch()

marcus = bird.new_page()

marcus.goto('https://outleys.site')

marcus.close()

4. To get html element: sky = marcus.query_selector(" .winter-class")

4.1. But best practice is to use locators marcus.get_by_role and marcus.get_by_text

4.2. And use function for waiting expect(marcus.get_by_text("Wait until element is ready to use", exact=True)).to_be_visible()

4.3. If you want xpath marcus.locator("xpath=//button").click()

4.4. Or marcus.locator("div", has_text="Sky is blue" ).click()

4.5. To get specific item in collection barana = page.get_by_role("listitem").nth(1)

5. To get multiple elements: sky = marcus.query_selector_all(" .winter-class") or marcus.get_by_role("listitem").all()

6. To wait for element loading and get it pg.wait_for_selector("a[id='name']", timeout=120000) it is way more better than Selenium.

7. To work from console:

>>> from playwright.sync_api import sync_playwright

>>> play = sync_playwright().start()

8. To select an element by text elephant = pg.get_by_text("Text on the screen")

9. Make right click elephant.click(button="right")

10. To make click with CTRL button: elephant.click(modifiers=["Control"])

11. Fancy staff. To automatically get locator type from command line: $ playwright codegen outleys.site

12. Use chromium.launch(headless=False, slow_mo=100) to make delays between actions

13. To get descendant locator marcus.get_by_role("listitem").filter(has_text="Some specific locator").get_by_role("button", name="Name of descendant")

14. To set listeners on the event marcus.on("response", handle_response)

15. To get text locator.inner_text() or locator.inner_html()

16. To set fullscreen:

b = p.chromium.launch(args=['--start-maximized'], headless=False)  

pg = b.new_page(no_viewport=True)

or page.set_viewport_size({"width": 666, "height": 666})

17. To switch between tabs use: context.pages[1].bring_to_front()

18. How to check if locator existing // element.count()

19. Have a problem. I have to change tab but page is not loadad into context. I mean page actualy is loaded but without "load" event so Playwright context consider the page as unloaded. If i run context.page[1] I get an error.

19.1. How to ignore "load" event.

19.1.1. Try to remove listner context.removeListener('load') // Context doesn't have such attribute.

19.1.2. Increase slow_mo to 2000 // doesn't helped

19.1.3. Try to change to native browser b = p.chromium.launch(channel="chromium")

19.1.4. Maybe I just going to use urls to start new tab. But how to get hrefs with playwright? // Similar with Selenium elem.get_attribute("href") // Doesn't helped

19.1.5. Try to change browser for Firefox b = p.firefox.launch()

19.1.6. To change tabs use keyboard Ctrl+tab p.keyboard.press("Control+Tab")

20. To modify request use ROUTE. Set router on all requests: page.route("**/*", handle_route). And set handler function:

def handle_route(route):

headers = route.request.headers

headers["cache-control"] = "max-age=777"

20.1. Set Cache-Control in request doesnt help. I still get no-cache response.

20.2. Try to set pragma request parameter.

21. How to enter text into an input element. // input_element.fill("some text")

22. How to get nested elements // elements = page.locator("xpath=//div[@id='something']/h1")

23. How to open new tab. // If you open new page from the same context it is a new tab infact despite it looks like new window.

24. How to check if element is visible. 

25. Problem. Context crashed. playwright._impl._api_types.Error: Execution context was destroyed, most likely because of a navigation. // Set delay after input request to get time for results to appear.

26. How to get url // pg.url

27. Is it possible to save browser's cookies for next sessions. // Just try do not use browser.new_context() Doesn't helped

27.1 Try browser_context.storage_state()  // Working. I could copy cookies, history and files in storage

stor = cxt.storage_state(path="state.json") # Save storage state into the file.

cxt = browser.new_context(storage_state="state.json") # Create a new context with the saved storage state.

27.2 It is possible also to save HTTP session and emulate HTTP headers.

27.3. Try to connect existing Chromium over connect_over_cdp // I got Error: connect ECONREFUSED 127.0.0.1:9222

br = playwright.chromium.connect_over_cdp("http://localhost:9222")

def_cxt = br.contexts[0]

pg = def_cxt.pages[0]

27.3.1. Start chromium from command line with $ chromium --remote-debugging-port=9222 // Working

27.4 Or try to use browser_type.launch_persistent_context(user_data_dir)

28. How to get attribute value