How to use set_default_navigation_timeout method in Playwright Python

Best Python code snippet using playwright-python

_interaction.py

Source:_interaction.py Github

copy

Full Screen

...328 返回主要资源响应。329 在多次重定向的情况下,导航将使用上次重定向的响应进行解析。330 如果不能返回,则返回 null。331 :param timeout:最大操作时间以毫秒为单位,默认为 30 秒,传递 0 以禁用超时。332 可以使用 browser_context.set_default_navigation_timeout(timeout)、333 browser_context.set_default_timeout(timeout)、334 page.set_default_navigation_timeout(timeout)335 或 page.set_default_timeout(timeout) 方法更改默认值。336 :param wait_until: <"load"|"domcontentloaded"|"networkidle"> 当认为操作成功时,默认加载。 事件可以是:#337 'domcontentloaded' - 当 DOMContentLoaded 事件被触发时,认为操作完成。338 'load' - 当加载事件被触发时,认为操作已经完成。339 'networkidle' - 当至少 500 毫秒没有网络连接时,认为操作完成。340 """341 if type(self._obj).__name__ == "Page":342 return self._obj.go_back(343 timeout=timeout,344 wait_until=wait_until,345 )346 elif type(self._obj).__name__ == "Frame":347 return self._obj.page.go_back(348 timeout=timeout,349 wait_until=wait_until,350 )351 else:352 raise TypeError(f"{self._obj}的类型应当是 Page 类型或 Frame 类型。")353 def go_forward(354 self,355 timeout: float = None,356 wait_until: Literal["domcontentloaded", "load", "networkidle"] = None357 ):358 """导航到历史记录的下一页。359 返回主要资源响应。360 在多次重定向的情况下,导航将使用上次重定向的响应进行解析。361 如果不能返回,则返回 null。362 :param timeout:最大操作时间以毫秒为单位,默认为 30 秒,传递 0 以禁用超时。363 可以使用 browser_context.set_default_navigation_timeout(timeout)、364 browser_context.set_default_timeout(timeout)、365 page.set_default_navigation_timeout(timeout)366 或 page.set_default_timeout(timeout) 方法更改默认值。367 :param wait_until: <"load"|"domcontentloaded"|"networkidle"> 当认为操作成功时,默认加载。 事件可以是:#368 'domcontentloaded' - 当 DOMContentLoaded 事件被触发时,认为操作完成。369 'load' - 当加载事件被触发时,认为操作已经完成。370 'networkidle' - 当至少 500 毫秒没有网络连接时,认为操作完成。371 """372 if type(self._obj).__name__ == "Page":373 return self._obj.go_forward(374 timeout=timeout,375 wait_until=wait_until,376 )377 elif type(self._obj).__name__ == "Frame":378 return self._obj.page.go_forward(379 timeout=timeout,380 wait_until=wait_until,381 )382 else:383 raise TypeError(f"{self._obj}的类型应当是 Page 类型或 Frame 类型。")384 def goto(385 self,386 url: str,387 *,388 timeout: float = None,389 wait_until: Literal["domcontentloaded", "load", "networkidle"] = None,390 referer: str = None391 ):392 """导航到 `url`393 返回主要资源响应。 在多次重定向的情况下,导航将使用上次重定向的响应进行解析。394 如果出现以下情况,该方法将抛出错误:395 存在 SSL 错误(例如,在自签名证书的情况下)。396 目标网址无效。397 导航期间超时。398 远程服务器没有响应或无法访问。399 主资源加载失败。400 当远程服务器返回任何有效的 HTTP 状态代码时,该方法不会抛出错误,包括 404“未找到”和 500“内部服务器错误”。401 可以通过调用 response.status 来检索此类响应的状态代码。402 注意:403 该方法要么抛出错误,要么返回主资源响应。 唯一的例外是导航到 about:blank 或导航到具有不同散列的相同 URL,这将成功并返回 null。404 :param url:页面导航到的 URL。 网址应包括方案,例如 https://。405 当通过上下文选项提供 base_url 并且传递的 URL 是路径时,它会通过新的 URL() 构造函数合并。406 :param timeout: 最大操作时间以毫秒为单位,默认为 30 秒,传递 0 以禁用超时。407 可以使用 browser_context.set_default_navigation_timeout(timeout)、408 browser_context.set_default_timeout(timeout)、409 page.set_default_navigation_timeout(timeout)410 或 page.set_default_timeout(timeout) 方法更改默认值。411 :param wait_until: <"load"|"domcontentloaded"|"networkidle"> 当认为操作成功时,默认加载。 事件可以是:#412 'domcontentloaded' - 当 DOMContentLoaded 事件被触发时,认为操作完成。413 'load' - 当加载事件被触发时,认为操作已经完成。414 'networkidle' - 当至少 500 毫秒没有网络连接时,认为操作完成。415 :param referer: 引用标头值。 如果提供,它将优先于 page.set_extra_http_headers(headers) 设置的引用标头值.416 """417 return self._obj.goto(418 url=url,419 timeout=timeout,420 wait_until=wait_until,421 referer=referer,422 )423 def hover(424 self,425 selector: str,426 timeout: float = None,427 modifiers: Optional[428 List[Literal["Alt", "Control", "Meta", "Shift"]]429 ] = None,430 position: Position = None,431 ):432 """鼠标悬停433 此方法通过执行以下步骤将鼠标悬停在元素上:434 等待对元素的可操作性检查。435 如果需要,将元素滚动到视图中。436 使用 page.mouse 将鼠标悬停在元素的中心或指定位置上。437 等待启动的导航成功或失败438 :param position: 相对于元素填充框的左上角使用的点。 如果未指定,则使用元素的一些可见点。439 :param modifiers: 要按下的修饰键。 确保在操作期间仅按下这些修饰符,然后将当前修饰符恢复回来。 如果未指定,则使用当前按下的修饰符440 :param selector: 用于搜索元素的选择器。 如果有多个元素满足选择器,将使用第一个。441 :param timeout: 最大操作时间以毫秒为单位,默认为 30 秒,传递 0 以禁用超时。442 可以使用 browser_context.set_default_navigation_timeout(timeout)、443 browser_context.set_default_timeout(timeout)、444 page.set_default_navigation_timeout(timeout)445 或 page.set_default_timeout(timeout) 方法更改默认值。446 """447 element = self._find_element_cross_frame(selector=selector)448 element.hover(449 modifiers=modifiers,450 timeout=timeout,451 position=position,452 )453 def inner_html(self, selector: str) -> str:454 """元素的 innerHTML 值。455 :param selector: 用于搜索元素的选择器。 如果有多个元素满足选择器,将使用第一个。456 """457 element = self._find_element_cross_frame(selector)458 return element.inner_html()459 def inner_text(self, selector: str) -> str:460 """元素的 innerText 值。461 :param selector: 用于搜索元素的选择器。 如果有多个元素满足选择器,将使用第一个。462 """463 element = self._find_element_cross_frame(selector)464 return element.inner_text()465 def input_value(self, selector: str, timeout: float = None) -> str:466 """元素的 value 属性的值。467 :param selector: 用于搜索元素的选择器。 如果有多个元素满足选择器,将使用第一个。468 :param timeout: 最大操作时间以毫秒为单位,默认为 30 秒,传递 0 以禁用超时。469 可以使用 browser_context.set_default_navigation_timeout(timeout)、470 browser_context.set_default_timeout(timeout)、471 page.set_default_navigation_timeout(timeout)472 或 page.set_default_timeout(timeout) 方法更改默认值473 """474 element = self._find_element_cross_frame(selector)475 return element.input_value(timeout=timeout)476 def is_checked(self, selector: str) -> bool:477 """返回是否选中元素。如果元素不是复选框或单选输入,则引发异常。"""478 return self._find_element_cross_frame(selector).is_checked()479 def is_disabled(self, selector: str) -> bool:480 """返回元素是否被禁用,与启用相反。"""481 return self._find_element_cross_frame(selector).is_disabled()482 def is_editable(self, selector: str) -> bool:483 """返回元素是否可编辑。"""484 return self._find_element_cross_frame(selector).is_editable()485 def is_enabled(self, selector: str) -> bool:...

