Comments for Get Element By Role

Topic for comments about Browser library keyword Get Element By Role

Hi Rene,

when I try to select a locator on a website I get the followin error message:
Error: locator.waitFor: Error: strict mode violation: getByRole(‘link’, { name: ‘Computers’ }) resolved to 2 elements:
1) Computers↔ aka getByRole(‘link’, { name: ‘Computers’ }).first()
2) Computers↔ aka getByRole(‘link’, { name: ‘Computers’ }).nth(1)

With the keyword “Get Element by Role” I couldn’t find an option to select one them like I can in PlayWright itself. Is there another way to do it?

There are two elements with the same attributes.

You can get all these elements by adding all_elements to True with the keyword Get Element By Role or disable the strict mode before.

then just add " >> nth=0" to the returned selector to get only the first one.

Hi Rene,

when I use the keyword like you mentioned it I will get a list of locators. Would it make sense to add that string to the returned selector? Maybe I just missunderstood you.

I found out that the following also works:
Click on
[Arguments] ${item}
${element} = Get Element By Role link all_elements=True name=${item}
Click ${element}[1]

Oh right.

You just take the first element of the list.

Hi Rene,

How exactly to use regular expr in “get element by role” ?
I don’t think giving an regular expe is working.
eg:
${login_button} get element by role button name=Login\s*In

Hi @RatexMak,

See the regex example in the help for Get Element By Role

${button}     Get Element By Role    button    name=/submit/i

In your case you would need to do something like:

${login_button}     get element by role     button     name=/Login\s*In/

You might also want to consider which flags if any you need, in the example from the help they used the case insensitive flag (i)

Dave.

Thank you, Damies13!
I have re-read the doument, and notices the regex should be following JavaScript model. I have try several regex after that.
And now ‘name=/Login\s*In/gm’ is working for me.
Thank you again for your generous help!

1 Like

So sorry to bother again.
I was trying to use ‘get element by role’ keyword in RF and have ‘set selector prefix’ previously. However, it seems that i cannot id the button with name=‘Query’ in a iframe.
Is ‘set selector prefix’ working for ‘get element by role’? Or any other suggestions?
And I believe the prefix is working for ‘click’, how do i get the locator of the query_button, and pass it to the ‘click’ function?

set selector prefix	xpath=//iframe[@name=\"pdiv_page-mainIframe\"] >>> 
${query_button}	get element by role	button	name=Query
click	${query_button}

page.frame_locator(“iframe[name="whCreate"]”).get_by_role(“row”, name=“星期䞀”).locator(“input[name="s\:scProjCode"]”).click()

Hello everyone,

I’m trying to get a child element by role using the cascade syntax and it’s not working, as described here:

Could you please help me on this?

I’m new to this forum as well, so I apologize for posting the same question in two different places, but I really need this.

Thanks a lot.