I am trying to create a Selenium function that will select a specific article in a Tree component. This article was already searched for in the corresponding searchbar. So it is the only element that is present.
I am trying to create the xPath for this element dynamically based on the ArticleId.
I know there are more simpler methods to select an element e.g. with By.className, By.Id, etc. ... but unfortunately this always selects the same element and does not work in my UseCase. So I try to select the element with the corresponding ArticleId.
My xPath looks like this: //span[contains(text(), 'articleId')]
The problem is that my test case does not find this element. But when I manually search for it in the FireFox console it works.
Heres my function:
public void selectArticleWithSpecificArticleNr(String article) {
WebElement correspondingArticle = waitTime.until(ExpectedConditions.elementToBeClickable(this.driver.findElement(By.xpath("//span[contains(text(), '" + article + "')]"))));
correspondingArticle.click();
}
That would be the span element as it is in the DOM:
<span _ngcontent-ewt-c164="" style="font-size: 13px; display: block; word-wrap: break-word; white-space: initial;" class="ng-star-inserted"> 10225929 </span>
I already tried to wait for a while before clicking on this element with Thread.sleep(ms), because maybe the element has not been present in the DOM yet. But that didn't work either.
The funny thing is that the first time I tried to run this test, the function found the element. But after I tried it again and again it didn't.