Error: locator.waitFor: Error: strict mode violation: locator

I need help. I’m using the Browser Library robot framework to automate a work application. However, I encountered the following challenge, trying to validate the names of existing columns in a simple way works:

Get Text    //span[text()='CPF']            contains    CPF
Get Text    //span[text()='CNS']            contains    CNS
Get Text    //span[text()='Nascimento']     contains    Nascimento
Get Text    //span[text()='Nome']           contains    Nome
Get Text    //span[text()='Nome da mãe']    contains    Nome da mãe
Get Text    //span[text()='UF']             contains    UF
Get Text    //span[text()='Município']      contains    Município
Get Text    //span[text()='Carteiras']      contains    Carteiras

Wanting to make something more technical, I tried to implement it as follows:

${element}    Set Variable    css=thead tr td span

Wait For Elements State    ${element}    
...    visible    5

Get Text    ${element}    contains    CPF
Get Text    ${element}    contains    CNS
Get Text    ${element}    contains    Nascimento
Get Text    ${element}    contains    Nome
Get Text    ${element}    contains    Nome da mãe
Get Text    ${element}    contains    UF
Get Text    ${element}    contains    Município
Get Text    ${element}    contains    Carteiras

Then the following error is displayed:

Error: locator.waitFor: Error: strict mode violation: locator(‘div .novaListagem table thead tr td span’) resolved to 8 elements: 1) CPF aka getByRole(‘cell’, { name: ‘CPF’, exact: true }).locator(‘span’) 2) CNS aka getByText(‘CNS’) 3) Nascimento aka getByText(‘Nascimento’, { exact: true }) 4) Nome aka getByRole(‘cell’, { name: ‘Nome’, exact: true }).locator(‘span’) 5) Nome da mãe aka getByText(‘Nome da mãe’) 6) UF aka getByText(‘UF’) 7) Município aka getByText(‘Município’) 8) Carteiras aka getByText(‘Carteiras’)

The expected result is that in validation it checks whether it contains the columns CPF, CNS, Birth, Name, Mother’s name, State, Municipality, Licenses.

Hi Alexandre,

Browser Library is working as expected, your css selector (css=thead tr td span) identifies 8 elements and get text only works when it’s selector identifies a single element, this is what the error is telling you.

From Get Text’s documentation:

Keyword uses strict mode, see Finding elements for more details about strict mode.

From Finding elements:

If strict mode is true and locator finds multiple elements from the page, keyword will fail.

Disabling strict mode may stop the error causing the test to fail, I don’t think it’s designed to work as your expecting.

If you want to get all the matching elements you can use Get Elements (not this is different to Get Element which can only get a single element), after getting all the elements in a list you can iterate over them with Get Text.

however a nicer solution might be to do something like this:

${columns}=    Create List    CPF    CNS    Nascimento    Nome    Nome da mãe    UF    Município    Carteiras
FOR    ${column}    IN    @{columns}
    Get Text    //span[text()='${column}']      contains    ${column}
END

Dave.