繁体   English   中英

Selenium send_keys挂起在Python上

[英]Selenium send_keys hangs on Python

我目前正在编写“ 使用Python进行测试驱动开发 ”中的示例,更具体地说是第一次功能测试。 但由于一些奇怪的原因, send_keys无法正常工作。 这就是我现在正在尝试的 - 顺便说一句,我改变了隐式等待显式等待!

    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_keys(Keys.ENTER)永远不会起作用,当我看到浏览器作为测试运行时, inputbox1.send_keys('Use peacock feathers to make a fly')从不写“使用孔雀羽毛制作飞行”输入框。

怎么了? 我正在使用最新的Selenium( 我想,我几天前刚刚下载了它 ,我确实有最新版本),Python和Django版本,这将在我的笔记本电脑中打开Firefox Developer Edition。 谢谢。

编辑:我尝试在Firefox中禁用多进程 ,但结果不会改变 - 在尝试编写并按回车时仍然会挂起。

感谢alexce帮助我!

我在测试类中更改了以下内容:

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 Developer Edition,显然Selenium完全不支持它。 所以我只是强迫Selenium加载我的常规Firefox而且它不再挂起了!

奇怪的是,我无法在我的Ubuntu shell中运行任何东西,但它将通过同一服务器上的Jupyter Notebook从IPython运行。

我不得不在代码中添加一个虚拟显示,使其作为.py脚本从shell运行...

如果它帮助任何面临类似问题的人是我添加到我的脚本的代码行,发送键开始工作没有问题。 似乎即使我将无头开关留给我的镀铬驱动器,它仍然需要。

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM