Issue 1:
System info

Playwright Version: [v1.32.0 (Java)]

Operating System: [macOS 13.3]

Browser: [Firefox, WebKit, (Chromium unclear)]

Other info:

Source code

I provided exact source code that allows reproducing the issue locally.

My website asynchronously navigates to another page while I am trying to use locators like this in a loop (because I am waiting for one of two locators to become visible):

navbar
.locator(LOGGED_IN_NAVBAR_SELECTOR)
.locator(ACCOUNT_MENU_BUTTON_SELECTOR)
.isVisible();

This results in an error "Execution context was destroyed". I have reproduced this on Webkit and Firefox, but could not manage to reproduce it with Chromium. It's not happening every time because of the racy nature of the async navigation. I don't know if Chromium doesn't have this problem or the timing is just different, thereby avoiding the error.

Stack trace on Webkit:

com.microsoft.playwright.PlaywrightException: Error {
message='Execution context was destroyed, most likely because of a navigation
=========================== logs ===========================
checking visibility of locator("app-navbar").locator("app-logged-in-navbar").locator("button:has-text('arrow_drop_down')")
============================================================
name='Error
stack='Error: Execution context was destroyed, most likely because of a navigation
=========================== logs ===========================
checking visibility of locator("app-navbar").locator("app-logged-in-navbar").locator("button:has-text('arrow_drop_down')")
============================================================
at Frame._contextDestroyed (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-4016531511947763690/package/lib/server/frames.js:1446:30)
at WKPage._removeContextsForFrame (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-4016531511947763690/package/lib/server/webkit/wkPage.js:453:32)
at WKPage._onFrameNavigated (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-4016531511947763690/package/lib/server/webkit/wkPage.js:438:10)
at WKSession.<anonymous> (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-4016531511947763690/package/lib/server/webkit/wkPage.js:389:127)
at WKSession.emit (node:events:513:28)
at /private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-4016531511947763690/package/lib/server/webkit/wkConnection.js:172:41
at runNextTicks (node:internal/process/task_queues:60:5)
at process.processImmediate (node:internal/timers:447:9)
}

at com.microsoft.playwright.impl.WaitableResult.get(WaitableResult.java:54)
at com.microsoft.playwright.impl.ChannelOwner.runUntil(ChannelOwner.java:111)
at com.microsoft.playwright.impl.Connection.sendMessage(Connection.java:126)
at com.microsoft.playwright.impl.ChannelOwner.sendMessage(ChannelOwner.java:102)
at com.microsoft.playwright.impl.FrameImpl.isVisibleImpl(FrameImpl.java:641)
at com.microsoft.playwright.impl.FrameImpl.lambda$isVisible$29(FrameImpl.java:627)
at com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:47)
at com.microsoft.playwright.impl.ChannelOwner.withLogging(ChannelOwner.java:87)
at com.microsoft.playwright.impl.FrameImpl.isVisible(FrameImpl.java:627)
at com.microsoft.playwright.impl.LocatorImpl.isVisible(LocatorImpl.java:374)
at com.microsoft.playwright.Locator.isVisible(Locator.java:3668)
<... project specific frames ...>
Caused by: com.microsoft.playwright.impl.DriverException: Error {
message='Execution context was destroyed, most likely because of a navigation
=========================== logs ===========================
checking visibility of locator("app-navbar").locator("app-logged-in-navbar").locator("button:has-text('arrow_drop_down')")
============================================================
name='Error
stack='Error: Execution context was destroyed, most likely because of a navigation
=========================== logs ===========================
checking visibility of locator("app-navbar").locator("app-logged-in-navbar").locator("button:has-text('arrow_drop_down')")
============================================================
at Frame._contextDestroyed (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-4016531511947763690/package/lib/server/frames.js:1446:30)
at WKPage._removeContextsForFrame (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-4016531511947763690/package/lib/server/webkit/wkPage.js:453:32)
at WKPage._onFrameNavigated (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-4016531511947763690/package/lib/server/webkit/wkPage.js:438:10)
at WKSession.<anonymous> (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-4016531511947763690/package/lib/server/webkit/wkPage.js:389:127)
at WKSession.emit (node:events:513:28)
at /private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-4016531511947763690/package/lib/server/webkit/wkConnection.js:172:41
at runNextTicks (node:internal/process/task_queues:60:5)
at process.processImmediate (node:internal/timers:447:9)
}
at com.microsoft.playwright.impl.Connection.dispatch(Connection.java:226)
at com.microsoft.playwright.impl.Connection.processOneMessage(Connection.java:206)
at com.microsoft.playwright.impl.ChannelOwner.runUntil(ChannelOwner.java:109)
... 53 more

Firefox stack trace:

