1

I have been having trouble on referring to a search box on a website through Selenium in VBA. The HTML code of the box is:

<input type = "search" class ="form-control input-sm"
placeholder aria-controls="result_table"> ==$0

I have tried

bot.findElementByCssSelector(".form-control").SendKeys ("werresf")
bot.findElementByCssSelector(".form-control.input-sm").SendKeys ("werresf")
bot.findElementByCssSelector(".input-sm").SendKeys ("werresf")
bot.findElementByCssSelector(".form-control input-sm").SendKeys ("werresf")
bot.findElementByClassName("form-control input-sm").SendKeys ("werresf")

But none of them seems to work. Any help is greatly appreciated.

3
  • Have you tried bot.FindElement(By.CssSelector(".form-control")); ? Commented Feb 25, 2020 at 9:51
  • Yes I have, but it does not work unfortunately. It gives object required error by this way. When I try to findElementByClassName it gives another error. When I try to findElementByCssSelector it does not give any error but it does not do anything either. Commented Feb 25, 2020 at 10:03
  • bot.findElementByCssSelector(".form-control.input-sm").SendKeys ("werresf") should work in your case. But you might have multiple items matching with that CSS and Selenium will pick the first element in that case, which might be a hidden element. So please test your css .form-control.input-sm in the browser devtools and then select the right item. Also make sure you point to the right frame, if any. Commented Feb 25, 2020 at 15:18

1 Answer 1

2

To send a character sequence within the desired element you can use either of the following Locator Strategies:

  • Using FindElementByCss:

    bot.FindElementByCss("input.form-control.input-sm[aria-controls='result_table']").SendKeys ("werresf")
    
  • Using FindElementByXPath:

    bot.FindElementByXPath("//input[@class='form-control input-sm' and @aria-controls='result_table']").SendKeys ("werresf")
    

Reference

You can find a couple of relevant discussions in:

Sign up to request clarification or add additional context in comments.

3 Comments

The second one worked. Thank you so much man. I have tried using XPath before but I did not come across "and @aria-controls='result_table'" part when I copied the XPath from the webpage. Did you use a different method on getting the XPath?
@KKB That was my bad, there was an error within the first locator, which I have corrected now. I din't use any different method, rather you are free to choose any attribute. Let me know if you have further queries.
After a bit of tinkering, I found out that one should replace the double quotes on the copied XPath with single quotes, this way, the VBA editor does not give a syntax error and it works like a charm. I will definitely let you know if I have further queries. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.