try:
async with setup_browser(n=0, stealth=False) as obj:
if obj.is_valid:
...except ValueError as e:
assert str(e) == "Invalid number of pages: 0"
core
Page
and Locators
retry_async
retry_async (max_retries:int=3, backoff_base:float=1.5)
Decorator for retrying an asynchronous function upon exception. Retries the execution of an async function a specified number of times (max_retries
) with an exponential backoff delay between attempts (backoff_base
). If all attempts fail, the exception from the last attempt is raised.
setup_browser
Setup browser by opening up chrome and all the other objects
setup_browser
setup_browser (*args, n:int=1, stealth:bool=False, **kwargs)
Browser context manager returns n pages and stealth for mode
BrowserCleanupError
Common base class for all non-exit exceptions.
PageCreationError
Common base class for all non-exit exceptions.
BrowserResources
BrowserResources (pw_obj:Optional[playwright.async_api._generated.Playwr ight]=None, brow:Optional[playwright.async_api._generat ed.Browser]=None, ctx:Optional[playwright.async_api._ge nerated.BrowserContext]=None, pages:List[playwright.async_api._generated.Page]=None, is_valid:bool=False)
async with setup_browser(n=1, stealth=False) as obj:
if obj.is_valid:
= await obj.pages[0].goto('http://example.org')
pg assert pg.status == 200
Page’s Monkey patching
wait_page
wait_page (page:playwright.async_api._generated.Page, pause=50, timeout=5000)
moneky patching Page.wait_page
. Wait until page and frames are ready to be loaded
async with setup_browser(n=1, headless=True) as obj:
if obj.is_valid:
= await obj.pages[0].goto('http://example.org')
pg await obj.pages[0].wait()
assert pg.status == 200
find_ele
find_ele (page:playwright.async_api._generated.Page, locator:str)
To locate elements on a web page using a given locator and return a list of those elements. Logs error if the locator
object is not present and returns None
.
async with setup_browser(n=1, headless=True) as obj:
if obj.is_valid:
await obj.pages[0].goto('https://nbdev.fast.ai/')
await obj.pages[0].wait()
assert len(await obj.pages[0].find_ele("//span[contains(text(), 'Blog')]") ) != 0
assert await obj.pages[0].find_ele("//span[contains(text(), 'blah')]") is None
Error find : @https://nbdev.fast.ai/ for //span[contains(text(), 'blah')] :-> Element not found.
find_all_links
find_all_links (page:playwright.async_api._generated.Page)
async with setup_browser(n=1, headless=True) as obj:
if obj.is_valid:
= obj.pages[0]
pg await pg.goto('https://nbdev.fast.ai/',timeout= 10000)
await pg.wait(10000)
= await find_all_links(obj.pages[0])
links assert type(links) == list
Locator’s Monkey patching
left_click
left_click (element:playwright.async_api._generated.Locator, timeout=5000)
for a given element it performs single click operation
= 'https://solveit.fast.ai/'
url async with setup_browser(n=1, headless=False) as obj:
if obj.is_valid:
await obj.pages[0].goto(url)
await obj.pages[0].wait()
= await obj.pages[0].find_ele("//a[contains(text(), 'Course Details')]")
loc assert len(loc) != 0
await loc[0].left_click()
assert url != obj.pages[0].url
enter_txt
enter_txt (element:playwright.async_api._generated.Locator, text:str, timeout:int=5000)
For a given element of type Locator
, it types the specified text
async with setup_browser(n=1, headless=True) as obj:
if obj.is_valid:
await obj.pages[0].goto('https://nbdev.fast.ai/')
await obj.pages[0].wait()
= await obj.pages[0].find_ele("//button[1]")
loc assert len(loc) != 0
await loc[0].left_click()
await obj.pages[0].wait()
= await obj.pages[0].find_ele("//input")
inp await inp[0].left_click()
await inp[0].enter_txt("type_text")
assert await inp[0].input_value() == "type_text"
get_text
get_text (element:playwright.async_api._generated.Locator, timeout:int=5000)
Gets the text content of an Locator
element
async with setup_browser(n=1, headless=True) as obj:
if obj.is_valid:
await obj.pages[0].goto('https://nbdev.fast.ai/')
await obj.pages[0].wait()
= await obj.pages[0].find_ele('//a[@role="button"][1]')
loc assert len(loc) != 0
assert await loc[0].get_text() == "Get started"