Full Screen

Full Screen

post.py

Source:post.py Github

copy

Full Screen

...11logger = Logger()12# async def ig_context(page, postId):13async def ig_context(page, postId, playwright):14 '''取得文案'''15 page.set_default_navigation_timeout(60000)16 a_list = []17 a_info = []18 all_a_text = '//html/body/div[1]/section/main/div/div[1]/article/div[3]/div[1]/ul/div/li/div/div/div[2]/span/a'19 context1_xpath = '//html/body/div[1]/section/main/div/div[1]/article/div[3]/div[1]/ul/div/li/div/div/div[2]/span'20 context2_xpath = '//html/body/div[1]/section/main/div/div/article/div[3]/div[1]/ul/div/li/div/div/div[2]/span'21 exist_context = ''22 for i in range(5):23 try:24 await page.goto(f"https://www.instagram.com/p/{postId}/")25 await page.wait_for_load_state('load')26 await page.wait_for_load_state('domcontentloaded')27 await page.wait_for_load_state('networkidle')28 await page.wait_for_timeout(random.randint(3000,5000))29 print('IG 已進入粉專')30 exists = await page.is_visible(context1_xpath)31 print(exists)32 break33 except TimeoutError as e:34 await page.screenshot(path='IG_TimeoutError1.png')35 print(f'IG: {postId} 連線逾時(未進入粉專) 嘗試第{i}次')36 logger.error(f'IG: {postId} 連線逾時(未進入粉專) 嘗試第{i}次')37 continue38 try:39 print('解析中')40 await page.screenshot(path='IG_debug.png')41 for context in [context1_xpath, context2_xpath]:42 try:43 await page.wait_for_selector(context, timeout=6000)44 except TimeoutError:45 print('pass')46 exists = await page.is_visible(context)47 if exists:48 exist_context += context49 print(f'xpath found')50 break51 else:52 print(f'pass {context}')53 54 # if not context_exist:55 # print('Context not found')56 # await page.screenshot(path='IG_context_notfound.png')57 # return None, None58 print('1')59 all_a_text = await page.query_selector_all(all_a_text)60 print('2')61 print(exist_context)62 text = await page.inner_text(exist_context,timeout=0)63 print('Text get')64 for a in all_a_text:65 a_text = await a.inner_text()66 print(f'Link get: {a_text}')67 attrs = await a.get_attribute('class')68 print(attrs)69 href = await a.get_attribute('href')70 print(href)71 if attrs == 'notranslate':72 print('Found tag')73 url = f'https://www.instagram.com{href}'74 print(url)75 tag_name = await _get_tag_name(url)76 tag_info = {'tag': a, 'name':tag_name}77 a_info.append(tag_info)78 # # print('Tag name saved')79 80 a_list.append(a)81 logger.info(f'IG: {postId} IG 已取得文案 https://www.instagram.com/p/{postId}/')82 return text, a_list83 except TimeoutError:84 await page.screenshot(path='IG_TimeoutError2.png')85 logger.error(f'IG 文案找不到標籤 請確認 IG 是否改版')86 87 except Exception as e:88 await page.screenshot(path='IG_UnexpectedError.png')89 logger.error(f'IG 文案: 預期外錯誤 {e}')90 print(f'IG 文案: 預期外錯誤 {e}')91 # try:92 # likes = await page.inner_text('//html/body/div[1]/section/main/div/div[1]/article/div[3]/section[2]/div/div[2]/a/span')93 # likes = int(likes)94 # except TimeoutError:95 # likes = 096 # logger.error(f'IG 文案讚數找不到標籤 請確認 IG 是否改版')97 98 99 # loop = asyncio.get_event_loop()100 # context, a_list = loop.run_until_complete(get_post_context(page, postId))101 # print('IG 已擷取文案:')102 # print('='*30)103 # print(context)104 # print('='*30)105 # return context, a_list106async def _get_tag_name(url):107 playwright = await async_playwright().start() 108 browser = await playwright.firefox.launch(headless=True, proxy={'server':'proxy.soax.com:9010','username':'F3JZJYifgvqVCHbd','password':'wifi;gb;three;;'}) # IG只能用firefox (chromium會被拒絕訪問)109 # browser = await playwright.firefox.launch(headless=True, proxy={'server':'34.81.17.230:8080'}) # IG只能用firefox (chromium會被拒絕訪問)110 my_browser = await browser.new_context(user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36')111 page = await my_browser.new_page()112 try:113 await page.goto(url)114 await page.screenshot(path='IG_debug1.png')115 tag_name = await page.inner_text('//html/body/div[1]/section/main/div/div[1]/h1')116 return tag_name117 except TimeoutError as e:118 print(e)119 await page.screenshot(path='IG_1TimeoutError.png')120async def extract_comments_full(page, postData):121 122 output_json = []123 postId = postData['postId']124 async def _get_post_comment(page, postId, output_json):125 page.set_default_navigation_timeout(70000)126 # 進入粉專頁面127 for i in range(5):128 try:129 await page.goto(f"https://www.instagram.com/p/{postId}/")130 await page.wait_for_load_state('load')131 await page.wait_for_timeout(random.randint(4000,8000))132 break133 except TimeoutError as e:134 await page.screenshot(path='IG_kol_page_timeout.png')135 print(f'IG 連線逾時: 重新嘗試第{i}次')136 logger.error(f'IG 連線逾時: 重新嘗試第{i}次 {e}')137 sleep(3)138 # raise f'連線逾時: {e}'139 continue140 # 擷取留言及子留言141 return Comment(output_json, page)142 loop = asyncio.get_event_loop() 143 loop.run_until_complete(_get_post_comment(page, postId, output_json))144 145 postData['comments'] = output_json146 147 return postData148async def basic_count(page, postId):149 async def get_basic_count(page, postId):150 for i in range(5):151 try:152 await page.goto(f"https://www.instagram.com/p/{postId}/")153 await page.wait_for_load_state('load')154 await page.wait_for_timeout(random.randint(4000,8000))155 break156 except TimeoutError as e:157 await page.screenshot(path='IG_kol_page_timeout.png')158 print(f'IG 連線逾時: 重新嘗試第{i}次')159 logger.error(f'IG 連線逾時: 重新嘗試第{i}次 {e}')160 sleep(3)161 # raise f'連線逾時: {e}'162 163 likes = await page.inner_text('//html/body/div[1]/section/main/div/div[1]/article/div[3]/section[2]/div/div/a/span')164 likes = int(likes)165 await press_more_comments(page)166 comments = await page.query_selector_all('//html/body/div[1]/section/main/div/div[1]/article/div[3]/div[1]/ul/ul')167 comment_count = 0168 if not comments == []:169 for c in comments:170 comment_count += 1171 more_replies_button = await c.query_selector('//li/ul/li/div/button/span')172 if more_replies_button:173 button_text = await more_replies_button.inner_text()174 replies_count = re.search(r'(\d+)', button_text).group(1)175 comment_count += int(replies_count) 176 177 print(f'IG Comment_count:{comment_count} Likes:{likes}')178 return (likes, comment_count)179 180 loop = asyncio.get_event_loop()181 likes, comment_count = loop.run_until_complete(get_basic_count(page, postId))182 return (likes, comment_count)183async def get_post_context(page, postId):184 '''取得文案'''185 page.set_default_navigation_timeout(30000)186 for i in range(5):187 try:188 await page.goto(f"https://www.instagram.com/p/{postId}/")189 await page.wait_for_load_state('load')190 # await page.wait_for_load_state('domcontentloaded')191 # await page.wait_for_load_state('networkidle')192 print('IG 已進入粉專')193 break194 except TimeoutError as e:195 await page.screenshot(path='IG_TimeoutError1.png')196 print(f'IG: {postId} 連線逾時(未進入粉專) 嘗試第{i}次')197 logger.error(f'IG: {postId} 連線逾時(未進入粉專) 嘗試第{i}次')198 sleep(3)199 continue...

