【问题标题】:Python selenium send_keys switches input before finishing first stringPython selenium send_keys 在完成第一个字符串之前切换输入
【发布时间】:2016-05-29 21:08:54
【问题描述】:

我正在使用 Firefox 驱动程序使用 selenium 针对 Django 的 StaticLiveServerTestCase 编写功能测试。

由于某种原因,我的send_keys 在中间被切断,其余的被发送到另一个领域:


密码字段的类型设置为“文本”以显示问题。

这是我的测试用例,非常接近the example in Django documentation

from django.contrib.auth import get_user_model
from django.contrib.staticfiles.testing import StaticLiveServerTestCase

from selenium.webdriver.firefox.webdriver import WebDriver

User = get_user_model()

class LoginSpec(StaticLiveServerTestCase):

    @classmethod
    def setUpClass(cls):
        super(LoginSpec, cls).setUpClass()
        cls.selenium = WebDriver()
        User.objects.create_user('username', 'username@example.com', 'password')

    @classmethod
    def tearDownClass(cls):
        User.objects.all().delete()
        cls.selenium.quit()
        super(LoginSpec, cls).tearDownClass()

    def test_login_with_valid_credentials(self):
        self.selenium.get('%s%s' % (self.live_server_url,  "/login"))

        username = self.selenium.find_element_by_name("username")
        username.send_keys("username")
        password = self.selenium.find_element_by_name("password")
        password.send_keys("password")
        ...

【问题讨论】:

  • 如果您在浏览器中手动执行相同的操作 - 同样的问题?

标签: python django selenium


【解决方案1】:

看起来(出于某种原因)某些东西将“用户名”中的“r”解释为 control 字符(将焦点移至下一个字段),而不是常规字符。

两种可能的解决方法:您可以在调用 send_keys 之前 clear() 元素和/或一次发送一个字符串中的字母:

def test_login_with_valid_credentials(self):
    self.selenium.get('%s%s' % (self.live_server_url,  "/login"))

    username = self.selenium.find_element_by_name("username")
    username.clear()
    username.send_keys("username")
    # if clear() alone doesn't solve the problem, you might also try:
    # for x in list("username"):
    #     username.send_keys(x)
    password = self.selenium.find_element_by_name("password")
    password().clear
    password.send_keys("password")
    ...

【讨论】:

    猜你喜欢
    • 2023-02-17
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    相关资源
    最近更新 更多