0

So let say I have an array called list that contains strings per below, when I loop through the array to send_keys each items from the array onto an online text editor element which already has focus:

list = ["First", "Second", "Third"]

for index in 0 ... list.size
      line = list[index]
      chain.send_keys(line).perform
      if index < list.size 
        page.driver.browser.action.send_keys(:return).perform
      end
end

The problem I'm facing is that instead of the output to look like this:

First

Second

Third

it instead looks like this:

First

First Second

First Second Third

Why is this happening ? is it because the previous actions are still in the action queue and have not cleared up ? or some other reason ? I'd appreciate if anyone can help.

Syed Abbas
  • 15
  • 5
  • is the 'chain' a web element or an Action builder ? – Bharathan Kumaran Dec 08 '18 at 11:53
  • Chain is an ActionBuilder. I resolved my issues by using send_keys on an element instead of using the ActionBuilder which was causing issues since it was executing previously stored actions in the queue. Not sure why the action queue cannot be cleared at least within ruby selenium bindings – Syed Abbas Dec 08 '18 at 19:33

2 Answers2

1

When using the actions api it builds up a list of actions that are then executed by calling perform. Calling perform however doesn't reset that list, so if you call perform again it repeats the same actions. With the way you're calling it

chain.send_keys(line).perform

adds a send_keys action to chain - then performs it. Next time it adds another send_keys action to chain and then performs both actions. Solutions for that would be just create a new action chain each time rather than reusing chain or calling chain.clear_actions to clear the action chain each time through the loop.

What isn't clear though is why you're using the action API at all rather than just calling send_keys on the element you want to send the keys too

el = find(...)  # find the element on the page you want to send the keys to
list.each do { |str| el.send_keys(str, :return) }
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Thank you ! yeah the reason I had opted to use action API was that when I initially did try to call send_keys on the element, it was not working due to not getting focus so I instantly switched to using action API instead of spending some more time on the previous method; after you called it out, I went back and used the send_keys on the element and in very little time resolved the issue with the element getting the focus and hence it is working like a charm! Thanks a lot for your help ! – Syed Abbas Dec 07 '18 at 20:59
0

The way selenium sendkeys works is, the sendkeys commands sends the string value to the text element, It will not do any check whether is there any text present in it or not. If you want to have the keys set newly for each time, Please use the command chain.clear() before chain.send_keys(line).perform in the loop. This will ensure the text in the element is cleared each time before the send_keys.

Let me know if this doesn't help you.

https://selenium-python.readthedocs.io/navigating.html#interacting-with-the-page

Bharathan Kumaran
  • 981
  • 10
  • 22
  • Thanks ! but see I had tried that but was then getting this error: NoMethodError: undefined method `clear' for # . not sure if its a different syntax – Syed Abbas Dec 07 '18 at 18:48
  • @SyedAbbas can you try the command mentioned in the https://www.rubydoc.info/gems/selenium-webdriver/0.0.28/Selenium/WebDriver/Element#clear-instance_method – Bharathan Kumaran Dec 07 '18 at 18:49
  • per the doc, I had already tried chain.clear but still get this: Failure/Error: chain.clear NoMethodError: undefined method `clear' for # – Syed Abbas Dec 07 '18 at 18:55