Browser Library: Getting "strict mode violation: resolved to 2 elements" issue when using Get Text

HI all,

I have a table in which I want to get text from a “td” element and below is the xpath I am using. I am getting the td number dynamically based on the th (column header) as I dont want to hardcode. This xpath worked fine when I used SeleniumLibrary but getting error in the Browser Library.

Xpath used:

//table[@aria-label=‘table’]//tr[1]/td[count(//table[@aria-label=‘table’]//span[text()=‘Host Name’]//parent::th/preceding-sibling::th)+1][@class=‘MuiTableCell-root MuiTableCell-body’]

Below are the logs: I need to use the first element.

waiting for selector “xpath=//table[@aria-label=‘table’]//tr[1]/td[2]”
strict mode violation: “xpath=//table[@aria-label=‘table’]//tr[1]/td[2]” resolved to 2 elements:
1) CDLEN015 aka playwright.$(“text=CDLEN015”)
2) 1 aka playwright.$(".patching-history-table .MuiPaper-root .MuiTable-root .MuiTableBody-root tr td:nth-child(2)")

Could some one please help.

Could some one please help?

Hi Rudy,
There are now 2 elements returned in the logging, But I Think you need to make definition of the xpath unique?

Hi JoostW,
yes how to do it is my question. I have only class name attribute for “td” elements and its same and I need to get text for this xpath

Hi Rudy,

Do you have an example of the DOM structure?

I have the same problem, but in my case in logs showing that I have N elements. But when I paced my XPath with unique value into Chrome DevTools it shows that I have only one match.

Example of xpath: //[contains(@class,‘commonValue’) and contains(@class,‘commonValue2’) and contains(.,‘uniqueValue’)]//[contains(@class,‘commonValue3’)]

As resolving of this problem, I just wrapped my XPath in brackets and take 1st element : (myXpath)[1]

I am using browser library and using below resolved my problem:

Library Browser strict=True

Looks like I already have its ins strict mode. but thanks for hint

Error: locator.elementHandle: Error: strict mode violation: " // div[contains(@class,‘commonValue’) and contains(@class,‘commonValue2’) and contains(.,‘ uniqueValue ’)]//div[contains(@class,‘commonValue3’)]" resolved to N elements:

Actually, I don’t know if you wrote correct. But when I set strict=False everything works fine.

Hello,
actually since a couple of Browser Library versions (can’t remember when exactly) when you choose a selector that correspond to more than one element in the page, the browser library return this kind of error as it can not choose which of the element you want to interact. Previously, it was selecting the 1st element it found.
Let’s imagine you have a page with several “OK” button, choosing click text=“OK” will return on error as there are many text=“OK” elements in it.

Using strict=False will switch to the old behavior, it will choose the 1st element if there are many in the page, but you must be aware of that of course :slight_smile:

1 Like