Search code examples
seleniumtestingautomationtestng-eclipsetestng-annotation-test

How to find locator xpath/cssselector for a tag inside the span tag?


This is the HTML Code[enter image description here][2]

I tried the below one. It's working. But am converting to TestNG the code will throw errors.

WebElement userActions = driver.findElement(By.xpath("//div[@class='row col-md-12 col-sm-12']"));
WebElement activateButton = userActions.findElements(By.xpath("//span[@class='ng-scope']")).get(1); 
activateButton.click();

Error:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='row col-md-12 col-sm-12']"}
  (Session info: chrome=102.0.5005.115)


TestNG:

    @Test(dependsOnMethods= {"selectClient"})
    public void activateUser() throws InterruptedException {
        WebElement userActions = driver.findElement(By.xpath("//div[@class='row col-md-12 col-sm-12']"));
        WebElement activateButton = userActions.findElements(By.xpath("//span[@class='ng-scope']")).get(1);
        activateButton.click();
        DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd MMMM yyyy");
        LocalDate date=LocalDate.now();
        WebElement mve = driver.findElement(By.xpath("//form/div[3]/div/input[@id='activationDate' and @type='text']"));
        mve.sendKeys(DTF.format(date));
        mve.sendKeys(Keys.TAB);
        WebElement activateClient = driver.findElement(By.xpath("//*[@id=\"save\" and @has-permission=\"ACTIVATE_CLIENT\" and @type='submit']"));
        activateClient.click();
        System.out.println("User Activation Successfully");
    }

  

Solution

  • Have you tried with belwo ExplicitWait,

    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    WebDriverWait wait = new WebDriverWait(driver,30);
    
    
    userActions = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("Xpath")));
    

    OR

    userActions = wait.until(ExpectedConditions.presenceOfElementLocated(By.xapth("Xpath")));
    

    OR

    userActions = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("Xpath")));