硒中的无效元素

Invalid Element in selenium

Html 代码:-

< input ng-model="ctrl.email" type="email" class="no-controls md-input ng-valid-email ng-not-empty ng-dirty ng-valid ng-valid-required ng-touched" required aria-label="Email" id="input_0" aria-invalid="false" style>

硒代码:-

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver","R://MCA//Rushabh//Setup//Testing Setup//Workspace//Selenium//src//chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.get("URL");
    String email = "EMail";
    //WebElement Login =driver.findElement(ByClassName.className("segment-button"));
    WebElement Login = driver.findElement(By.cssSelector
        ("no-controls.ng-pristine.ng-untouched.md-input.ng-empty.ng-valid-email.ng-invalid.ng-invalid-required"));

    JavascriptExecutor jse = (JavascriptExecutor) driver;
    jse.executeScript("arguments[0].setAttribute('aria-invalid',true);",Login);
    Login.click();
}

在 Selenium 中显示错误:--

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"no-controls.ng-pristine.ng-untouched.md-input.ng-empty.ng-valid-email.ng-invalid.ng-invalid-required"}

在这里我可以使用哪个元素进行测试......

您可以为 Login 元素使用以下任何一种选择器:

  1. id : input_0

  2. xpath : //input[@ng-model='ctrl.email']

  3. xpath : //input[@type='email']

  4. xpath : //input[@class='no-controls md-input ng-valid-email ng-not-empty ng-dirty ng-valid ng-valid-required ng-touched']

  5. xpath : //input[contains(@class,'ng-valid-email')] 注意: 此处您可以 select class 值的任何唯一部分。

  6. CSS/CSS select或:input[class='no-controls md-input ng-valid-email ng-not-empty ng-dirty ng-valid ng-valid-required ng-touched']

  7. CSS/CSS select或:input[id='input_0']

Final Note : Try this locators one by one. Please also check locators type like xpath, csspath

Example and Syntax

这里使用的css选择器使用class no-controls.ng-pristine.ng-untouched.md-input.ng-empty.ng-valid-email.ng-invalid.ng-invalid-required 这不是一个好的做法,因为 class 会根据您在输入框上执行的操作而改变,例如单击、输入值等。

使用 id #input_0 作为 css 选择器

WebElement Login = driver.findElement(By.cssSelector("#input_0"));

或使用 aria-label input[aria-label='Email']

WebElement Login = driver.findElement(By.cssSelector("input[aria-label='Email']"));

HTML 输入 «

<input ng-model="ctrl.email" type="email" class="no-controls md-input ng-valid-email ng-not-empty ng-dirty ng-valid ng-valid-required ng-touched" required aria-label="Email" id="input_0" aria-invalid="false" style>

使用以下任何一项XPaths:

//input[@id="input_0"]
//input[@type="email"]
//input[@type="email"][@aria-label="Email"]

Java 代码 «

int ELEMENT_WAIT_TIME_SEC = 60;
WebDriverWait explicitWait = new WebDriverWait(driver, ELEMENT_WAIT_TIME_SEC);

String locator = "//input[@type='email'][@aria-label='Email']";
By findBy = By.xpath( locator );
WebElement element = explicitWait.until(ExpectedConditions.elementToBeClickable( findBy ));

jse.executeScript("arguments[0].setAttribute('aria-invalid',true);", element);