Selenium is not sending keys to my gwt-SuggestBox

790 Views Asked by At

I'm using selenium, scripting in python, to test a webpage that has a gwt-SuggestBox:

<div id="streamSuggestBox">
  <table> <tbody><tr> <td> <div class="gwt-Label errorText">Stream:</div> </td> 
  <td> <div><input type="text" class="gwt-SuggestBox"></div> </td> 
  </tr> </tbody></table>
</div>

selenium can find the div and the input widget:

(Pdb) sugInput = self.driver.find_element_by_id("streamSuggestBox").find_element_by_tag_name("input")
(Pdb) p sugInput.tag_name
u'input'

and if there is text in the input widget, sugInput.clear() clears it.

The problem is that sugInput.send_keys("s") does not work -- nothing is displayed in the input field and suggestions are not brought up. How should I enter data in my gwt-SuggestBox?

2

There are 2 best solutions below

0
On

You probably need to invoke a key press to get the input to start working. Here is how to do the above example from C# in python:

el = self.driver.find_element_by_xpath("//div[@id='streamSuggestBox']/input[@class='gwt-SuggestBox']")
script = "arguments[0].setAttribute('value', argument[1])"
self.driver.execute_script(script,el,[value])

To invoke the suggest you could probably do this:

el = self.driver.find_element_by_xpath("//div[@id='streamSuggestBox']/input[@class='gwt-SuggestBox']")
el.send_keys("s")
el.send_keys(Keys.TAB)

depending on what key press is binded to the suggest input this will probably work

0
On

You can try to directly set the value attribute. Sorry the code below is in C#, but the concept should be the same in python:

string script = "arguments[0].setAttribute('value', argument[1])";
IWebElement suggestionBox = //find it somehow...
driver.ExecuteScript(script, suggestionBox , "I typed this!");

The takeaway is to use javascript to directly modify the value attribute of the element. Selenium doesn't provide direct support for this in their API because they don't believe a user would ever directly modify a value. However for "difficult" objects sometimes it is necessary as a workaround.