硒PhantomJS send_keys不起作用 [英] selenium PhantomJS send_keys doesn't work

查看:206
本文介绍了硒PhantomJS send_keys不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用硒和PhantomJS进行测试.我遵循了Selenium的简单用法,但是send_keys在PhantomJS上不起作用,而在Firefox上起作用.为什么?我必须改用button.click()?

I am using selenium and PhantomJS for testing. I followed Selenium's simple usage, but send_keys doesn't work on PhantomJS, it works on Firefox. Why? I have to use button.click() instead?

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.PhantomJS()

driver.get("http://www.python.org/")
elem = driver.find_element_by_id("q")
elem.clear()
elem.send_keys("python")
elem.send_keys(Keys.RETURN)
# button = driver.find_element_by_id('submit')
# button.click()
print driver.title
print driver.page_source
driver.close()

推荐答案

我非常怀疑这只是计时问题.

I highly suspect it's just timing issue.

Selenium的click()如果在单击后重定向页面,将等待页面加载,而send_key()不等待. (PhantomJS是无头的,它比Firefox快)

Selenium's click() will wait for page to load if it is redirected after clicking, while send_key() doesn't wait. (PhantomJS is headless, which is faster than Firefox)

请尝试在elem.send_keys(Keys.RETURN)之后,print driver.title之前添加一些类似于time.sleep(5)的睡眠,并查看是否可以获得所需的结果.

Please try add some sleep like time.sleep(5) after elem.send_keys(Keys.RETURN), before print driver.title, and see if you can get the result you want.

在实际的测试项目中,只需使用WebDriverWait,直到driver.title等于所需的值.

In real testing project, you just need to use WebDriverWait until driver.title equals to the value you want.

这篇关于硒PhantomJS send_keys不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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