Sending Keys Using Splinter

2024/11/15 17:51:47

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 Browserfrom django.test.client import Client

context.browser = Browser('chrome')
context.client = Client()
Answer

You can send keys by switching to the active element:

from selenium.webdriver.common.keys import Keyscontext.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.

https://en.xdnf.cn/q/71727.html

Related Q&A

Split lists within dataframe column into multiple columns [duplicate]

This question already has answers here:Split a Pandas column of lists into multiple columns(13 answers)Closed 3 years ago.I have a Pandas DataFrame column with multiple lists within a list. Something l…

Drawing with turtle(python) using PyCharm

Im running the latest PyCharm Pro version and trying to run the below code from a scratch file but it doesnt seem to work import turtlewn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) a…

How to adapt my current splash screen to allow other pieces of my code to run in the background?

Currently I have a splash screen in place. However, it does not work as a real splash screen - as it halts the execution of the rest of the code (instead of allowing them to run in the background).This…

Reversed array slice including the first element [duplicate]

This question already has answers here:Python reverse-stride slicing(8 answers)Closed 5 years ago.Lets say I have:>>> a = [1, 2, 3, 4]And I want to get a reversed slice. Lets say I want the 1s…

Problem using py2app with the lxml package

I am trying to use py2app to generate a standalone application from some Python scripts. The Python uses the lxml package, and Ive found that I have to specify this explicitly in the setup.py file that…

How to run TensorFlow on AMD/ATI GPU?

After reading this tutorial https://www.tensorflow.org/guide/using_gpu I checked GPU session on this simple code import numpy as np import matplotlib.pyplot as plt import tensorflow as tfa = tf.constan…

Pure virtual function call

Im using boost.python to make python-modules written in c++. I have some base class with pure virtual functions which I have exported like this:class Base {virtual int getPosition() = 0; };boost::pytho…

Expected String or Unicode when reading JSON with Pandas

I try to read an Openstreetmaps API output JSON string, which is valid.I am using following code:import pandas as pd import requests# Links unten minLat = 50.9549 minLon = 13.55232# Rechts oben maxLat …

How to convert string labels to one-hot vectors in TensorFlow?

Im new to TensorFlow and would like to read a comma separated values (csv) file, containing 2 columns, column 1 the index, and column 2 a label string. I have the following code which reads lines in th…

Pandas dataframe boolean mask on multiple columns

I have a dataframe (df) containing several columns with an actual measure and corresponding number of columns (A,B,...) with an uncertainty (dA, dB, ...) for each of these columns:A B dA dB …