Full Screen

Full Screen

ggogle_tans.py

Source:ggogle_tans.py Github

copy

Full Screen

...14 context.set_default_timeout(0)15 # Open new page16 page1 = context.new_page()17 page1.set_default_timeout(0)18 page1.set_default_navigation_timeout(0)19 page2 = context.new_page()20 page2.set_default_timeout(0)21 page2.set_default_navigation_timeout(0)22 # Go to https://translate.google.cn/?sl=en&tl=zh-CN&op=translate23 page1.goto("https://translate.google.cn/?sl=en&tl=zh-CN&op=translate")24 page2.goto("https://translate.google.cn/?sl=en&tl=zh-CN&op=translate")25 # Click [aria-label="原文"]26 # page.click("[aria-label=\"原文\"]")27 # Fill [aria-label="原文"]28 # 一个简单的并行,把需要翻译的内容分成两部分,分开在两个网页上翻译29 result1 = []30 result2 = []31 trans_num = len(trans_list)32 trans_lis_1 = trans_list[0:trans_num // 2]33 trans_lis_2 = trans_list[trans_num // 2:trans_num]34 for i,j in zip(trans_lis_1, trans_lis_2):35 # 把内容分别输入到两个网页中进行翻译,然后休息3s...

Full Screen

Full Screen

