Selenium send_键挂起在Python上
我目前正在为“”中的示例编程,更具体地说是第一个功能测试。但由于某种奇怪的原因,Selenium send_键挂起在Python上,python,django,selenium,tdd,functional-testing,Python,Django,Selenium,Tdd,Functional Testing,我目前正在为“”中的示例编程,更具体地说是第一个功能测试。但由于某种奇怪的原因,发送密钥无法正常工作。这就是我现在正在尝试的——顺便说一下,我将隐式等待改为显式等待 inputbox = self.browser.find_element_by_id('id_new_item') self.assertEqual( # This passes, it's here just for completeness inputbox.get_attribute('plac
发送密钥
无法正常工作。这就是我现在正在尝试的——顺便说一下,我将隐式等待改为显式等待
inputbox = self.browser.find_element_by_id('id_new_item')
self.assertEqual( # This passes, it's here just for completeness
inputbox.get_attribute('placeholder'),
'Enter a To-Do item'
)
inputbox.send_keys('Buy peacock feathers')
inputbox.send_keys(Keys.ENTER) # Everything okay up to here
WebDriverWait(self.browser, 10).until(
EC.text_to_be_present_in_element((By.CSS_SELECTOR, "table#id_list_table tr td"), "Buy peacock feathers")
)
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr')
self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
inputbox1 = self.browser.find_element_by_id('id_new_item') # Changed the variable only to test if it would hang too - and it does
inputbox1.send_keys('Use peacock feathers to make a fly')
inputbox1.send_keys(Keys.ENTER) # This hangs
self.fail()
WebDriverWait(self.browser, 10).until(
EC.text_to_be_present_in_element((By.CSS_SELECTOR, "table#id_list_table tr td"), "Use peacock feathers to make a fly")
)
它永远不会到达self.fail()
。我试着把它移到前一行,测试失败了,这是应该的。但是,inputbox1.send_key(key.ENTER)
永远不会工作,当我看到浏览器作为测试运行时,inputbox1.send_key('Use piock feathers to make a fly')
从不在输入框中写入“Use piock feathers to make a fly”
发生了什么事?我正在使用最新的Selenium(我想,我几天前刚刚下载了它,我有最新的版本)、Python和Django版本,这将在我的笔记本电脑中打开Firefox开发者版。多谢各位
编辑:我试过了,但结果没有改变-当尝试写入并按enter键时,它仍然挂起。感谢Alexe的帮助 我在测试课上更改了以下内容:
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
def setUp(self):
binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe')
self.browser = webdriver.Firefox(firefox_binary=binary)
问题是什么?我使用的是Firefox开发者版,显然Selenium并不完全支持它。所以我只是强迫Selenium加载我的常规Firefox,它不再挂起 奇怪的是,我无法在我的Ubuntu shell中运行任何东西,但它将通过Jupyter笔记本上的IPython在完全相同的服务器上运行 我必须在代码中添加一个虚拟显示,使其作为.py脚本从shell运行 如果它能帮助任何面临类似问题的人,那么下面是我添加到脚本中的代码行,发送键开始正常工作。 而且,即使我为我的chrome驱动程序打开了无头开关,它仍然是需要的
from pyvirtualdisplay import Display
# Set screen resolution to 1366 x 768. This is needed
# to run in the shell. Seems fine in iPython.
display = Display(visible=0, size=(1366, 768))
display.start()
inputbox1.send_keys(keys.RETURN)怎么样?另外,您使用的是什么selenium和firefox版本?就是这样,输出相同。Firefox开发者版是46.0a2版,Selenium版是2.52.0版。好吧,我想可能是Firefox对于这个Selenium版本来说太新了。尝试将firefox降级到最新版本。让我知道,谢谢。你也可以试试chrome,这样我们就可以确定这是firefox特有的。