Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Seleniu send#u key can';t型数字,如5或6_Python_Selenium - Fatal编程技术网

Python:Seleniu send#u key can';t型数字,如5或6

Python:Seleniu send#u key can';t型数字,如5或6,python,selenium,Python,Selenium,所以我在google selenium网站上看到了这个问题,但它还没有得到解决 当您创建元素时。发送_键('12345') 它将返回“123”。5被解析为backspace 这有什么办法吗 使用最新的selenium、chrome、chromedriver、python 2.7、Ubuntu12.04,我没有chromedriver,因此无法进行测试,但键入数字5的另一种方法是使用以下命令: Keys.NUMPAD5 您的代码如下所示: element.send_keys(Keys.NUMPA

所以我在google selenium网站上看到了这个问题,但它还没有得到解决

当您创建元素时。发送_键('12345')

它将返回“123”。5被解析为backspace

这有什么办法吗


使用最新的selenium、chrome、chromedriver、python 2.7、Ubuntu12.04,我没有chromedriver,因此无法进行测试,但键入数字5的另一种方法是使用以下命令:

Keys.NUMPAD5
您的代码如下所示:

element.send_keys(Keys.NUMPAD5)
PS:发送“5”在FirefoxDriver上运行良好,上述命令也是如此

driver = webdriver.Chrome("\\chromedriver.exe")
driver.get("Https://www.google.com")
driver.find_element_by_id("lst-ib").send_keys("12345")

在windows上使用最新的selenium、chrome、chromedriver和Python2.7,它运行良好

这似乎是一个bug。。。我找到了一个可能的解决办法

通过搜索,我找到了。在讨论中,提到了一种解决方法:使用
textBox.send_-keys(“R&d”)
,而不是
textBox.send_-keys(“R/&d”)
,然后删除额外的
/
字符即可正常工作。额外的字符被删除,如下所示:

textBox.send_keys("R/&d")
textBox.sendKeys(Keys.LEFT);
textBox.sendKeys(Keys.LEFT);
textBox.sendKeys(Keys.BACK_SPACE);
我不能测试这个,因为我没有类似于你的设置。我很高兴听到这是否有效

send_keys()方法在我的windows设置中正常工作,但我构造了一个可能解决您的问题的小方法。正如其他人已经建议的那样,它使用
selenium.webdriver.common.keys.keys
常量:

def send_keys_workaround(element, string):
    for s in string:
        if s == '5':
            element.send_keys(Keys.NUMPAD5)
        elif s == '6':
            element.send_keys(Keys.NUMPAD6)
        else:
            element.send_keys(s)
我已经建立了一个简约的JSFIDLE,以测试它是否至少在windows上工作。我希望它也能在你的Ubuntu上运行。全文如下:

from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


def send_keys_workaround(element, string):
    for s in string:
        if s == '5':
            element.send_keys(Keys.NUMPAD5)
        elif s == '6':
            element.send_keys(Keys.NUMPAD6)
        else:
            element.send_keys(s)


driver = WebDriver("****INSERT PATH TO CHROMEDRIVER***")
driver.get("http://jsfiddle.net/zwktb7tn/")

wait = WebDriverWait(driver, 20, 2) 
wait.until(expected_conditions.frame_to_be_available_and_switch_to_it((By.NAME, 'result')))

element = driver.find_element_by_id('inputfield')
send_keys_workaround(element, "123456") 

我也有这个问题。这是由于我在远程Ubuntu(16.04)实例上安装了VNC客户端(tightVNC)


我按照这里的建议()删除了tightVNC。我安装了vnc4server,这似乎解决了无法传递特定数字的异常行为。

感谢jump4fun的帮助。在我的代码中使用了它,它是有效的。我可以在命令行中输入数字,它们被转换为send_键的字符串

if page_number == 1:
   pass
else:
   print('Attempt to start from Page', page_number)
   driver.find_element_by_id(page_number_id).click()
   driver.find_element_by_id(page_number_id).send_keys(Keys.CONTROL, 'a')
   driver.find_element_by_id(page_number_id).clear()
   element = driver.find_element_by_id("pageNumber")
   # send_keys doesn't like numbers directly from the gui but is OK if NUMPAD is used
   # this workaround maps the entered digits to the send_key string argument
   send_keys_workaround(element, str(page_number)) 
   driver.find_element_by_id(page_number_id).send_keys(Keys.RETURN)
   sleep(10)

soup = BeautifulSoup(driver.page_source, 'lxml')

# obtain the current page and last page numbers
page=soup.find('div', attrs={'class':'text-lg-right'})
# print(page)
page_number = page.input['value']

正如亲爱的@Epiwin所提到的,在TightVNC或chrome驱动程序和它的组合中,这是一个可怕的bug,我已经完全删除了它,并安装了TigerVNC,最终它成功了。
顺便说一句,我不知道为什么!但是在迁移到TigerVNC后,远程连接的速度提高了,这是我的另一个优点。

你能指定准确的URL并指向你要填写的准确文本框吗?不管它是什么URL。无法键入5或6您是否尝试发送原始字符串?在原始字符串
r'12345'
中,应忽略任何其他字符串解释。它对于在Windows中编写正确的路径名非常有用。@scorreia是的,它将5和6解释为退格。我可能会放弃使用selenium的方式,只使用javascript……这听起来像个bug!您应该在上发布(但首先搜索它以查看是否已提出此问题)。这是迄今为止最好的答案,我将发布我的发现。如果您以某种方式单独发送密钥,您可能会很幸运,或者找出到底出了什么问题。例如,
send_-keys(list('12345'))
或“'12345'”:元素中字母的
。send_-keys(letter)
,但这只是一个猜测。这在ubuntu 14.10中也可以正常工作。也许ubuntu 12.04最新的selenium和chromedriver已经过时了?不过,我还没有用Xvfb测试它。