user3624378
user3624378

Reputation: 417

Selenium and Angular

I'm trying to test an Angular site. The problem is that I can't click the element since no action is taken. So when I tell Selenium to find element By.xpath, it's not possible since I have not clicked the element. See picture below:

enter image description here

When I inspect the element "TEST1", the DOM is loaded. For example. see picture below:

enter image description here

Now I can copy full xpath:

/html/body/tvv-overlay/overlay/ng-transclude/div[1]/div/div/div[1]/div/div[2]/nyps-cases/div/ul/li[1]/a/span

But the problem is that my Selenium script just can't click the element since it is not loaded:

driver.findElement(By.xpath("/html/body/tvv-overlay/overlay/ng-transclude/div[1]/div/div/div[1]/div/div[2]/nyps-cases/div/ul/li[1]/a/span")).click();

Is it not doable to test Angular with Selenium?

Upvotes: 1

Views: 808

Answers (2)

Jayanth Bala
Jayanth Bala

Reputation: 838

Angular will construct the element dynamically, always go for wait until method based on your situations.

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

Upvotes: 0

cruisepandey
cruisepandey

Reputation: 29362

You can try with explicit waits :

Also Angular based pages are dynamic in nature, so explicit wait is a way to go.

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/tvv-overlay/overlay/ng-transclude/div[1]/div/div/div[1]/div/div[2]/nyps-cases/div/ul/li[1]/a/span"))).click();

also if you know that TEST1 will not change, then try this xpath.

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='TEST1']"))).click();

Recommendation is :

You should always use relative xpath, try to avoid absolute xpath as much as possible.

Upvotes: 2

Related Questions