0

So I'm trying to test out the first name section of this link (https://softwaretestinggn8fe.herokuapp.com/) using selenium in java to show that the error message "First Name can only contain characters." will be shown when I type "545" into the first name section of the website. However my test fails and this is the error I get:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"*[name='submit']"}
  (Session info: chrome=86.0.4240.198)

Does anyone know how to solve this issue?

    import org.testng.AssertJUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.Assert;
    import org.testng.annotations.Test;
    
    
    public class WebTesting {
        
        @Test
        public void test1() {
            
            System.setProperty("webdriver.chrome.driver", "C:/Users/mtecl/Desktop/Drivers/chromedriver.exe");    // configure path to the driver
            
            WebDriver driver = new ChromeDriver();
            
            driver.get("https://softwaretestinggn8fe.herokuapp.com/");
            
            //Username
            WebElement testUsername = driver.findElement(By.id("firstname")); //id: first name
        
            testUsername.sendKeys("545");
            
            //Button
            driver.findElement(By.name("submit")).click();
            
            String expected = "First Name can only contain characters.";
            String actual = driver.findElement(By.xpath("html/body/div/main/div[2]/form/div[1]/span")).getText();
            
            Assert.assertEquals(expected, actual);
        
        }
    }
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

1

To click() on the element Sign up you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    driver.get("https://softwaretestinggn8fe.herokuapp.com/");
    WebElement testUsername = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#firstname")));
    testUsername.sendKeys("545");
    testUsername.submit();
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#firstname +span"))).getText());
    
  • xpath:

    driver.get("https://softwaretestinggn8fe.herokuapp.com/");
    WebElement testUsername = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='firstname']")));
    testUsername.sendKeys("545");
    testUsername.submit();
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='firstname']//following-sibling::span[1]"))).getText());
    
  • Console Output:

    First Name can only contain characters.
    
  • Browser Snapshot:

FirstName


References

You can find a couple of relevant detailed discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352