Selection Specifications (first-of-type, nth-child) Not Working

Hi,

I’m running into an error with my program. Each of the elements I am looping through do not have a unique-tested. The only thing that distinguishes them is their text, which I am trying to verify if any of them matches with the provided argument.

I am using a for loop to iterate across each of the messages to look for the desired text. I have used first-of-type and nth-child to try to specify the index (amongst other things like nth-of-type(${index})). However I still get the same strict mode error.

Error: locator.elementHandle: Error: strict mode violation: locator('.Toastify__toast-body:first-of-type') resolved to 3 elements:
1) <div role="alert" class="Toastify__toast-body">…</div> aka getByRole('alert').first()
2) <div role="alert" class="Toastify__toast-body">…</div> aka getByRole('alert').nth(1)
3) <div role="alert" class="Toastify__toast-body">…</div> aka getByRole('alert').nth(2)

image

Does anyone know why this is happening?

Edit: Each of my elements do not have a unique test-id, nor any other ways to distinguish one another other than order (1st, 2nd, 3rd) and text*

Sorry, here is the code in the screenshot if you’d like to copy-paste it

Verify Toastify Message Appears
[Arguments] ${text}
${count}= Get Element Count ${TOAST_BODY}
${found}= Set Variable False
FOR ${index} IN RANGE 1 ${count}+1
IF ${index} == 1
${toast_text}= Get Text ${TOAST_BODY}:nth-of-type(1)
ELSE
${toast_text}= Get Text ${TOAST_BODY}:nth-child(${index})
END
Run Keyword If ‘${text}’ in ‘${toast_text}’
… Set Variable ${found} True
END
IF ${found}
Toastify message with text “${text}” WAS found
ELSE
Toastify message with text “${text}” WAS NOT found
END

Hi Kenneth,

An easier approach might be to use Get Elements with ${TOAST_BODY} and then for item in the list you got from Get Elements, something like this:

Verify Toastify Message Appears
    [Arguments]     ${text}
    ${alerts}=     Get Elements     ${TOAST_BODY}
    ${found}=     Set Variable     False
    FOR     ${alert}     IN     @{alerts}
        ${toast_text}=     Get Text     ${alert}
        Run Keyword If     ‘${text}’ in ‘${toast_text}’
        … Set Variable ${found}     True
    END
    IF ${found}
        Toastify message with text “${text}” WAS found
    ELSE
        Toastify message with text “${text}” WAS NOT found    
    END

Dave.

3 Likes

Thanks so much for the help Dave your code works (with a couple minor fixes)!

Here is the final code I used.

Verify Toastify Message Appears
    [Arguments]     ${text}
    ${alerts}=     Get Elements     ${TOAST_BODY}
    ${found}=     Set Variable     False
    FOR     ${alert}     IN     @{alerts}
        ${toast_text}=     Get Text     ${alert}
        Run Keyword If     '${text}' in '${toast_text}'
        ...    Set Variable     {found}     True
    END
    IF    ${found}
        Log    Toastify message with text “${text}” WAS found
    ELSE
        Log    Toastify message with text “${text}” WAS NOT found    
    END
2 Likes