1

I am new to Selenium with Java.

As per the requirement I need to access a TAB within my Angular application.

<a _ngcontent-ng-c689374991="" data-val="Dreams" routerlink="/dreams" data-toggle="tab" href="/dreams" role="tab" class="active"><span _ngcontent-ng-c870109355="" class="slanted-text">Dreams</span></a>

Every time I try to locate the TAB, it gives error stating "Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:"

Tried the following ways using xpath, but unable to achieve the result:-

  1. Using Full XPath
driver.findElement(By.xpath("/html/body/app-root/app-top-nav/div/div/div/div[2]/div/div[2]/ul/li[5]/a")).click();
  1. Using XPath
driver.findElement(By.xpath("//*[@id=\"offcanvasExample\"]/div[2]/ul/li[1]/a")).click();
  1. Using Router Link
driver.findElement(By.xpath("//div[@_ngcontent-ng-c870109355-router-link='/dreams']")).click();
  1. Using HREF
driver.findElement(By.xpath("//*[contains(text(), 'href=/dreams')]")).click();

Could someone please guide me?

4
  • Try XPATH of //a[@href='/dreams'] Commented Jan 16, 2024 at 20:41
  • @pcalkins - Still the same error. Commented Jan 17, 2024 at 7:03
  • Does an iframe appear in front of it? Commented Jan 17, 2024 at 8:50
  • be sure to use a webdriverwait... angular tends to load DOM objects after page load and/or update them frequently. Commented Jan 17, 2024 at 18:12

1 Answer 1

0

Wait for the element to be clickable with WebDriverWait in Selenium First, wait for the angular page to complete with the below method

public ExpectedCondition<Boolean> waitForPageLoadingAngular() {
    return driver -> {
        try {
            return (Boolean) ((JavascriptExecutor) getWebDriver()).executeScript("return window.jQuery.active == '0'");
        }
        catch(Throwable  t) {
            return true;
        }
    };
}

This will help to wait for element to be clickable

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Dreams']")));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.