app.py

Source:app.py Github

copy

Full Screen

...10mydb = Database(f'mongodb://{os.environ["MONGODB_USERNAME"]}:{os.environ["MONGODB_PASSWORD"]}@{os.environ["MONGODB_HOSTNAME"]}:27017/')11async def worker(context, url: str):12 page = await context.new_page()13 page.set_default_timeout(180000)14 page.set_default_navigation_timeout(180000)15 await page.goto(url, timeout=0)16 await page.locator('//*[@id="__next"]/div[1]/div[3]/div[3]/div[2]/div[2]/div[2]/div[1]/div/h1').wait_for()17 product_star = page.locator('//div[@class="d-none d-block-lg ml-12 left-0 pos-sticky"]/div[@class="d-flex ai-center"]/p[1]')18 product_qualities = await page.query_selector_all('//div[@class="d-flex ai-center bg-color-neutral-600 mr-auto"]/p[@class="text-body-2"]')19 quality_construction = await product_qualities[0].text_content()20 purchase_value = await product_qualities[1].text_content()21 innovation = await product_qualities[2].text_content()22 features = await product_qualities[3].text_content()23 ease_of_use = await product_qualities[4].text_content()24 design = await product_qualities[5].text_content()25 product_star = await product_star.all_text_contents()26 product_star = product_star[0]27 query = dict(time=datetime.datetime.now(), link=url, quality_construction=quality_construction, purchase_value=purchase_value, innovation=innovation,28 features=features, ease_of_use=ease_of_use, design=design, product_star=product_star)29 # save post details30 mydb.insert_query(query=query, db_name='digikala', col_name='product_detail')31async def main():32 async with async_playwright() as playwright:33 browser = await playwright.chromium.launch(headless=True)34 host = "https://www.digikala.com"35 product_pre = "https://www.digikala.com/product"36 context = await browser.new_context()37 # Open new page38 page = await context.new_page()39 # set 90 seconds timeout for poor connections40 page.set_default_timeout(90000)41 page.set_default_navigation_timeout(90000)42 # Go to https://www.digikala.com/search/category-mobile-phone/43 await page.goto("https://www.digikala.com/search/category-mobile-phone/", timeout=90000)44 await page.locator(45 "//html/body/div[1]/div[1]/div[3]/div[3]/div[1]/div/section[1]/div[2]/div[1]/div/div[1]/article").wait_for()46 result = html.fromstring(await page.content())47 products = result.xpath("//article/a")48 product_links = []49 for product in products:50 image = product.xpath(".//img[not(contains(@data-src, '.svg'))][1]")51 if image:52 image = image[0].get("data-src")53 try:54 title = product.xpath(".//h2//text()")[0]55 link = product.get("href")...

Full Screen

Full Screen

Playwright tutorial

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful