使用Splinter发送密钥 [英] Sending Keys Using Splinter

查看:140
本文介绍了使用Splinter发送密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Splinter测试自动完成框。我需要将向下和输入键发送到浏览器,但是这样做很麻烦。

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.

我目前正在找到一个输入框,并在该框中成功输入 tes

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

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

接下来要做的是向浏览器发送一些键,特别是向下键(选择第一个自动完成建议),然后发送 enter键选择该自动完成元素。

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甚至尝试过一些javascript

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

我正在使用的软件包:

django 1.6
django- behavior == 0.1.2
splinter 0.6

django 1.6 django-behave==0.1.2 splinter 0.6

当前配置为:

from splinter.browser import django的浏览器
.test.client import Client

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

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


推荐答案

您可以通过切换到活动元素来发送密钥:

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)

活动元素为la您与之交互的第一个元素,因此在这种情况下,您键入的字段。

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

switch_to_active_element()返回 selenium.webdriver.remote.webelement.WebElement ,而不是 splinter.driver.webdriver.WebDriverElement ,因此很遗憾您无法调用 send_keys 直接返回 find_by _ *(...)的返回值。

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.

这篇关于使用Splinter发送密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