3

I need to open a webpage and send a keypress event (ctrl + f) which will move the focus to a text box inside the webpage. Then I need to send a string to that text box.

I have two problems:

  • The ctrl + f key press event does not work.
  • I am not able to locate the element in focus which is a text box element or send a string to that field.

This is the code I am trying:

self.driver = self.get_driver()
actions = ActionChains(self.driver)
actions.key_down(Keys.CONTROL).send_keys('f').key_up(Keys.CONTROL).perform()
editable = self.driver.execute_script("return document.activeElement")
print(editable.text)
editable.sendKeys("foo")

The solution in the other question did not solve my issue. I have provided the solution that worked for me, which is different from the way it works in Java version.

user3394432
  • 91
  • 1
  • 7
  • 1
    try `Keys.chord` http://stackoverflow.com/questions/11578768/how-to-press-ctrla-to-select-all-content-in-a-page-by-selenium-webdriver-using – johntellsall Jun 05 '14 at 19:46
  • This solution worked for me. self.driver.find_element_by_css_selector('body').send_keys(Keys.CONTROL, 'f') element = self.driver.switch_to_active_element() element.send_keys('foo') element = self.driver.switch_to_active_element() element.send_keys(Keys.RETURN) I needed to find the body of the webpage prior to sending the send_keys action. Also, switch_to_active_element was a simpler way of identifying element in focus instead of using execute_script("return document.activeElement"). – user3394432 Jun 06 '14 at 18:25
  • If this question is reopened you should repost your solution with the answer. – ZyX Jun 06 '14 at 18:59

0 Answers0