com.microsoft.playwright.PlaywrightException: Error {
message='Execution context was destroyed, most likely because of a navigation
=========================== logs ===========================
checking visibility of locator("app-navbar").locator("app-logged-in-navbar").locator("button:has-text('arrow_drop_down')")
============================================================
name='Error
stack='Error: Execution context was destroyed, most likely because of a navigation
=========================== logs ===========================
checking visibility of locator("app-navbar").locator("app-logged-in-navbar").locator("button:has-text('arrow_drop_down')")
============================================================
at Frame._contextDestroyed (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-12798600472171551690/package/lib/server/frames.js:1446:30)
at FFPage._onExecutionContextDestroyed (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-12798600472171551690/package/lib/server/firefox/ffPage.js:141:19)
at FFPage._onExecutionContextsCleared (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-12798600472171551690/package/lib/server/firefox/ffPage.js:144:88)
at FFSession.emit (node:events:513:28)
at /private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-12798600472171551690/package/lib/server/firefox/ffConnection.js:204:41
at runNextTicks (node:internal/process/task_queues:60:5)
at process.processImmediate (node:internal/timers:447:9)
}

at com.microsoft.playwright.impl.WaitableResult.get(WaitableResult.java:54)
at com.microsoft.playwright.impl.ChannelOwner.runUntil(ChannelOwner.java:111)
at com.microsoft.playwright.impl.Connection.sendMessage(Connection.java:126)
at com.microsoft.playwright.impl.ChannelOwner.sendMessage(ChannelOwner.java:102)
at com.microsoft.playwright.impl.FrameImpl.isVisibleImpl(FrameImpl.java:641)
at com.microsoft.playwright.impl.FrameImpl.lambda$isVisible$29(FrameImpl.java:627)
at com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:47)
at com.microsoft.playwright.impl.ChannelOwner.withLogging(ChannelOwner.java:87)
at com.microsoft.playwright.impl.FrameImpl.isVisible(FrameImpl.java:627)
at com.microsoft.playwright.impl.LocatorImpl.isVisible(LocatorImpl.java:374)
at com.microsoft.playwright.Locator.isVisible(Locator.java:3668)
< ... project specific frames ... >
Caused by: com.microsoft.playwright.impl.DriverException: Error {
message='Execution context was destroyed, most likely because of a navigation
=========================== logs ===========================
checking visibility of locator("app-navbar").locator("app-logged-in-navbar").locator("button:has-text('arrow_drop_down')")
============================================================
name='Error
stack='Error: Execution context was destroyed, most likely because of a navigation
=========================== logs ===========================
checking visibility of locator("app-navbar").locator("app-logged-in-navbar").locator("button:has-text('arrow_drop_down')")
============================================================
at Frame._contextDestroyed (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-12798600472171551690/package/lib/server/frames.js:1446:30)
at FFPage._onExecutionContextDestroyed (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-12798600472171551690/package/lib/server/firefox/ffPage.js:141:19)
at FFPage._onExecutionContextsCleared (/private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-12798600472171551690/package/lib/server/firefox/ffPage.js:144:88)
at FFSession.emit (node:events:513:28)
at /private/var/folders/l5/mt5xnj_14rn9j4lsnfs5n9b40000gn/T/playwright-java-12798600472171551690/package/lib/server/firefox/ffConnection.js:204:41
at runNextTicks (node:internal/process/task_queues:60:5)
at process.processImmediate (node:internal/timers:447:9)
}
at com.microsoft.playwright.impl.Connection.dispatch(Connection.java:226)
at com.microsoft.playwright.impl.Connection.processOneMessage(Connection.java:206)
at com.microsoft.playwright.impl.ChannelOwner.runUntil(ChannelOwner.java:109)
... 53 more

This is actually the same issue as #16720

The solution there was to use assertThat(locator).isVisible.

This doesn't work here, because I am manually waiting for one of two conditions to occur, so I need to use Locator.isVisible.

Previously this was "solved" (?) by putting a Page.waitForLoadState(LoadState.NETWORKIDLE) in front of the visibility checking loop. This presumably worked because the async navigation happens shortly after loading the page, so it would have happened during the NETWORKIDLE waiting, after which the execution context is stable.

I now removed the waitForLoadState because NETWORKIDLE is discouraged, and asserting for web elements is recommended, per #22897 (comment)

But then this bug got triggered instead.

Issue 2:
I would like to suggest an enhancement for the Playwright testing framework. Currently, when using the codegen/selector picker feature, it would be beneficial to have the ability to exclude certain types of selectors from being generated. For example, in cases where I am using a framework that assigns a unique ID to each element, I would like to disable the "#ids" selector option to prevent the codegen from randomly selecting one of these dynamically generated IDs.

Proposed Solution:

I propose two possible solutions to address this issue:

Ability to Eliminate Selectors: It would be helpful to have a configuration option or flag that allows users to specify certain types of selectors to be excluded from the codegen/selector picker. This way, I can indicate that the "#ids" selector should be ignored, and the codegen will not generate code using this selector type.

Selection Range of Available Selectors: Alternatively, providing a range of available selectors to choose from during code generation would also be beneficial. This would allow me to manually select the desired selector type from the available options, excluding the ones I do not wish to use, such as "#ids".

I believe these enhancements would greatly improve the flexibility and usability of the Playwright testing framework, especially in scenarios where specific selectors need to be ignored or selected manually.

examples how Aqua (Jetbrains Testing IDE) implemented an feature simular