OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to check if an element exists on the page in Playwright.js

  • Thread starter Thread starter cole
  • Start date Start date
C

cole

Guest
I am using playwright.js to write a script for https://target.com, and on the page where you submit shipping information, it will provide the option to use a saved address if you have gone through the checkout process previously on that target account.

I would like to enter fresh shipping information every time I run the script, so I must have playwright click delete if it exists on the page, and then enter the shipping information.

The function that is shown below works to click delete, but then it times out at if (await page.$$("text='Delete'") != []) rather than executing the else part of the function.

How can I rewrite this function so that it simply checks if the element (selector: text='Delete') exists, and clicks it if it does, and executes the filling part of the function if it doesn't?

Code:
  async function deliveryAddress() {
    if (await page.$$("text='Delete'") != []) {
      await page.click("text='Delete'", {force:true})
      await deliveryAddress()
    } else {
      await page.focus('input#full_name')
      await page.type('input#full_name', fullName, {delay: delayms});
      await page.focus('input#address_line1')
      await page.type('input#address_line1', address, {delay: delayms});
      await page.focus('input#zip_code')
      await page.type('input#zip_code', zipCode, {delay: delayms});
      await page.focus('input#mobile')
      await page.type('input#mobile', phoneNumber, {delay: delayms});
      await page.click("text='Save & continue'", {force:true})
    }
  }

<p>I am using playwright.js to write a script for <a href="https://target.com" rel="noreferrer">https://target.com</a>, and on the page where you submit shipping information, it will provide the option to use a saved address if you have gone through the checkout process previously on that target account.</p>
<p>I would like to enter fresh shipping information every time I run the script, so I must have playwright click delete if it exists on the page, and then enter the shipping information.</p>
<p>The function that is shown below works to click delete, but then it times out at <code>if (await page.$$("text='Delete'") != [])</code> rather than executing the <code>else</code> part of the function.</p>
<p>How can I rewrite this function so that it simply checks if the element (selector: <code>text='Delete'</code>) exists, and clicks it if it does, and executes the filling part of the function if it doesn't?</p>
<pre class="lang-js prettyprint-override"><code> async function deliveryAddress() {
if (await page.$$("text='Delete'") != []) {
await page.click("text='Delete'", {force:true})
await deliveryAddress()
} else {
await page.focus('input#full_name')
await page.type('input#full_name', fullName, {delay: delayms});
await page.focus('input#address_line1')
await page.type('input#address_line1', address, {delay: delayms});
await page.focus('input#zip_code')
await page.type('input#zip_code', zipCode, {delay: delayms});
await page.focus('input#mobile')
await page.type('input#mobile', phoneNumber, {delay: delayms});
await page.click("text='Save & continue'", {force:true})
}
}
</code></pre>
 
Top