Playwright >> Tips and Tricks (1)
Table of Contents
This tutorial will explain how to use Playwright to get all cookies, get all links in the page, get all links containing specific string/text, click buttons, click links, click all links in new tabs (click multiple elements), get all pages/tabs, get all attributes of element, customize click timeout, click element if it exists, click specific position, click and drag (drag and drop).
Playwright get all cookies
We can use the cookies() method of BrowserContext to get cookies. if no parameter is specified, all cookies will be returned.
# Get all cookies
all_cookies = browser_context.cookies()
Playwright get all links
We can use the locator() method of page object and specify ‘a’ expression to get all the links of the page.
found_elements = page.locator('a')
print(f"{found_elements.count()} link/links are found.")
Playwright get all links containing specific string/text
We can use the locator() method of page object and specify ‘a:has-text()' expression to get all the links of the page.
found_elements = page.locator('a:has-text("playwright")')
print(f"{found_elements.count()} link/links are found.")
Playwright click button
We can locate the button element first and use the click() method to click it.
Solution 1
element = page.locator('input[name="mybutton"]')
element.click()
Solution 2
element = page.click('input[name="mybutton"]')
Playwright click link
We can locate the link element first and use the click() method to click it.
Solution 1
element = page.locator('a:has-text("playwright")').first
element.click()
Solution 2
page.click('a:has-text("playwright") >> nth=0')
Playwright click all links in new tabs (click multiple elements)
We can locate all the link elements statisfying specific condition first and loop through them and use the click() method to click them.
We can use the modifier keys parameters to open the links in new tabs.
elements = page.locator('a:has-text("playwright")')
count = elements.count()
for i in range(count):
elements.nth(i).click(modifiers=["Control", "Shift"])
Playwright get all pages/tabs
We can use the pages property of BrowserContext to get all opened pages.
## Get all opened pages/tabs
opened_pages = context.pages
# Loop through all the pages/tabs
for p in opened_pages:
print(p.url)
Playwright get all attributes of element
We can use the evaluate() method of locator object to execute Javascript expression to get all the attribute names of the element first. And loop through them and use the get_attribute() method to get the value of each attribute.
# elements = page.locator('a:has-text("playwright")')
element = page.locator('img').first
# Get all attribute names by using JavaScript expression evaluated by evaluate() method
el_attrs = element.evaluate("el => el.getAttributeNames()")
# Loop through all attribute names and get the attribute value
for attr in el_attrs:
print(attr, ":", element.get_attribute(attr))
Playwright click timeout
We can use the timeout paramter to customize the waiting time when click the element.
# Unit is millisecond, 60000 means 60 seconds, default is 30000 (30 seconds)
page.click("#myButton", timeout=60000)
Playwright click element if it exists
We can firstly find all the elments satisfying specific condition and use the count() method to get the number of elements.
If the number of elements is greater than 0, we can use the first() method to get the first element and use the click() method to click it.
# Get the links containing text "playwright"
elements = page.locator('a:has-text("playwright")')
# Get the found number
link_number = elements.count()
# If the number is greater than 0, click the first link
if link_number > 0:
elements.first.click()
Playwright click specific position
We can use the mouse property of the page object to click the element at specific position.
# page.mouse.click(x, y)
page.mouse.click(250, 110)
Playwright click and drag (drag and drop)
We can use the mouse property of the page object to drag and drop the element.
We can use the down() method of mouse property of page object to click the element and use the move() method to drag the element and use the up() method to drop the element.
page.mouse.move(100, 100)
page.mouse.down()
page.mouse.move(200, 200)
page.mouse.up()