简体   繁体   中英

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");
    }

  

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")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM