[Python] How to wait for element to disappear if you cannot rely on locators

Discussão em 'Python' iniciado por Stack, Agosto 24, 2025.

  1. Stack

    Stack Membro Participativo

    Playwright discourages ElementHandle, but how else would I do the following?

    # get a handle to the old message
    old_message_element = page.locator("#message").element_handle()
    # open new message from message list
    page.locator(f"//*[@id='inbox']//h4[text()='{subject}']").click()
    # wait for the old message to be unloaded
    old_message_element.wait_for_element_state(state="hidden")
    # type an answer
    answer_input = page.locator("#message textarea[name='answer']")
    answer_input.click()
    answer_input.type("answer")
    ...


    The newly selected message could be the same as the old one but it will be a different DOM element, so using a locator for it to appear is not enough, the old one first needs to be gone. This matters because a reply text box gets replaced when loading the new message, so clicking it too quickly will cause the browser to lose focus.

    Using a locator to wait for the old message to disappear will also not work because the locator matches with the new message too.

    Continue reading...

Compartilhe esta Página