0

What I need is a way to get out of async with (line 2) if page.click (line 4) doesn't find the element, I tried with break but it didn't work, here's the code:

try:
    async with page.expect_download(timeout=120000) as download_info:
        try:
            await page.click(First_Row_Download, timeout=3000)
        except:
            break

    Download = await download_info.value
    await Download.save_as(Download.suggested_filename)
    Download_Finished = True
    break
except:
    await page.wait_for_timeout(Wait_Time) # 3 Minutos
    await page.click(Button_Refresh)
  • Are you sure it is actually raising an exception? Try inspecting the exception to verify. – Amos Baker Aug 29 '22 at 14:39
  • I've already verified that if page.click() doesn't find the element it enters the except code! –  Aug 29 '22 at 14:41
  • Wouldn't just ``pass`` instead of ``break`` exit the ``async with`` block? I know it's not good pratice, though – SystemSigma_ Aug 29 '22 at 14:50
  • The pass as well as the break makes the code go back to async with, and the code doesn't continue until the 60000 millisecond timeout ends, and what I want is to completely get out of this async and go to the except below! –  Aug 29 '22 at 14:53

1 Answers1

0

How about checking if the element is clickable: https://playwright.dev/docs/api/class-locator#locator-click

use trial option and if it returns true continue execution

  await page.click(First_Row_Download, , {trial:true})
senaid
  • 534
  • 2
  • 8
  • Thanks for the trial tip, but in this case it doesn't work, the code goes back to the async with line and waits for the 60000 timeout to finish, what I want is to get out of it without waiting for the timeout! –  Aug 30 '22 at 16:39
  • I was thinking on checking this with if statement before you even enter async method, and if condition is true then proceed, this way you dont need try/catch – senaid Aug 31 '22 at 08:06
  • I understand you, that's almost what I did to solve the problem, but I still don't know a method of if the code enters the async with exit without waiting for the timeout. –  Aug 31 '22 at 12:57