有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Selenium中检查表单元素有效性状态

我想知道是否可以从Selenium检查表单元素的验证状态。我想这是一种属性,但它的含义并不明显

//all these form elements are required as part of the form
WebElement createAccountEmailInput = this.wrapper.getDriver().findElement(By.id("createAccountEmail"));
WebElement createAccountUsernameInput = this.wrapper.getDriver().findElement(By.id("createAccountUsername"));
WebElement createAccountPasswordInput = this.wrapper.getDriver().findElement(By.id("createAccountPassword"));
WebElement createAccountPasswordConfirmInput = this.wrapper.getDriver().findElement(By.id("createAccountPasswordConfirm"));
WebElement createAccountSubmitButton = this.wrapper.getDriver().findElement(By.id("createAccountSubmitButton"));

//doesn't submit on no input
createAccountSubmitButton.click();

// at this point, all the inputs will be in the invalid state. How to verify that state?

表格:

<form id="homeCreateAccount">
    <div class="form-group">
        <label for="createAccountEmail">Email:</label>
        <input type="email" class="form-control loginEmailUsername" id="createAccountEmail"
        placeholder="Email" name="email" required>
    </div>
    <div class="form-group">
        <label for="createAccountUsername">Username:</label>
        <input type="text" class="form-control loginEmailUsername" id="createAccountUsername"
        placeholder="Username" name="email" required>
    </div>
    <div class="form-group">
        <label for="createAccountPassword">Password:</label>
        <input type="password" class="form-control loginPassword" id="createAccountPassword"
        placeholder="Password" name="password" required>
    </div>
    <div class="form-group">
        <label for="createAccountPasswordConfirm">Password Confirmation:</label>
        <input type="password" class="form-control loginPassword" id="createAccountPasswordConfirm"
        placeholder="Password Confirm" name="password" required>
    </div>
    <button type="submit" class="btn btn-primary" id="createAccountSubmitButton">Create Account <i class="fas fa-user-plus"></i></button>
</form>

共 (1) 个答案

  1. # 1 楼答案

    您可以在查询选择器中使用:invalid伪类来选择无效元素