使用Selenium粘贴命令 [英] Paste command using Selenium

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

问题描述

我正在Windows 7上使用Python 2.7和Selenium 2-44-0。我正在寻找一种比send_keys更快的输入文本的方法。 Send_keys一次将打印1个字母(这样可以更好地模仿实际用户)。我想要一种将所有内容都打印出来的方法,就像粘贴内容一样。

I'm using Python 2.7 and Selenium 2-44-0 on Windows 7. I'm looking for a quicker way of inputting text than using send_keys. Send_keys will print 1 letter at a time (which better imitates an actual user). I would like a way to print all of them out at once, as if the content was pasted.

例如,Sikuli具有以下功能:

For example, Sikuli has the following functionality:

paste("this will all populate the field at the same time")

我想知道是否有办法在Python中编写具有相同结果的方法。因此,而不是:

I'm wondering if there's a way to write a method in Python that will have the same result. So, instead of:

el.send_keys("this will do 1 letter at a time")

具有类似

el.paste_keys("this will do the entire line at once")

由于上述命令要求在硒功能中添加代码,使用python方法可能更有意义。也许是这样的:

Since the above command would require adding code to selenium functionality, it would prob make more sense to have a python method. Maybe something along the lines of:

def paste_keys(self, xpath, text):
    os.environ['CLIPBOARD'] = text
    el = self.driver.find_element_by_xpath(xpath)
    el.send_keys(Keys.CONTROL, 'v')

使用该环境变量实际上并不充当副本,而且我不知道如何在不下载第三方的情况下从代码级别设置剪贴板

Using that environmental variable doesn't actually act as a 'copy', though, and I don't know how to set the clipboard from the code level without downloading 3rd party software.

推荐答案

这有效:

def paste_keys(self, xpath, text):
    os.system("echo %s| clip" % text.strip())
    el = self.driver.find_element_by_xpath(xpath)
    el.send_keys(Keys.CONTROL, 'v')

不能有空格在%s之后,因为它将添加到复制的文本中。

There cannot be a space after %s, for it will add that to the copied text.

这篇关于使用Selenium粘贴命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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