"More than one element found for locator" warning

22,457

Solution 1

Try this instead:

element.all(by.css("ul.nav button")).first()

Basically, this tells Protractor that you already know there's more than one element, and you just want the first one (like you said in your question).

Solution 2

The warning is there for a reason. You've tied your tests too closely to your data. The selector is too general & you should be more specific. Either by saying element(by.css("ul.nav button:nth-child(1)")) or scoping your search differently. Protractor tests aren't supposed to be testing style or dom, they're supposed to be testing business logic.

Solution 3

Less code:

$$("ul.nav button").first()

Solution 4

Do not underestimate xpath. You can solve thousands of problems with it, including this one

let elem = element(by.xpath('(//div//a)[3]'))

You can specify the number of element to use. Keep in mind the numbers start from 1, not 0 as usually in js

Share:
22,457
alecxe
Author by

alecxe

"I am a soldier, at war with entropy itself" I am a Software Developer and generalist who is in love with the Python language and community. I greatly value clean and maintainable code, great software, but I know when I need to be a perfectionist and when it stands in a way of product delivery. I like to break things, to find new ways to break things, to solve hard problems, to put things under test and stress, and to have my mind blown by an interesting question. Some of my interests: Learning, Productivity, AI, Space Exploration, Internet of Things. "If you change the way you look at things, the things you look at change." - Wayne Dyer If you are looking for a different way to say "Thank you": Amazon wish list Pragmatic wish list Today I left my phone at home And went down to the sea. The sand was soft, the ocean glass, But I was still just me. Then pelicans in threes and fours, Glided by like dinosaurs, An otter basked upon its back, And dived to find another snack. The sun corpuscular and bright, Cast down a piercing shaft, And conjured an inspiring sight On glinting, bobbing craft. Two mermaids rose up from the reef, Out of the breaking waves. Their siren song was opium grief, Their faces from the grave. The mermaids asked a princely kiss To free them from their spell. I said to try a poet’s bliss. They shrugged and bid farewell. The sun grew dark and sinister, In unscheduled eclipse. As two eight-headed aliens Descended in their ships. They said the World was nice enough But didn’t like our star. And asked the way to Betelgeuse, If it wouldn’t be too far. Two whales breached far out to sea, And flew up to the sky, The crowd was busy frolicking, And didn’t ask me why. Today I left my phone at home, On the worst day, you’ll agree. If only I had pictures, If only you could see. Not everything was really there, I’m happy to confess, But I still have the memories, Worth more than tweets and stress. Today I left my phone at home, I had no shakes or sorrow. If that is what my mind can do, It stays at home tomorrow. Gavin Miller

Updated on February 09, 2021

Comments

  • alecxe
    alecxe over 3 years

    In one of my tests, I'm locating an element using a CSS selector:

    element(by.css("ul.nav button"))
    

    There is more than one element matching the query, but, since I need only the first one, I'm okay with the selector.

    The problem is, protractor throws a warning:

    WARNING - more than one element found for locator By.cssSelector("ul.nav button") - the first result will be used

    Is it possible to suppress the warning? In other words, how can I let protractor know that I'm aware of the problem and don't want the warning to be shown anymore?


    Using protractor development version (installed directly from the master branch).