Sending Keys Using Splinter
Asked Answered
L

2

6

I want to test an autocomplete box using Splinter. I need to send the 'down' and 'enter' keys through to the browser but I'm having trouble doing this.

I am currently finding an input box and typing 'tes' into that box successfully

context.browser.find_by_xpath(\\some\xpath\).first.type('tes')

What I want to do next is to send some keys to the browser, specifically the 'down' key (to select the first autocomplete suggestion) then send the 'enter' key to select that autocomplete element.

I've tried extensive searches and can't figure out how to do this.

I even tried some javascript

script = 'var press = jQuery.Event("keypress"); press.keyCode = 34; press.keyCode = 13;'
context.browser.execute_script(script)

but that didn't do anything unfortunately

packages I'm using:

django 1.6 django-behave==0.1.2 splinter 0.6

current config is:
from splinter.browser import Browser from django.test.client import Client

context.browser = Browser('chrome')
context.client = Client()
Lester answered 10/2, 2015 at 11:15 Comment(0)
F
5

You can send keys by switching to the active element:

from selenium.webdriver.common.keys import Keys

context.browser.find_by_xpath('//input[@name="username"]').first.type('test')
active_web_element = context.browser.driver.switch_to_active_element()  
active_web_element.send_keys(Keys.PAGE_DOWN)
active_web_element.send_keys(Keys.ENTER)

The active element will be the last element you interacted with, so in this case the field you typed in.

switch_to_active_element() returns a selenium.webdriver.remote.webelement.WebElement, not a splinter.driver.webdriver.WebDriverElement, so unfortunately you cannot call send_keys on the return value of find_by_*(...) directly.

Fourierism answered 20/2, 2017 at 16:19 Comment(1)
To anyone facing problems with modals: You can select the modal by id and then send Keys.ESCAPE to get rid of it.Standing
C
-1

From the documentation this should work:

from splinter import Browser
from selenium.webdriver.common.keys import Keys

browser = Browser()
browser.type(Keys.RETURN)
Citizenry answered 5/9, 2022 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.