"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[72565],{3905:(e,t,n)=>{n.d(t,{Zo:()=>p,kt:()=>d});var r=n(67294);function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function l(e){for(var t=1;t=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}var s=r.createContext({}),c=function(e){var t=r.useContext(s),n=t;return e&&(n="function"==typeof e?e(t):l(l({},t),e)),n},p=function(e){var t=c(e.components);return r.createElement(s.Provider,{value:t},e.children)},u={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},m=r.forwardRef((function(e,t){var n=e.components,a=e.mdxType,o=e.originalType,s=e.parentName,p=i(e,["components","mdxType","originalType","parentName"]),m=c(n),d=a,f=m["".concat(s,".").concat(d)]||m[d]||u[d]||o;return n?r.createElement(f,l(l({ref:t},p),{},{components:n})):r.createElement(f,l({ref:t},p))}));function d(e,t){var n=arguments,a=t&&t.mdxType;if("string"==typeof e||a){var o=n.length,l=new Array(o);l[0]=m;var i={};for(var s in t)hasOwnProperty.call(t,s)&&(i[s]=t[s]);i.originalType=e,i.mdxType="string"==typeof e?e:a,l[1]=i;for(var c=2;c{n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>i,default:()=>m,frontMatter:()=>l,metadata:()=>s,toc:()=>p});n(67294);var r=n(3905);function a(){return a=Object.assign||function(e){for(var t=1;t=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}const l={},i="Locators",s={unversionedId:"guides/locators",id:"version-20.8.1/guides/locators",title:"Locators",description:"Locators is a new experimental API that combines waitForSelector and element",source:"@site/versioned_docs/version-20.8.1/guides/locators.md",sourceDirName:"guides",slug:"/guides/locators",permalink:"/guides/locators",draft:!1,tags:[],version:"20.8.1",frontMatter:{},sidebar:"docs",previous:{title:"Query Selectors",permalink:"/guides/query-selectors"},next:{title:"Evaluate JavaScript",permalink:"/guides/evaluate-javascript"}},c={},p=[{value:"Clicking an element",id:"clicking-an-element",level:2},{value:"Filling out an input",id:"filling-out-an-input",level:2},{value:"Hover over an element",id:"hover-over-an-element",level:2},{value:"Scroll an element",id:"scroll-an-element",level:2},{value:"Configuring locators",id:"configuring-locators",level:2},{value:"Getting locator events",id:"getting-locator-events",level:2}],u={toc:p};function m(e){var{components:t}=e,n=o(e,["components"]);return(0,r.kt)("wrapper",a({},u,n,{components:t,mdxType:"MDXLayout"}),(0,r.kt)("h1",a({},{id:"locators"}),"Locators"),(0,r.kt)("p",null,"Locators is a new experimental API that combines ",(0,r.kt)("inlineCode",{parentName:"p"},"waitForSelector")," and element\nactions in a single unit. In combination with additional precondition checks\nthis allows locators to retry failed actions automatically leading to less flaky\nautomation scripts."),(0,r.kt)("admonition",a({},{type:"note"}),(0,r.kt)("p",{parentName:"admonition"},"Locators API is experimental and we will not follow semver for breaking changes\nin the Locators API.")),(0,r.kt)("h2",a({},{id:"clicking-an-element"}),"Clicking an element"),(0,r.kt)("pre",null,(0,r.kt)("code",a({parentName:"pre"},{className:"language-ts"}),"await page.locator('button').click();\n")),(0,r.kt)("p",null,"The following preconditions are automatically checked:"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},"Ensures the element is in the viewport."),(0,r.kt)("li",{parentName:"ul"},"Waits for the element to become\n",(0,r.kt)("a",a({parentName:"li"},{href:"https://pptr.dev/api/puppeteer.elementhandle.isvisible/"}),"visible")," or hidden."),(0,r.kt)("li",{parentName:"ul"},"Waits for the element to become enabled."),(0,r.kt)("li",{parentName:"ul"},"Waits for the element to have a stable bounding box over two consecutive\nanimation frames.")),(0,r.kt)("h2",a({},{id:"filling-out-an-input"}),"Filling out an input"),(0,r.kt)("pre",null,(0,r.kt)("code",a({parentName:"pre"},{className:"language-ts"}),"await page.locator('input').fill('value');\n")),(0,r.kt)("p",null,"Automatically detects the input type and choose an approritate way to fill it out with the provided value."),(0,r.kt)("p",null,"The following preconditions are automatically checked:"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},"Ensures the element is in the viewport."),(0,r.kt)("li",{parentName:"ul"},"Waits for the element to become\n",(0,r.kt)("a",a({parentName:"li"},{href:"https://pptr.dev/api/puppeteer.elementhandle.isvisible/"}),"visible")," or hidden."),(0,r.kt)("li",{parentName:"ul"},"Waits for the element to become enabled."),(0,r.kt)("li",{parentName:"ul"},"Waits for the element to have a stable bounding box over two consecutive\nanimation frames.")),(0,r.kt)("h2",a({},{id:"hover-over-an-element"}),"Hover over an element"),(0,r.kt)("pre",null,(0,r.kt)("code",a({parentName:"pre"},{className:"language-ts"}),"await page.locator('div').hover();\n")),(0,r.kt)("p",null,"The following preconditions are automatically checked:"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},"Ensures the element is in the viewport."),(0,r.kt)("li",{parentName:"ul"},"Waits for the element to become\n",(0,r.kt)("a",a({parentName:"li"},{href:"https://pptr.dev/api/puppeteer.elementhandle.isvisible/"}),"visible")," or hidden."),(0,r.kt)("li",{parentName:"ul"},"Waits for the element to have a stable bounding box over two consecutive\nanimation frames.")),(0,r.kt)("h2",a({},{id:"scroll-an-element"}),"Scroll an element"),(0,r.kt)("pre",null,(0,r.kt)("code",a({parentName:"pre"},{className:"language-ts"}),"await page.locator('div').scroll({\n scrollLeft: 10,\n scrollTop: 20,\n});\n")),(0,r.kt)("p",null,"The following preconditions are automatically checked:"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},"Ensures the element is in the viewport."),(0,r.kt)("li",{parentName:"ul"},"Waits for the element to become\n",(0,r.kt)("a",a({parentName:"li"},{href:"https://pptr.dev/api/puppeteer.elementhandle.isvisible/"}),"visible")," or hidden."),(0,r.kt)("li",{parentName:"ul"},"Waits for the element to have a stable bounding box over two consecutive\nanimation frames.")),(0,r.kt)("h2",a({},{id:"configuring-locators"}),"Configuring locators"),(0,r.kt)("p",null,"Locators can be configured to tune configure the preconditions and other other options:"),(0,r.kt)("pre",null,(0,r.kt)("code",a({parentName:"pre"},{className:"language-ts"}),"await page\n .locator('button')\n .setEnsureElementIsInTheViewport(false)\n .setTimeout(0)\n .setVisibility(null)\n .setWaitForEnabled(false)\n .setWaitForStableBoundingBox(false)\n .click();\n")),(0,r.kt)("h2",a({},{id:"getting-locator-events"}),"Getting locator events"),(0,r.kt)("p",null,"Currently, locators support a single event that notifies you when the locator is about to perform the action:"),(0,r.kt)("pre",null,(0,r.kt)("code",a({parentName:"pre"},{className:"language-ts"}),"let willClick = false;\nawait page\n .locator('button')\n .on(LocatorEmittedEvents.Action, () => {\n willClick = true;\n })\n .click();\n")),(0,r.kt)("p",null,"This event can be used for logging/debugging or other purposes. The event might\nfire multiple times if the locator retries the action."))}m.isMDXComponent=!0}}]);