【问题标题】:Verifying form input stops for few seconds using Selenium使用 Selenium 验证表单输入停止几秒钟
【发布时间】:2021-02-10 02:01:30
【问题描述】:

我目前正在尝试为不同的输入字段(用户名、姓名、密码和确认密码)输入特定值。输入值后,它会单击提交按钮,并返回任何validationMessage 错误。但是,对于密码确认文本输入,即使所有其他文本输入都通过,它也会显示错误。

代码:

try:

element = driver.find_element_by_name("username")
element.send_keys(random_value)
if WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#username[name='username']"))).get_attribute("validationMessage") !="":
    print(WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#username[name='username']"))).get_attribute("validationMessage"))
else:
    print("Username: PASS")
  
except:
    print("Check username html value")

try:
    element = driver.find_element_by_name("name")
    element.send_keys(random_value)
    if WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#name[name='name']"))).get_attribute("validationMessage") !="":
        print(WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#name[name='name']"))).get_attribute("validationMessage"))
    else:
        print("Name: PASS")  

    
except:
    print("Check name html value")


try:
    element2 = driver.find_element_by_name("password")
    element2.send_keys(random_value)
    if WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#password[name='password']"))).get_attribute("validationMessage") !="":
        print(WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#password[name='password']"))).get_attribute("validationMessage"))
    else:
        print("Password: PASS")
    
except:
    print("Check password html value")

try:
    element3 = driver.find_element_by_name("password_confirmation")
    element3.send_keys(random_value)
    if WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#password_confirmation[name='password_confirmation']"))).get_attribute("validationMessage") !="":
        print(WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#password_confirmation[name='password_confirmation']"))).get_attribute("validationMessage"))
    else:
        print("Password confirmation: PASS")
    
except:
    print("Check password confirm html value")

目前我得到的错误是输出是“检查密码确认 html 值”,但是 driver.find_element_by_name("password_confirmation") 是正确的,它确实输入了指定的字符串

【问题讨论】:

  • 使用except Exception as e: print(e) 打印出您的try 块中发生的错误。
  • 我得到这个作为输出,只有很多白线,'消息:(白线)'
  • 如果在没有 try/except 子句的情况下运行代码会出现什么错误?
  • 引发 TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
  • 所以在尝试查找密码确认时超时。尝试将超时时间从 1 增加到 10。如果这不起作用,请仔细检查 html 源代码。此外,在您已将 element3 分配给 password_confirmation 元素后,无需再引发 webdriverwait。这同样适用于所有的 try/except 块。

标签: python selenium


【解决方案1】:

看来您的 css 选择器是错误的。您正在使用input#,它将搜索id。 html 中的id 不是pass_confirmation,而是password_confirm

试试这个。

try:
    element3 = driver.find_element_by_name("password_confirmation")
    element3.send_keys(random_value)
    if WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#password_confirm[name='password_confirmation']"))).get_attribute("validationMessage") !="":
        print(WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#password_confirm[name='password_confirmation']"))).get_attribute("validationMessage"))
    else:
        print("Password confirmation: PASS")
    
except:
    print("Check password confirm html value")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-30
    • 2012-09-15
    • 1970-01-01
    • 2021-03-12
    • 2014-05-18
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多