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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
KKB
  • 37
  • 1
  • 8
  • Have you tried `bot.FindElement(By.CssSelector(".form-control"));` ? – Jose Martínez Poquet Feb 25 '20 at 09: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. – KKB Feb 25 '20 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. – supputuri Feb 25 '20 at 15:18

1 Answers1

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    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 Feb 26 '20 at 07:54
  • @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. – undetected Selenium Feb 26 '20 at 08:08
  • 1
    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. – KKB Feb 26 '20 at 08:17