Question

I have a webpage with 2 search fields (top and bottom) and some text in-between them and I want to send a query to the bottom field. Both fields are identical (same code).

I tried storing the bottom one in a variable and use that variable to send the keys, but somehow it always sends the text to the first one.

var bottomSearch = _WebDriver.FindElements(By.Id("inputBox"))[1];
   Assert.IsTrue(bottomSearch.Displayed);
   bottomSearch.Clear();
   bottomSearch.Click();
   bottomSearch.SendKeys("test");
   bottomSearch.SendKeys(Keys.Enter);

So: Clear() works and properly deletes any text already present in the bottom search, Click() also works, SendKeys sends text to the top searchbox and SendKeys(Keys.Enter) goes back to the bottom one and presses Enter from there. I use the firefox driver and also tried selecting the element by CssSelectors or other identifiers but did not work.

Any help or ideas are most appreciated! Thank you!

Here is the code for the search fields:

<div class="searchbox-input">
<input id="inputBox" class="querybox" type="text" placeholder="Entrer le terme recherché" name="inputBox" value="test">
</div>
Was it helpful?

Solution

OK, so after 2 days of trial and error I asked the website's devs to change the id of the bottomSearch and now everything works well.

After all these tries I'm tempted to suspect a bug in the driver or the SendKeys() method, but since I failed to found any reference to back this up and I successfully dealt with same ID fields before, maybe it's just something in the website's implementation or mine.

So in conclusion, follow the best practice advices and this should never happen :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top