1

I'm new to Selenium WebDriver. I'm trying to automate a search function on a particular page.

search icon

There are a search input box and a search button (actually, it is a search icon) next to it. I'm able to identify the search box & enter (search) data into the box. But the problem is in identifying the search icon. Inspecting with Firebug shows it has 3 matching nodes. I see all 3 of them have identical attributes & values. The tag name indicates the search icon is an image file.

 <img id="search-icon" style="width:auto; height:23px" onerror="this.onerror=null; this.src='/sites/all/themes/hjknewstheme/images/searchicon.png'" src="/sites/all/themes/hjknewstheme/images/gray_search.svg"/>

How can I get Selenium identify the locator for this element? I tried using ID & also customized XPath. Neither one is working. Any help in handling this issue would be greatly appreciated.

Here's the Xpath I used:

driver.findElementByXPath("//img[@src='/sites/all/themes/hjknewstheme/images/gray_search.svg']").click();

I also used id as a locator. That didn't work either. Here's a snippet of the search icon.

search icon[![][1]][1]

The search icon image is at the beginning of my post.

  • Which XPath or ID have you used to locate the element? Add screen if possible the exact scene of search icon. Which error are you getting? – Bharat Mane Aug 4 '17 at 6:55
  • 2
    You should identify a unique parent then. We are unable to help with just this html snippet, id should be fine since it should be unique, maybe you are doing something wrong in your code. Please share code and more of the html. – lauda Aug 4 '17 at 7:46
  • How it is possible that locating by ID is not working? ID is supposed to be unique - is it not? If not unique - that is your problem (invalid HTML). – Peter M. Aug 4 '17 at 14:30
  • @Peter Masiar, I agree ID is supposed to be unique & is the reason why I tried to use it initially. I don't know why it was not recognized. That's why I thought of asking here. All 3 matching nodes have same id & attributes. I'm still new to Selenium & Java. Trying to find a way to make it work – kodesmann Aug 4 '17 at 21:58
  • @kodesmann - ID was not recognized because it is not unique. invalid HTML. Browsers are designed to ignore such invalid HTML, but it does not make it valid. – Peter M. Aug 4 '17 at 22:10
1

In your xpath simply add [1] or [2] or [3] to select the correct one out of the 3 matches.

driver.findElements(By.xpath("your locator here)[1]"));

This will fetch you the first element with same locator. Like wise

driver.findElements(By.xpath("(your locator here)[2]"));

will fetch you second element with the same locator

driver.findElement(By.id("(element id)[1]"))
  • 1
    Please edit your answer to explain what it adds to the existing answers to this question, and why it is a better answer than any of the others. sqa.stackexchange.com/help/how-to-answer has more information on what makes a good answer. – Kate Paulk Aug 7 '17 at 11:34
  • Thanks guys. Now got the permission :). Have updated the answer to reflect more data and clarity – Ashish Deshmukh Aug 10 '17 at 7:02
0

Maybe it is not a best solution but arguably the easiest one. Find all your elements, using List<WebElement> elements = driver.findElements(By.xpath("your locator here"));

And then refer to it by element id in list of elements.

0

Ad @dyymni said, if you cannot identify element directly, you have to use some scripting magic:

  • get list of matching elements, loop over it, and identify requested element by other means (some other attribute)
  • identify a parent element which you can locate uniquely, then find sub-element.

All depends of the flexibility of the scripting language you use. I use Python (and try to avoid XPath if i can - and usually I can), and such "magic" is trivial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.